custom-components / weatheralerts

A sensor that gives you weather alerts from alerts.weather.gov.
MIT License
123 stars 15 forks source link

Canadian Weather availability #40

Open mcn1964x opened 4 years ago

mcn1964x commented 4 years ago

Would it be possible to add a Canadian weather alert option?

thanks,

Chris

jlverhagen commented 4 years ago

I'll try to get it added when I get the config flow coded into weatheralerts to allow for easier UI integration configuration. The Canadian weather alert feed is in XML format and contains much less information than the US NWS weather alert feed. I don't think there will be too much difficulty integrating it if I can simplify the current YAML packages, otherwise the Canadian alerts may need their own YAML package to handle notification automations.

If you don't want to wait, I'll post some YAML and Lovelace code to allow you to display weather alerts using the Feedparser integration (installable via HACS). And then, with some extra template sensors set up in YAML, reliable automations could be used to also trigger notifications.

I'll get some YAML and Lovelace code tested and posted this week.

[post edited] Initial testing of Feedparser with scan_interval set in YAML platform config seems to be working to override the hardcoded interval. I'll keep testing to be sure.

jlverhagen commented 4 years ago

Here is what I came up with to be able to display Canadian weather alerts in the UI. First you'll need to install the Feedparser integration (installable via HACS). Then you will need to get your alert RSS feed URL for your location. Go to https://weather.gc.ca/canada_e.html and click your province or territory link. On your province or territory page, scroll down and you will find a list of cities. Choose your city, or the nearest city to you. Once on the city weather page, scroll down past the Current Conditions, Forecast, Averages and extremes, and Yesterday's Data, and you will see a Follow box. Click the RSS icon (first icon). A popup box will give you Weather and Alerts buttons. Click the Alerts button and it will open the alerts RSS feed for your selected city. The URL for that alerts RSS feed is what you will enter into the Feedparser platform configuration posted below.

If using YAML packages, create a new YAML package file and add this code (change the feed_url URL to the appropriate alert feed URL for your city):

sensor:
  - platform: feedparser
    name: Canada Weather Alerts
    feed_url: 'https://weather.gc.ca/rss/warning/mb-39_e.xml'
    date_format: '%Y-%m-%dT%H:%M:%SZ'
    scan_interval: 60

  - platform: template
    sensors:
      #sensor that only counts active alerts
      canada_weatheralerts_active_alerts:
        friendly_name: Weather Alerts
        entity_id:
          - sensor.canada_weather_alerts
          - sensor.time_hour
        unit_of_measurement: Alerts
        icon_template: mdi:alert-rhombus
        value_template: >-
          {% set alerts_active = namespace(count=0) %}
          {% if (state_attr('sensor.canada_weather_alerts', 'entries')) %}
            {% for entry in state_attr('sensor.canada_weather_alerts', 'entries') %}
              {% if ' ended' not in entry['summary'].lower() and 'no watches or warnings in effect' not in entry['summary'].lower() %}
                {% set alerts_total.count = alerts_total.count + 1 %} 
              {% endif %}
            {% endfor %}
          {% endif %}
          {{ alerts_active.count }}

      #sensor that counts all active and ended/expired alerts
      canada_weatheralerts_total_alerts:
        friendly_name: Weather Alerts
        entity_id:
          - sensor.canada_weather_alerts
          - sensor.time_hour
        unit_of_measurement: Alerts
        icon_template: mdi:alert-rhombus
        value_template: >-
          {% set alerts_total = namespace(count=0) %}
          {% if (state_attr('sensor.canada_weather_alerts', 'entries')) %}
            {% for entry in state_attr('sensor.canada_weather_alerts', 'entries') %}
              {% if 'no watches or warnings in effect' not in entry['summary'].lower() %}
                {% set alerts_total.count = alerts_total.count + 1 %} 
              {% endif %}
            {% endfor %}
          {% endif %}
          {{ alerts_total.count }}

If you don't use YAML packages, you will have to place the platform: sections above into your existing sensor: configuration (probably in configuration.yaml or sensor.yaml).

And then to display the active alerts in a Lovelace Markdown card, you could use something like this:

{% for entry in state_attr('sensor.canada_weather_alerts', 'entries') %}
{% if ' ended' not in entry['summary'].lower() and 'no watches or warnings in effect' not in entry['summary'].lower() %}
Title: {{ entry['title'].split(' , ')[0] }}
Area: {{ entry['title'].split(' , ')[1] }}
Summary: {{ entry['summary'].split(' Issued: ')[0] }}
Issued: {{ entry['summary'].split(' Issued: ')[1] }}
Published: {{ as_timestamp(entry['published'])|timestamp_local }}
Updated: {{ as_timestamp(entry['updated'])|timestamp_local }}
{% endif %}
{% endfor %}

Automations to control notifications would require some additional work. Notifications could be set up with what is here, but they probably will not be very reliable if based only on alert counts. Additional sensor(s) would be needed to track the alert IDs that caused triggering so older alerts don't retrigger notifications when another alert has ended.

TRusselo commented 2 years ago

thanks for the workaround. would love to see an actual integration someday!