Skip to content

Instantly share code, notes, and snippets.

@finity69x2
Last active November 9, 2022 00:06
Show Gist options
  • Save finity69x2/2b8336429008e9e783bdbf70905c9f6c to your computer and use it in GitHub Desktop.
Save finity69x2/2b8336429008e9e783bdbf70905c9f6c to your computer and use it in GitHub Desktop.
Home Assistant Configuration for Adding Weather Alerts from the US NWS using a REST Sensor
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 another custom component (https://community.home-assistant.io/t/echo-devices-alexa-as-media-player-testers-needed/58639), it will also trigger an announcement to all of your Echo devices.
You next need to find either your NWS Zone ID or County ID. It is better to use both ID's here.
You can find your Zone or County ID by going to https://alerts.weather.gov/, scroll down to your state and click on the “zone list” and/or "county list" then look for the entry for your county.
Once you have your desired ID you create a sensor, replacing my id (INZ009, INC033) with yours. The ID is case sensitive!
Then when you have your ID's, enter them into the configuration below.
Here are all the sections required in the configuration.yaml:
---------------------------------
sensor:
- platform: rest
resource: https://api.weather.gov/alerts/active/count
name: NWS Alert Count Raw
value_template: >
{% if value_json is defined and value_json.zones.INZ009 is defined %}
{{ value_json.zones.INZ009 }}
{% elif value_json is defined and value_json.zones.INC033 is defined %}
{{ value_json.zones.INC033 }}
{% else %}
0
{% endif %}
headers:
User-Agent: Homeassistant
Accept: application/ld+json
scan_interval: 60
- platform: template
sensors:
nws_alert_count_filtered:
friendly_name: NWS Alert Count
entity_id: sensor.nws_alert_count_raw
value_template: >
{% if is_state('sensor.nws_alert_count_raw', 'unavailable') or is_state('sensor.nws_alert_count_raw', 'unknown') %}
{{ states.sensor.nws_alert_count_filtered.state }}
{% else %}
{{ states.sensor.nws_alert_count_raw.state }}
{% endif %}
- platform: rest
resource: https://api.weather.gov/alerts/active?zone=INZ009,INC033
name: NWS Alert Event Raw
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
- platform: template
sensors:
nws_alert_event_filtered:
friendly_name: NWS Alert Event
entity_id: sensor.nws_alert_event_raw
value_template: >
{% if is_state('sensor.nws_alert_event_raw', 'unavailable') or is_state('sensor.nws_alert_event_raw', 'unknown') %}
{{ states.sensor.nws_alert_event_filtered.state }}
{% else %}
{{ states.sensor.nws_alert_event_raw.state }}
{% endif %}
- platform: template
sensors:
nws_alert_event_2:
friendly_name: NWS Alert Event 2
entity_id: sensor.nws_alert_event_raw
value_template: >
{% if state_attr('sensor.nws_alert_event_raw', 'features')[1] is defined %}
{{ state_attr('sensor.nws_alert_event_raw', 'features')[1]['properties'].event }}
{% else %}
None
{% endif %}
nws_alert_event_3:
friendly_name: NWS Alert Event 3
entity_id: sensor.nws_alert_event_raw
value_template: >
{% if state_attr('sensor.nws_alert_event_raw', 'features')[2] is defined %}
{{ state_attr('sensor.nws_alert_event_raw', 'features')[2]['properties'].event }}
{% else %}
None
{% endif %}
---------------------------------------------
automation:
- alias: 'NWS Weather Alert Pop Up Control'
trigger:
platform: state
entity_id: sensor.nws_alert_count_filtered
condition:
- condition: template
value_template: '{{states.sensor.nws_alert_count_filtered.state | int > 0}}'
action:
service: script.nws_popup_on_wx_alert
- alias: NWS Notification Weather Alert
trigger:
platform: state
entity_id: sensor.nws_alert_count_filtered
condition:
- condition: template
value_template: '{{states.sensor.nws_alert_count_filtered.state | int > 0}}'
action:
- service: notify.pushbullet_notify
data:
message: "NWS: {{ states.sensor.nws_alert_event_filtered.state }}"
- alias: NWS Announcement Weather Alert
initial_state: 'on'
trigger:
- platform: state
entity_id: sensor.nws_alert_count_filtered
condition:
condition: and
conditions:
- condition: template
value_template: '{{states.sensor.nws_alert_count_filtered.state | int > 0}}'
- condition: template
value_template: "{{ (('Severe' in states.sensor.nws_alert_event_filtered.state) or ('Tornado' in states.sensor.nws_alert_event_filtered.state)) and 'Warning' in states.sensor.nws_alert_event_filtered.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_filtered.state }} for your area.
It expires at {{ as_timestamp(state_attr('sensor.nws_alert_event_raw', '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_filtered.state }} for your area.
It expires at {{ as_timestamp(state_attr('sensor.nws_alert_event_raw', 'features')[0].properties.expires)| timestamp_custom('%-I:%M %p on %-m-%-d-%Y') }}."
- alias: NWS Announcement Weather Alert 2
initial_state: 'on'
trigger:
- platform: state
entity_id: sensor.nws_alert_count_filtered
condition:
condition: and
conditions:
- condition: template
value_template: '{{states.sensor.nws_alert_count_filtered.state | int > 1}}'
- condition: template
value_template: "{{ (('Severe' in states.sensor.nws_alert_event_2.state) or ('Tornado' in states.sensor.nws_alert_event_2.state)) and 'Warning' in states.sensor.nws_alert_event_2.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!,,,A second severe weather alert has been issued. The National
Weather Service has issued a
{{ states.sensor.nws_alert_event_2.state }} for your area.
It expires at {{ as_timestamp(state_attr('sensor.nws_alert_event_raw', 'features')[1].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!,,,A second severe weather alert has been issued. The National Weather Service has issued a
{{ states.sensor.nws_alert_event_2.state }} for your area.
It expires at {{ as_timestamp(state_attr('sensor.nws_alert_event_raw', 'features')[1].properties.expires)| timestamp_custom('%-I:%M %p on %-m-%-d-%Y') }}."
- alias: NWS Announcement Weather Alert for MBR
initial_state: 'on'
trigger:
- platform: state
entity_id: sensor.nws_alert_count_filtered
condition:
condition: and
conditions:
- condition: template
value_template: '{{states.sensor.nws_alert_count_filtered.state | int > 0}}'
- condition: template
value_template: "{{ 'Tornado' in states.sensor.nws_alert_event_filtered.state and 'Warning' in states.sensor.nws_alert_event_filtered.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.master_bedroom_dot
message: "Warning!,,,Warning!,,,The National Weather Service has issued a
{{ states.sensor.nws_alert_event_filtered.state }} for your area.
It expires at {{ as_timestamp(state_attr('sensor.nws_alert_event_raw', '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.master_bedroom_dot
message: "Warning!,,,Warning!,,,The National Weather Service has issued a
{{ states.sensor.nws_alert_event_filtered.state }} for your area.
It expires at {{ as_timestamp(state_attr('sensor.nws_alert_event_raw', 'features')[0].properties.expires)| timestamp_custom('%-I:%M %p on %-m-%-d-%Y') }}."
- alias: NWS Announcement Weather Alert 2 for MBR
initial_state: 'on'
trigger:
- platform: state
entity_id: sensor.nws_alert_count_filtered
condition:
condition: and
conditions:
- condition: template
value_template: '{{states.sensor.nws_alert_count_filtered.state | int > 1}}'
- condition: template
value_template: "{{ 'Tornado' in states.sensor.nws_alert_event_2.state and 'Warning' in states.sensor.nws_alert_event_2.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.master_bedroom_dot
message: "Warning!,,,Warning!,,,A second severe weather alert has been issued. The National Weather Service has issued a
{{ states.sensor.nws_alert_event_2.state }} for your area.
It expires at {{ as_timestamp(state_attr('sensor.nws_alert_event_raw', 'features')[1].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.master_bedroom_dot
message: "Warning!,,,Warning!,,,A second severe weather alert has been issued. The National Weather Service has issued a
{{ states.sensor.nws_alert_event_2.state }} for your area.
It expires at {{ as_timestamp(state_attr('sensor.nws_alert_event_raw', 'features')[1].properties.expires)| timestamp_custom('%-I:%M %p on %-m-%-d-%Y') }}."
---------------------------------------------------------------
script:
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_filtered.state != 'None' %}
persistent_notification.create
{% endif %}
data_template:
notification_id: "nwswxalert"
message: "{{ state_attr('sensor.nws_alert_event_raw', 'features')[0].properties.description }}"
title: '{{ states.sensor.nws_alert_event_raw.state }}'
-------------------------------------------------------------
I created two announcements so that a minor event won't wake you nup in the middle of the night.
You’ll notice I repeat the announcement twice in case the first announcement doesn’t wake you up.
And I haven’t worked out the volume setting part yet. If anyone has any working solutions feel free to let me know.
The script above generates a persistent notification pop-up.
@Thepyropc
Copy link

Im needing some help i keep getting an error on mine and im not really sure how to solve the problem.

`#NWS Alerts Attempt 1

  • platform: rest
    resource: https://api.weather.gov/alerts/active/count
    name: NWS Alert Count
    value_template: >
    {% if value_json.zones. ILC043 is defined %}
    {{ value_json.zones. ILC043 }}
    {% else %}
    0
    {% endif %}
    headers:
    User-Agent: Homeassistant
    Accept: application/ld+json
    scan_interval: 60

  • platform: template
    sensors:
    nws_alert_count_filtered:
    friendly_name: NWS Alert Count
    entity_id: sensor.nws_alert_count_raw
    value_template: >
    {% if is_state('sensor.nws_alert_count_raw', 'unavailable') or is_state('sensor.nws_alert_count_raw', 'unknown') %}
    {{ states.sensor.nws_alert_count_filtered.state }}
    {% else %}
    {{ states.sensor.nws_alert_count_raw.state }}
    {% endif %}

  • platform: rest
    resource: https://api.weather.gov/alerts/active?zone=INZ009,INC033
    name: NWS Alert Event Raw
    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
  • platform: template
    sensors:
    nws_alert_event_filtered:
    friendly_name: NWS Alert Event
    entity_id: sensor.nws_alert_event_raw
    value_template: >
    {% if is_state('sensor.nws_alert_event_raw', 'unavailable') or is_state('sensor.nws_alert_event_raw', 'unknown') %}
    {{ states.sensor.nws_alert_event_filtered.state }}
    {% else %}
    {{ states.sensor.nws_alert_event_raw.state }}
    {% endif %}

  • platform: template
    sensors:
    nws_a;ert_event_2:
    friendly_name: NWS Alert Event 2
    entity_id: sensor.nws_alert_event_raw
    value_template: >
    {% if state_attr('sensor.nws_alert_event_raw', 'features')[1] is defined %}
    {{ state_attr('sensor.nws_alert_event_raw', 'features')[1]['properties'].event }}
    {% else %}
    None
    {% endif %}
    nws_alert_event_3:
    friendly_name: NWS Alert Event 3
    entity_id: sensor.nws_alert_event_raw
    value_template: >
    {% if state_attr('sensor.nws_alert_event_raw', 'features')[2] is defined %}
    {{ state_attr('sensor.nws_alert_event_raw', 'features')[2]['properties'].event }}
    {% else %}
    None
    {% endif %}
    `

any thoughts on what to do i keep getting the error ?String does not match the pattern of DEPRECATED*"
im getting this error every time i see
entity_id: sensor.nws_alert_event_raw

ignore the indent errors if there not working (there not showing correctly in my preview but they are right in my yaml

@Thepyropc
Copy link

nws_a;ert_event_2
ignore that line found that and fixed it but i still have other errors like stated before

@finity69x2
Copy link
Author

remove the "entiity_id:" lines. That option has been deprecated for template sensors.

@Thepyropc
Copy link

remove the "entiity_id:" lines. That option has been deprecated for template sensors.

Ok did that and got rid of my errors not sure why it worked this time. because i did that before and it gave me more errors. but looks like in theory it should be working now.

thank you

will repost if i cant get the alerts on Alexa to work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment