EDIT (04/2023): There is a new integration that can be used in place of the information found in this first post.
It uses the custom integration found here. There is additional information at that link for configuration and usage examples
Due to the possible discontinuation of the WU weather API at the end of the year, I was motivated to find a reliable alternative for timely emergency weather notifications. We already have a weather radio but it’s in a part of the house that we might not hear at all times so I wanted to come up with a way to augment that system. Here is what I’ve come up with.
This set up uses the public API directly from the US National Weather Service so unfortunately it won’t be useful to anyone outside the US. But I recently saw a post by a non-US user addressing the same scenario for the EU so that may be an option for those users.
This will allow several functions including a persistent pop-up notification on your HA front end, a notification to whichever notify service you use (I’m using Pushbullet in this example), and, if you use the awesome custom component by @keatontaylor (Echo Devices (Alexa) as Media Player - Testers Needed), it will also trigger an announcement to all of your Echo devices.
I’m no programming expert so some of this stuff might be done more smoothly (and I’m open to suggestions…) but I’ve tested it as much as I can and it’s working so far. I haven’t been able to fully test the “warning” test yet but I’m pretty confident it will work. At least it worked using created data in my template editor.
Also thanks to @pnbruckner for helping me sort out some details.
You first need to find either your NWS Zone ID or County ID. I’m not sure which is better but I used my Zone ID here.
You can find your Zone ID by going to https://alerts.weather.gov/, scroll down to your state and click on the “zone list” then look for the entry for your county.
Here are the code sections:
Once you have your Zone ID you create two sensors, replacing my zone id (INZ009) with yours. The Zone ID is case sensitive!:
sensor:
- platform: rest
resource: https://api.weather.gov/alerts/active/count
name: NWS Alert Count
value_template: >
{% if value_json.zones.INZ009 is defined %}
{{ value_json.zones.INZ009 }}
{% else %}
0
{% endif %}
headers:
User-Agent: Homeassistant
Accept: application/ld+json
scan_interval: 60
- platform: rest
resource: https://api.weather.gov/alerts/active?zone=INZ009
name: NWS Alert Event
value_template: >
{% if value_json.features[0] is defined %}
{{ value_json['features'][0]['properties'].event }}
{% else %}
None
{% endif %}
json_attributes:
- features
headers:
User-Agent: Homeassistant
Accept: application/geo+json
scan_interval: 60
Then you create the automations:
- alias: 'NWS Weather Alert Pop Up Control'
trigger:
platform: state
entity_id: sensor.nws_alert_count
action:
service: script.nws_popup_on_wx_alert
- alias: NWS Notification Weather Alert
trigger:
platform: state
entity_id: sensor.nws_alert_count
condition:
condition: template
value_template: '{{states.sensor.nws_alert_count.state | int > 0}}'
action:
- service: notify.pushbullet_notify
data:
message: "NWS: {{ states.sensor.nws_alert_event.state }}"
- alias: NWS Announcement Weather Alert
trigger:
- platform: state
entity_id: sensor.nws_alert_count
condition:
condition: and
conditions:
- condition: template
value_template: '{{states.sensor.nws_alert_count.state | int > 0}}'
## Added a time condition because I don't want a non-emergency alert waking me up
- condition: or
conditions:
- condition: time
after: '09:00:00'
before: '23:00:00'
- condition: template
value_template: >
{{ 'Warning' in states.sensor.nws_alert_event.state }}
action:
#- service: media_player.volume_set
# data:
# entity_id:
# - media_player.computer_room_dot
# - media_player.kitchen_dot
# - media_player.livingroom_dot
# - media_player.master_bedroom_dot
# volume_level: 0.9
- service: media_player.alexa_tts
data_template:
entity_id:
- media_player.computer_room_dot
- media_player.kitchen_dot
- media_player.livingroom_dot
- media_player.master_bedroom_dot
- media_player.garage_dot
- media_player.big_room_dot
message: "Attention!. . . Attention!. . . The National Weather Service has issued a
{{ states.sensor.nws_alert_event.state }} for your area.
It expires at {{ as_timestamp(state_attr('sensor.nws_alert_event', 'features')[0].properties.expires)| timestamp_custom('%-I:%M %p on %-m-%-d-%Y') }}."
- delay: '00:00:10'
- service: media_player.alexa_tts
data_template:
entity_id:
- media_player.computer_room_dot
- media_player.kitchen_dot
- media_player.livingroom_dot
- media_player.master_bedroom_dot
- media_player.garage_dot
- media_player.big_room_dot
message: "Attention!. . . Attention!. . . The National Weather Service has issued a
{{ states.sensor.nws_alert_event.state }} for your area.
It expires at {{ as_timestamp(state_attr('sensor.nws_alert_event', 'features')[0].properties.expires)| timestamp_custom('%-I:%M %p on %-m-%-d-%Y') }}."
You’ll notice I repeat the announcement twice in case the first announcement doesn’t wake you up. And the only announcement that happens between 11PM & 9AM are alerts that contain a ‘warning’ in the event.
And I haven’t worked out the volume setting part yet. If anyone has any working solutions feel free to let me know.
Last you create the script that generates the persistent notification pop-up:
nws_popup_on_wx_alert:
alias: NWS Weather Alert Pop Up
sequence:
## Dismiss any current alert so the UI isn't filled
## up with these if there are more then one.
## Only show the latest alert
- service: persistent_notification.dismiss
data:
notification_id: "nwswxalert"
## Create a new persistant notification in the UI for a new alert
- service_template: >
{% if states.sensor.nws_alert_event.state != 'None' %}
persistent_notification.create
{% endif %}
data_template:
notification_id: "nwswxalert"
message: "{{ state_attr('sensor.nws_alert_event', 'features')[0].properties.description }}"
title: '{{ states.sensor.nws_alert_event.state }}'
I will (hopefully) also come up with a way to handle notifications when there are more than one simultaneous alerts for an area. I have a work in progress but I need to be able to test it.
If you don’t have any active alerts and would like to test all of this the just go to https://api.weather.gov/alerts/active/count and search in those results for a zone that has an entry and use that ID.
I hope this helps others and I’m looking forward to constructive feedback.