I’m trying to determine when an NWS Alert has ended so I can set turn off a Boolean switch. Can someone tell me if this trigger with an “Alerts” state below 1 means the alert has ended?
And in addition to the above even if you remove the attribute entry that will still only tell if ALL the alerts have ended, not any specific alert.
That sensor state shows the total number of alerts currently active. so an alert could end and that number might still be above 0 if it started out with more than one alert originally.
And it would be possible for one alert to have ended and another alert issued at the same time and the state would still show more than 0 (basically going from a state 1 to another state of 1 with no 0 in between).
Thanks. It’s certainly not black and white! I still think it would work then in most cases if I omit the attribute as Didgeridrew suggested. For example last night there was a thunderstorm moving through my area and I received 5 alerts over the course of about 6 hours. I’m just trying to reduce the alerts. Except for a severity of extreme or severe, those I don’t block at all, which turn several lights in my house red, and plays the alert through my Sonos speakers.
Another factor complicating matters is that I monitor 6 zone ID’s, not just my county. So there could be different alerts from different zones.
But regardless of the number of zones I monitor, I have a “NWS Alert Switch” that I’m checking the state, and only sends an alert of the Boolean has been on for 5 hours. But after all is said and done I want to turn the switch off, which is what prompted the question regding the end of the alert.
I guess I still don’t fully understand what you are trying to achieve.
What is the purpose of the boolean?
What turns the boolean on?
What needs to happen to turn the boolean off?
If you are looking at a specific alert and want to know if that no longer exists then that data exists in the “alerts” attribute list of alerts. each alert has 2 ways of identifying the alert - one is the “id:” value of the alert and the other is the “url:”
Here is my completed NWS Alerts automation. The purpose of the Boolean is so I don’t receive another notification for at least 5 hours IF the alert is Moderate, Minor, or Unknown and the NWS Alert Switch was off. If the severity is Severe or Extreme, the Boolean is not used, and I get all of those alerts, lights are turned on, and notifications are played.
alias: NWS Alert
description: >-
Only turn lights red and play audio alert when severity is Extreme or Severe.
These alerts are not subject to the 5 hour delay caused by NWS Alrert Switch
as are Moderate, Minor, and Unknown.
triggers:
- entity_id:
- sensor.nws_alerts_alerts
trigger: state
to: null
- trigger: numeric_state
entity_id:
- sensor.nws_alerts_alerts
below: 1
enabled: true
id: Alert has ended
conditions: []
actions:
- choose:
- conditions:
- condition: template
value_template: |-
{{ state_attr('sensor.nws_alerts_alerts','Alerts')
| selectattr('Severity', 'in', ['Severe','Extreme'])
| list | count > 0 }}
sequence:
- action: script.notification_text_message_to_pixel_8
metadata: {}
data:
message: >-
⚠️{% set alerts =
state_attr('sensor.nws_alerts_alerts','Alerts') %} {{
alerts[0].Headline }}
Severity: {{ alerts[0].Severity }}
Area: {{ alerts[0].AreasAffected }}
enabled: true
- action: script.notification_text_message_to_galaxy_s9
metadata: {}
data:
message: >-
{% set alerts = state_attr('sensor.nws_alerts_alerts','Alerts')
%} {{ alerts[0].Headline }}
Severity: {{ alerts[0].Severity }}
Area: {{ alerts[0].AreasAffected }}
enabled: false
- action: light.turn_on
metadata: {}
data:
rgb_color:
- 255
- 0
- 0
brightness_pct: 100
target:
entity_id:
- light.office_lamp_two
- light.smart_multicolor_bulb
- light.master_bedroom_lamp_2
enabled: true
- action: notify.send_message
metadata: {}
data:
message: >-
The following severe weather alert has been issued by the
National Weather Service.
target:
entity_id:
- notify.echo_dot_living_room_announce
- notify.echo_dot_master_bedroom_announce
- notify.echo_dot_office_announce
- notify.echo_dot_garage_announce
enabled: false
- action: notify.send_message
metadata: {}
data:
message: >-
{% set alerts = state_attr('sensor.nws_alerts_alerts','Alerts')
%} {{ alerts[0].Headline }}
target:
entity_id:
- notify.echo_dot_living_room_announce
- notify.echo_dot_master_bedroom_announce
- notify.echo_dot_office_announce
- notify.echo_dot_garage_announce
enabled: false
- conditions:
- condition: template
value_template: |-
{{ state_attr('sensor.nws_alerts_alerts','Alerts')
| selectattr('Severity', 'in', ['Moderate','Minor','Unknown'])
| list | count > 0 }}
- condition: state
entity_id: input_boolean.nws_weather_alert_switch
state:
- "off"
sequence:
- action: script.notification_text_message_to_pixel_8
metadata: {}
data:
message: >-
⚠️{% set alerts =
state_attr('sensor.nws_alerts_alerts','Alerts') %} {{
alerts[0].Headline }}
Severity: {{ alerts[0].Severity }}
Area: {{ alerts[0].AreasAffected }}
enabled: true
- action: input_boolean.turn_on
metadata: {}
target:
entity_id: input_boolean.nws_weather_alert_switch
data: {}
- conditions:
- condition: template
value_template: |-
{{ state_attr('sensor.nws_alerts_alerts','Alerts')
| selectattr('Severity', 'in', ['Moderate','Minor','Unknown'])
| list | count > 0 }}
- condition: state
entity_id: input_boolean.nws_weather_alert_switch
state:
- "on"
for:
hours: 5
minutes: 0
seconds: 0
enabled: true
sequence:
- action: script.notification_text_message_to_pixel_8
metadata: {}
data:
message: >-
⚠️{% set alerts =
state_attr('sensor.nws_alerts_alerts','Alerts') %} {{
alerts[0].Headline }}
Severity: {{ alerts[0].Severity }}
Area: {{ alerts[0].AreasAffected }}
enabled: true
- conditions:
- condition: trigger
id:
- Alert has ended
sequence:
- action: script.notification_text_message_to_pixel_8
metadata: {}
data:
message: The NWS alert has ended, turning off NWS Alert Switch.
enabled: true
- action: input_boolean.turn_off
metadata: {}
target:
entity_id: input_boolean.nws_weather_alert_switch
data: {}
mode: single
variables:
alert: "{{ state_attr('sensor.nws_alerts_alerts','headline') }}"
description: "{{ state_attr('sensor.nws_alerts_alerts','description') }}"
id: "{{ state_attr('sensor.nws_alerts_alerts','id') }}"
I see what you are trying to do and the automation above will do what you want (mostly).
but be aware of a one thing…
the “alert has ended” trigger isn’t really a “THE alert has ended”. it’s really a “ALL alerts have ended”. If there is an alert that lasts for days (I’ve seen a couple) then the boolean will never be reset during that time.
I would probably have another automation that turns off the boolean after it’s been on for 5 hours.
I still want to receive alerts after 5 hours. Just not inundated with them. I had a thunderstorm alert in my area a couple of weeks ago and got 7 alerts in about 10 hours.
I did a test of an active zone ID and it did last for about 2 1/2 days. But then at the end the Boolean was turned off which is what I wanted.
Since the state trigger will fire at the basically the same time as the numeric state trigger, and the mode is set to single you may find that the outcome is a bit unreliable. It would be better to remove the Numeric state trigger, then move the “Alert has ended” action to the first option of the choose and use a Numeric State condition instead of a Trigger condition.
The first option of the choose statement only handles “Severe” and “Extreme” alerts. So the “Alert has ended” action it would not execute if the alerts were Moderate, Minor, or Unknown. If I understand you correctly.
I’m not sure how you had your original alert notification set up but you should only get one notification for each alert. Again that’s as long as you have the notifications set up correctly to exclude any already existing alerts.
I don’t know if you have looked at my github in the packages section or not but in there it has working examples of notifications that work the way I describe above.