- alias: NWS Announce Weather Alert
id: announce_weather_alert
trigger:
- platform: state
entity_id: sensor.nws_alerts_gps_2
action:
- repeat:
sequence:
- variables:
alert: "{{ repeat.item }}"
- if:
- condition: template
value_template: "{{ ('Tornado' in alert.Event) and (alert.Type == 'Alert') }}"
then:
sequence:
- metadata: {}
data:
priority: True
volume: 0.9
this_message: >-
The National Weather Service has {{ iif(alert.Type ==
'Alert', 'issued a', 'updated the') }} {{alert.Event}}
for our area. {% if(alert.Event != alert.Headline) %}
{{ alert.Headline }}
{% else %}
It will expire in {{alert.Expires | as_datetime() | time_until(2)}}. {{ (alert.Instruction or '') | replace('\n', ' ') }}
{% endif %}
action: script.house_announcement
else:
- metadata: {}
data:
volume: 0.9
this_message: >-
The National Weather Service has {{ iif(alert.Type == 'Alert',
'issued a', 'updated the') }} {{alert.Event}} for our area. {%
if(alert.Event != alert.Headline) %}
{{ alert.Headline }}
{% else %}
It will expire in {{alert.Expires | as_datetime() | time_until(2)}}. {{ (alert.Instruction or '') | replace('\n', ' ') }}
{% endif %}
action: script.house_announcement
for_each: >-
{{ trigger.to_state.attributes.Alerts | reject('in',
trigger.from_state.attributes.Alerts) | list() |
sort(attribute='Expires') }}
I filter the word âtornadoâ and allow it to override my announcer script to play the alert even if ânight modeâ/âsilent modeâ is enabled (suppresses TTS notifications after 9pm local time).
Edit: This is modified from the scripts posted here:
there is an open issue on the github repo that has suggested alternative automations. Iâve been working on tweaking those a bit and Iâm pretty sure Iâm pretty much there.
Iâll be adding those to my repo shortly but here are the ones Iâm using now for testing based on those:
automation:
- alias: NWS Test - Mobile App Notifications
id: nws_test_mobile_app_notifications
description: ""
trigger:
- platform: state
entity_id: sensor.nws_alerts_test
condition:
- condition: numeric_state
entity_id: sensor.nws_alerts_test
above: 0
action:
- repeat:
sequence:
- service: script.turn_on
continue_on_error: true
entity_id: script.notification_pushover_message
data:
variables:
target: my_phone
message: "NWS Test: {{ repeat.item.Event }}"
sound: echo
- delay:
seconds: 5
for_each: "{{ trigger.to_state.attributes.Alerts | reject('in', trigger.from_state.attributes.Alerts) | sort(attribute='ID') | list() }}"
mode: queued
initial_state: "on"
max: 10
#################################################################
- alias: NWS Test - Persistent Notifications
id: nws_test_persistent_notifications
description: ""
trigger:
- platform: state
entity_id: sensor.nws_alerts_test
condition:
- condition: numeric_state
entity_id: sensor.nws_alerts_test
above: 0
action:
- repeat:
sequence:
- service: script.nws_alerts_persistent_notification
data:
notification_id: "NWS_{{ repeat.item.ID }}"
title: "NWS Test: {{ repeat.item.Event }}"
message: "{{ repeat.item.Description }}"
- delay:
seconds: 5
for_each: "{{ trigger.to_state.attributes.Alerts | reject('in', trigger.from_state.attributes.Alerts) | sort(attribute='ID') | list() }}"
mode: queued
initial_state: "on"
max: 10
######################################################
- alias: NWS Test - TTS Announcements
id: nws_test_tts_announcements
description: ""
trigger:
- platform: state
entity_id: sensor.nws_alerts_test
condition:
- condition: numeric_state
entity_id: sensor.nws_alerts_test
above: 0
action:
- repeat:
sequence:
- variables:
alert: "{{ repeat.item }}"
- if:
- condition: template
value_template: "{{ ('Tornado Warning' in alert.Event) and (alert.Type == 'Alert') }}"
then:
- service: script.turn_on
entity_id: script.nws_alerts_test_announce
data:
variables:
message: This is only a test. The National Weather Service Has issued a tornado warning for our area! this is only a test.
- delay:
minutes: 1
for_each: "{{ trigger.to_state.attributes.Alerts | reject('in', trigger.from_state.attributes.Alerts) | list() | sort(attribute='ID') }}"
- repeat:
sequence:
- variables:
alert: "{{ repeat.item }}"
- if:
- condition: template
value_template: "{{ ('Severe Thunderstorm Warning' in alert.Event) and (alert.Type == 'Alert') }}"
then:
- service: script.turn_on
entity_id: script.nws_alerts_test_announce
data:
variables:
message: This is only a test. The National Weather Service Has issued a severe thunderstorm warning for our area! this is only a test.
- delay:
minutes: 1
for_each: "{{ trigger.to_state.attributes.Alerts | reject('in', trigger.from_state.attributes.Alerts) | list() | sort(attribute='ID') }}"
mode: queued
initial_state: "on"
max: 10
Of course youâll need to modify for your needs.
And in my production setup I use two different scripts for the announcements. one for tornados that announces everywhere and one for everything else that doesnât announce in the bedrooms.
title: Alerts
type: vertical-stack
cards:
- type: conditional
conditions:
- entity: sensor.nws_alerts
state_not: '0'
card:
type: markdown
content: |
{% set num_alerts = states('sensor.nws_alerts_2') | int %}
{% for alert_num in range(num_alerts) %}
{% set alert = state_attr('sensor.nws_alerts_2', 'Alerts')[alert_num].Event %}
{% if 'Warning' in alert or 'Watch' in alert %}
---
## {{ state_attr('sensor.nws_alerts_2', 'Alerts')[alert_num].Event }}
{{ state_attr('sensor.nws_alerts_2', 'Alerts')[alert_num].Description }}
{% endif %}
{% endfor %}
You could also trigger the display of the alert on other things, like the Severity being âExtremeâ or âSevereâ for example. Iâm still working on the logic for notifications.
Perhaps a better phrasing would be âstill working through the notifications, because Iâm figuring out notifications in general, and what I even wantâ. The persistent notification example seems good, Iâm just trying to wrap my head around it!
I have a FR in an issue from @GreyBandit which is also related to a request from @Sekonicpr above about the âendsâ vs âexpiresâ data from the API.
The request is to add âendsâ.
I can actually see why the âendsâ might be better data to provide than the âexpiresâ data. TBH, Iâm not really sure why âexpiresâ is there but I think someone asked for that data a long time back.
I donât use âexpiresâ in any way personally so I have no strong opinion either way.
I resist the urge to make anything break but I also resist the urge to just add every field from the API to the attributes since it makes the sensor harder to read for us mere humans.
So the question Iâll ask here is does anyone actually use the âexpiresâ in automations or rely on that data that using âendsâ wouldnât actually be a better alternative?
Iâm suggesting replacing âexpiresâ with âendsâ instead of having both.
Seeing as how elections are in the air in the US iâll (hopefully) solve this question with a poll.
âendsâ vs âexpiresâ
Keep âexpiresâ only
remove âexpiresâ and add âendsâ only
keep âexpiresâ and still add âendsâ
0voters
the poll closes in 2 weeks so Iâll make the decision then.
but for full disclosure the option to have both will need to have a strong mandate to be implemented.
So Iâm a bit of a purist. The EAS message that comes through the SAME header format only has the fields for start time and duration. The duration is not a date/time it ends at, just how long after the message is issued that it will auto expire and no longer be active. You could use the issue date/time and add the duration to it to get expiration date/time, yes. And that information may be available over the API (which is different than the EAS message). So that said, personally Iâd choose neither, but if I had to it would be just expires.
Hello! I think we need both, or we need some logic ⌠what I have found is that:
(1) When populated, âendsâ is the best indicator of when the alert is no longer applicable. Looking at other web sites and how they present alerts for my area, this is always the value that is shown (when it is not null).
(2) However, âendsâ is sometimes null. In those cases, the value for âexpiresâ is the one that seems to communicate when the alert is no longer applicable.
I manually changed my local copy of your integration several months ago to add âendsâ. I have also included this ⌠which is admittedly a bit of a hack but has resulted in my alert end dates/times always matching what I see displayed elsewhere:
if tmp_dict["Ends"] == None:
tmp_dict["Ends"] = alert["properties"]["expires"]
Realizing this is a hack, I donât expect you to implement the same. However, I would request both attributes be available so we can build the necessary logic into our dashboards. Thank you!
Iâve updated the example code in the ânws_alerts_package.yamlâ at the repo using the above code. Itâs way cleaner and doesnât use any external dependencies.
thanks again to @T3chArmy for the suggestions. the ones I put in the repo are only minimally modified from their suggestions.
Pardon my ignorance, but the package file seems to be missing the Persistent Notifications script. Or am I looking in the wrong place? I keep getting an error that tells me that script.nws_alerts_persistent_notification is not a valid action.
This was my previously working automation. This would sent 1 notification for each new alert.
alias: SMS - Weather Alert - NWS Alert
description: ""
trigger:
- platform: numeric_state
entity_id: sensor.nws_alerts
above: "0"
condition: []
action:
- data:
message: >-
NWS Alert for XXXX
{{state_attr('sensor.nws_alerts', 'title') }}
Event Severity - {{state_attr('sensor.nws_alerts', 'event_severity') }}
Event Status - {{state_attr('sensor.nws_alerts', 'event_status') }}
Source - {{state_attr('sensor.nws_alerts', 'attribution') }}
Description - {{state_attr('sensor.nws_alerts', 'display_desc') }}
This alert was sent from the XXXX Home Assistant. Save
this number to your contacts for future reference.
action: notify.sms_alerts_small_group
mode: single
I think you can use the same/similar code for the mobile app notification automation posted above or at the repo in the new version package.
this hasnât been tested but might get you what you want. Except it wonât give you the attribution but since that is always a static value (itâs always âData provided by Weather.govâ) so adding it as a template doesnât really gain you anything. Just hard code it if you want it which is what Iâll do in the example:
automation:
- alias: NWS - Mobile App Notifications
id: nws_mobile_app_notifications
trigger:
- platform: state
entity_id: sensor.nws_alerts
condition:
- condition: numeric_state
entity_id: sensor.nws_alerts
above: 0
action:
- repeat:
sequence:
- data:
message: >-
NWS Alert for XXXX
{{ repeat.item.Event }}
Event Severity - {{ repeat.item.Severity }}
Event Status - {{ repeat.item.Status }}
Source - Data provided by Weather.gov
Description - {{ repeat.item.Description }}
This alert was sent from the XXXX Home Assistant. Save this number to your contacts for future reference.
action: notify.sms_alerts_small_group
- delay:
seconds: 5
for_each: "{{ trigger.to_state.attributes.Alerts | reject('in', trigger.from_state.attributes.Alerts) | sort(attribute='ID') | list() }}"
mode: queued
initial_state: "on"
max: 10