GENWiki

Premier IT Outsourcing and Support Services within the UK

User Tools

Site Tools


opensource:pulling_the_uk_severe_weather_warnings_from_the_met_office

Metoffice Regional Severe Weather Warnings

I know, just look out the window right? Well, if for some reason you want to integrate the severe weather warnings with Home Assistant then you can do it, but not easily.

The Metoffice only publish the warnings via RSS, and on their website - there is no API to allow this to be retrieved (for some reason). The 'area' is defined in the RSS feed URL and you can get these URL's from the metoffice website. In this example we're using 'em' east midlands, which is most applicable to me.

We're going to do this with Pyscript, and AppDaemon, both are valid and both work the same (basically).

Firstly, create a helper called input_text.metofficeww (that's an text helper called metofficeww).

PyScript

  1. import feedparser
  2. import requests
  3. import json
  4. import pprint
  5. import datetime
  6. @pyscript_compile
  7. def getmetww(url):
  8. try:
  9. headers = {'User-Agent': 'curl/7.68.0'}
  10. response = requests.get(url, headers=headers)
  11. data = response.content # bytes cus its UTF-8
  12. return data, None
  13. except Exception as exc:
  14. return None, exc
  15.  
  16. @service
  17. def metww(action=None, id=None):
  18. """Metoffice Weather Warnings API (RSS) fetcher and parser"""
  19. log.info(f"MI5 got action {action} id {id}")
  20. contents, exception = task.executor(getmetww, "https://www.metoffice.gov.uk/public/data/PWSCache/WarningsRSS/Region/em")
  21. if exception:
  22. raise exception
  23.  
  24. feed = feedparser.parse(contents)
  25. if feed.entries:
  26. ww = feed.entries[0].summary
  27. else:
  28. ww = ""
  29.  
  30. cww = input_text.metofficeww
  31. if cww != ww:
  32. log.info(f"Current Value: {cww} new value {ww}")
  33. input_text.metofficeww = ww

You would place this in your ./pyscript folder, in a file named something like metofficeww.py and then after changing the URL to your region you'd call the pyscript.reload service to load it in.

Check your logs for any booboo's, but if you're all good, fire it off with a call to its pyscript.metww service. You should then setup automation to call that service at a sensible time, like 0700, or even on a recurring schedule like every 4 hours if you're going to do something with it during the day. For me, I call it at 0700 and then announce it with my alarm IF there's a warning.

AppDaemon

For anyone who wants to do it the AppDaemon way, its very similar:

  1. import hassapi as hass
  2. import feedparser
  3. import requests
  4. import json
  5. import pprint
  6. import datetime
  7.  
  8. class MetOfficeWW(hass.Hass):
  9.  
  10. def initialize(self):
  11. self.run_hourly(self.run_schedule, datetime.time(hour=0))
  12.  
  13. def run_schedule(self, cb_args):
  14. url = "https://www.metoffice.gov.uk/public/data/PWSCache/WarningsRSS/Region/em"
  15. response = requests.get(url)
  16. data = response.content # bytes cus its UTF-8
  17. feed = feedparser.parse(data)
  18.  
  19. if feed.entries:
  20. ww = feed.entries[0].summary
  21. else:
  22. ww = ""
  23.  
  24. cww = self.get_entity("input_text.metofficeww")
  25.  
  26. if cww.state != ww:
  27. log = "Setting metofficeww to "+ww
  28. self.log(log)
  29. self.set_textvalue("input_text.metofficeww",ww)
  30. self.set_state("input_text.metofficeww",ww)
  31. else:
  32. self.log("No Change Required")

Don't forget to add it to your apps.yaml file, something like this (assuming you called the file metofficeww.py

  1. metofficeww:
  2.   module: metofficeww
  3.   constrain_start_time: "08:00:00"
  4.   class: MetOfficeWW
  5.   constrain_end_time: "22:00:00"
/home/gen.uk/domains/wiki.gen.uk/public_html/data/pages/opensource/pulling_the_uk_severe_weather_warnings_from_the_met_office.txt · Last modified: 2024/02/21 17:59 by genadmin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki