From b7749e578c83737f3bc63afdd6d17ebd7f36fd64 Mon Sep 17 00:00:00 2001 From: finity69x2 <32221243+finity69x2@users.noreply.github.com> Date: Sat, 7 Sep 2024 09:28:13 -0400 Subject: [PATCH] Create nws_alerts_package_old_version.yaml this is the older version of the package. it relies on external dependencies and is overly complicated. I recommend you use the new version --- packages/nws_alerts_package_old_version.yaml | 803 +++++++++++++++++++ 1 file changed, 803 insertions(+) create mode 100644 packages/nws_alerts_package_old_version.yaml diff --git a/packages/nws_alerts_package_old_version.yaml b/packages/nws_alerts_package_old_version.yaml new file mode 100644 index 0000000..bc05cdc --- /dev/null +++ b/packages/nws_alerts_package_old_version.yaml @@ -0,0 +1,803 @@ +################################################################################# +# +# This is the package for NWS ALERTS USING THE CUSTOM COMPONENT +# +################################################################################# + +## For testing the API + +# https://api.weather.gov/alerts/active/count + +# https://api.weather.gov/alerts/active?zone=INZ009,INC033 + +######################## BINARY SENSOR ######################################### + +binary_sensor: + - platform: template + sensors: + nws_alerts_are_active: + friendly_name: NWS Alerts Are Active + value_template: > + {{ states('sensor.nws_alerts') | int(0) > 0 }} + icon_template: >- + {% if states('sensor.nws_alerts') | int(0) > 0 %} + mdi:weather-lightning + {% else %} + mdi:weather-sunny + {% endif %} + + +############################## VARIABLE ##################################### + + +## this uses the hass_variable custom integration. +## you can install thru HACS or manually from https://github.com/Wibias/hass-variables +## This custom integration isn't needed for the NWS Alerts integration to function +## but I use it for my automations so that I can keep track of which alerts have been notified and/or announced + +variable: + nws_alerts_event_ids: + value: 'none' + restore: false + nws_alerts_announced_ids: + value: 'none' + restore: false + +######################## AUTOMATION ########################################### + +automation: + + - alias: NWS Alerts Update Event ID History + id: nws_alerts_update_event_id_history + initial_state: 'on' + trigger: + - platform: state + entity_id: sensor.nws_alerts + action: + - delay: + minutes: 5 + - repeat: + sequence: + - delay: + seconds: 5 + - condition: template + value_template: > + {% set ns = namespace(ids=[]) %} + {% for x in range(0,states('sensor.nws_alerts')|int ) %} + {% set id = state_attr('sensor.nws_alerts', 'Alerts')[x].ID %} + {% set ns.ids = ns.ids + [id] %} + {% endfor -%} + {% set current_id_list = set(ns.ids) %} + {% set history_id_list = set( (states.sensor.nws_alerts_event_ids.attributes.values()| reject('match', 'nws_alerts_event_ids') | reject('match', 'None') | reject('match', 'unknown'))|list ) %} + {{ (current_id_list.difference(history_id_list)|list) | count > 0 }} + - service: variable.update_sensor + target: + entity_id: sensor.nws_alerts_event_ids + data: + value: > + {% set ns = namespace(ids=[]) %} + {% for x in range(0,states('sensor.nws_alerts')|int ) %} + {% set id = state_attr('sensor.nws_alerts', 'Alerts')[x].ID %} + {% set ns.ids = ns.ids + [id] %} + {% endfor -%} + {% set current_id_list = set(ns.ids) %} + {% set history_id_list = set( (states.sensor.nws_alerts_event_ids.attributes.values()| reject('match', 'nws_alerts_event_ids') | reject('match', 'None') | reject('match', 'unknown'))|list ) %} + {% if (current_id_list.difference(history_id_list)|list)[0] is defined %} + {{ (current_id_list.difference(history_id_list)|list)[0] }} + {% endif %} + - service: variable.update_sensor + target: + entity_id: sensor.nws_alerts_event_ids + data: + attributes: + history_1: > + {% set ns = namespace(ids=[]) %} + {% for x in range(0,states('sensor.nws_alerts')|int ) %} + {% set id = state_attr('sensor.nws_alerts', 'Alerts')[x].ID %} + {% set ns.ids = ns.ids + [id] %} + {% endfor -%} + {% set current_id_list = set(ns.ids) %} + {% set history_id_list = set( (states.sensor.nws_alerts_event_ids.attributes.values()| reject('match', 'nws_alerts_event_ids') | reject('match', 'None') | reject('match', 'unknown'))|list ) %} + {% if (current_id_list.difference(history_id_list)|list)[0] is defined %} + {{ (current_id_list.difference(history_id_list)|list)[0] }} + {% endif %} + history_2: "{{ state_attr('sensor.nws_alerts_event_ids', 'history_1') }}" + history_3: "{{ state_attr('sensor.nws_alerts_event_ids', 'history_2') }}" + history_4: "{{ state_attr('sensor.nws_alerts_event_ids', 'history_3') }}" + history_5: "{{ state_attr('sensor.nws_alerts_event_ids', 'history_4') }}" + history_6: "{{ state_attr('sensor.nws_alerts_event_ids', 'history_5') }}" + history_7: "{{ state_attr('sensor.nws_alerts_event_ids', 'history_6') }}" + history_8: "{{ state_attr('sensor.nws_alerts_event_ids', 'history_7') }}" + history_9: "{{ state_attr('sensor.nws_alerts_event_ids', 'history_8') }}" + history_10: "{{ state_attr('sensor.nws_alerts_event_ids', 'history_9') }}" + history_11: "{{ state_attr('sensor.nws_alerts_event_ids', 'history_10') }}" + history_12: "{{ state_attr('sensor.nws_alerts_event_ids', 'history_11') }}" + history_13: "{{ state_attr('sensor.nws_alerts_event_ids', 'history_12') }}" + history_14: "{{ state_attr('sensor.nws_alerts_event_ids', 'history_13') }}" + history_15: "{{ state_attr('sensor.nws_alerts_event_ids', 'history_14') }}" + until: + - condition: template + value_template: > + {% set ns = namespace(ids=[]) %} + {% for x in range(0,states('sensor.nws_alerts')|int ) %} + {% set id = state_attr('sensor.nws_alerts', 'Alerts')[x].ID %} + {% set ns.ids = ns.ids + [id] %} + {% endfor -%} + {% set current_id_list = set(ns.ids) %} + {% set history_id_list = set( (states.sensor.nws_alerts_event_ids.attributes.values()| reject('match', 'nws_alerts_event_ids') | reject('match', 'None') | reject('match', 'unknown'))|list ) %} + {{ (current_id_list.difference(history_id_list)|list) | count == 0}} + + ######################## PERSISTENT NOTIFICATIONS ########################################### + + ## This will send up to 3 persistent notifications for alerts. + ## If there are more than 3 alerts there will be a fourth notification to check the NWS for further information. + + - alias: 'NWS Alerts Create Persistent Notifications' + id: nws_alerts_create_persistent_notifications + initial_state: 'on' + trigger: + - platform: state + entity_id: sensor.nws_alerts + condition: + - "{{ states('sensor.nws_alerts') | int > 0 }}" + action: + - choose: + - conditions: + - "{{ states('sensor.nws_alerts') | int >= 3 }}" + sequence: + - if: + - "{{ (state_attr('sensor.nws_alerts', 'Alerts')[0].ID not in states.sensor.nws_alerts_event_ids.attributes.values()|list) and (state_attr('sensor.nws_alerts', 'Alerts')[0].ID != states('sensor.nws_alerts_event_ids')) }}" + then: + - service: script.nws_alerts_persistent_notification + data: + notification_id: "nwswxalert0" + title: > + {{ state_attr('sensor.nws_alerts', 'Alerts')[0].Event }} + message: > + {{ state_attr('sensor.nws_alerts', 'Alerts')[0].Description }} + - delay: + seconds: 2 + - if: + - "{{ (state_attr('sensor.nws_alerts', 'Alerts')[1].ID not in states.sensor.nws_alerts_event_ids.attributes.values()|list) and (state_attr('sensor.nws_alerts', 'Alerts')[1].ID != states('sensor.nws_alerts_event_ids')) }}" + then: + - service: script.nws_alerts_persistent_notification + data: + notification_id: "nwswxalert1" + title: > + {{ state_attr('sensor.nws_alerts', 'Alerts')[1].Event }} + message: > + {{ state_attr('sensor.nws_alerts', 'Alerts')[1].Description }} + - delay: + seconds: 2 + - if: + - "{{ (state_attr('sensor.nws_alerts', 'Alerts')[2].ID not in states.sensor.nws_alerts_event_ids.attributes.values()|list) and (state_attr('sensor.nws_alerts', 'Alerts')[2].ID != states('sensor.nws_alerts_event_ids')) }}" + then: + - service: script.nws_alerts_persistent_notification + data: + notification_id: "nwswxalert2" + title: > + {{ state_attr('sensor.nws_alerts', 'Alerts')[2].Event }} + message: > + {{ state_attr('sensor.nws_alerts', 'Alerts')[2].Description }} + - conditions: + - "{{ states('sensor.nws_alerts') | int == 2 }}" + sequence: + - if: + - "{{ (state_attr('sensor.nws_alerts', 'Alerts')[0].ID not in states.sensor.nws_alerts_event_ids.attributes.values()|list) and (state_attr('sensor.nws_alerts', 'Alerts')[0].ID != states('sensor.nws_alerts_event_ids')) }}" + then: + - service: script.nws_alerts_persistent_notification + data: + notification_id: "nwswxalert0" + title: > + {{ state_attr('sensor.nws_alerts', 'Alerts')[0].Event }} + message: > + {{ state_attr('sensor.nws_alerts', 'Alerts')[0].Description }} + - delay: + seconds: 2 + - if: + - "{{ (state_attr('sensor.nws_alerts', 'Alerts')[1].ID not in states.sensor.nws_alerts_event_ids.attributes.values()|list) and (state_attr('sensor.nws_alerts', 'Alerts')[1].ID != states('sensor.nws_alerts_event_ids')) }}" + then: + - service: script.nws_alerts_persistent_notification + data: + notification_id: "nwswxalert1" + title: > + {{ state_attr('sensor.nws_alerts', 'Alerts')[1].Event }} + message: > + {{ state_attr('sensor.nws_alerts', 'Alerts')[1].Description }} + - conditions: + - "{{ states('sensor.nws_alerts') | int == 1 }}" + sequence: + - if: + - "{{ (state_attr('sensor.nws_alerts', 'Alerts')[0].ID not in states.sensor.nws_alerts_event_ids.attributes.values()|list) and (state_attr('sensor.nws_alerts', 'Alerts')[0].ID != states('sensor.nws_alerts_event_ids')) }}" + then: + - service: script.nws_alerts_persistent_notification + data: + notification_id: "nwswxalert0" + title: > + {{ state_attr('sensor.nws_alerts', 'Alerts')[0].Event }} + message: > + {{ state_attr('sensor.nws_alerts', 'Alerts')[0].Description }} + - delay: + seconds: 2 + - condition: template + value_template: "{{ states('sensor.nws_alerts') | int >= 4 }}" + - condition: template + value_template: > + {% set ns = namespace(ids=[]) %} + {% for x in range(0,states('sensor.nws_alerts')|int ) %} + {% set id = state_attr('sensor.nws_alerts', 'Alerts')[x].ID %} + {% set ns.ids = ns.ids + [id] %} + {% endfor -%} + {% set current_id_list = set(ns.ids) %} + {% set history_id_list = set( (states.sensor.nws_alerts_event_ids.attributes.values()| reject('match', 'nws_alerts_event_ids') | reject('match', 'None') | reject('match', 'unknown'))|list ) %} + {{ (current_id_list.difference(history_id_list)|list) | count > 0 }} + - condition: template + value_template: > + {{ (state_attr('sensor.nws_alerts', 'Alerts')[0].ID not in states.sensor.nws_alerts_event_ids.attributes.values()|list) + and (state_attr('sensor.nws_alerts', 'Alerts')[0].ID != states('sensor.nws_alerts_event_ids')) }} + - service: script.nws_alerts_persistent_notification + data: + notification_id: "nwswxalert4" + title: More Than 3 New Alerts! + message: Check your local National Weather Service for all current alerts! + + +########################## NOTIFICATIONS ########################################################################## + + ## This will send up to 3 notifications for alerts. + ## If there are more than 3 alerts there will be a fouth notification to check the NWS for further information. + + - alias: NWS Alerts Notification Weather Alert + id: nws_alerts_notification_weather_alert + initial_state: 'on' + trigger: + platform: state + entity_id: sensor.nws_alerts + condition: + - "{{states('sensor.nws_alerts') | int > 0}}" + action: + - choose: + - conditions: + - "{{ states('sensor.nws_alerts') | int >= 3 }}" + sequence: + - if: + - "{{ (state_attr('sensor.nws_alerts', 'Alerts')[0].ID not in states.sensor.nws_alerts_event_ids.attributes.values()|list) and (state_attr('sensor.nws_alerts', 'Alerts')[0].ID != states('sensor.nws_alerts_event_ids')) }}" + then: + - service: script.turn_on + continue_on_error: true + entity_id: script.notification_pushover_message + data: + variables: + target: Jeffs_phone + message: > + "NWS: {{ state_attr('sensor.nws_alerts', 'Alerts')[0].Event }}" + sound: echo + - delay: + seconds: 2 + - if: + - "{{ (state_attr('sensor.nws_alerts', 'Alerts')[1].ID not in states.sensor.nws_alerts_event_ids.attributes.values()|list) and (state_attr('sensor.nws_alerts', 'Alerts')[1].ID != states('sensor.nws_alerts_event_ids')) }}" + then: + - service: script.turn_on + continue_on_error: true + entity_id: script.notification_pushover_message + data: + variables: + target: Jeffs_phone + message: > + "NWS: {{ state_attr('sensor.nws_alerts', 'Alerts')[1].Event }}" + sound: echo + - delay: + seconds: 2 + - if: + - "{{ (state_attr('sensor.nws_alerts', 'Alerts')[2].ID not in states.sensor.nws_alerts_event_ids.attributes.values()|list) and (state_attr('sensor.nws_alerts', 'Alerts')[2].ID != states('sensor.nws_alerts_event_ids')) }}" + then: + - service: script.turn_on + continue_on_error: true + entity_id: script.notification_pushover_message + data: + variables: + target: Jeffs_phone + message: > + "NWS: {{ state_attr('sensor.nws_alerts', 'Alerts')[2].Event }}" + sound: echo + - conditions: + - "{{ states('sensor.nws_alerts') | int == 2 }}" + sequence: + - if: + - "{{ (state_attr('sensor.nws_alerts', 'Alerts')[0].ID not in states.sensor.nws_alerts_event_ids.attributes.values()|list) and (state_attr('sensor.nws_alerts', 'Alerts')[0].ID != states('sensor.nws_alerts_event_ids')) }}" + then: + - service: script.turn_on + continue_on_error: true + entity_id: script.notification_pushover_message + data: + variables: + target: Jeffs_phone + message: > + "NWS: {{ state_attr('sensor.nws_alerts', 'Alerts')[0].Event }}" + sound: echo + - delay: + seconds: 2 + - if: + - "{{ (state_attr('sensor.nws_alerts', 'Alerts')[1].ID not in states.sensor.nws_alerts_event_ids.attributes.values()|list) and (state_attr('sensor.nws_alerts', 'Alerts')[1].ID != states('sensor.nws_alerts_event_ids')) }}" + then: + - service: script.turn_on + continue_on_error: true + entity_id: script.notification_pushover_message + data: + variables: + target: Jeffs_phone + message: > + "NWS: {{ state_attr('sensor.nws_alerts', 'Alerts')[1].Event }}" + sound: echo + - conditions: + - "{{ states('sensor.nws_alerts') | int == 1 }}" + sequence: + - if: + - "{{ (state_attr('sensor.nws_alerts', 'Alerts')[0].ID not in states.sensor.nws_alerts_event_ids.attributes.values()|list) and (state_attr('sensor.nws_alerts', 'Alerts')[0].ID != states('sensor.nws_alerts_event_ids')) }}" + then: + - service: script.turn_on + continue_on_error: true + entity_id: script.notification_pushover_message + data: + variables: + target: Jeffs_phone + message: > + "NWS: {{ state_attr('sensor.nws_alerts', 'Alerts')[0].Event }}" + sound: echo + - delay: + seconds: 5 + - condition: template + value_template: "{{ states('sensor.nws_alerts') | int >= 4 }}" + - condition: template + value_template: > + {% set ns = namespace(ids=[]) %} + {% for x in range(0,states('sensor.nws_alerts')|int ) %} + {% set id = state_attr('sensor.nws_alerts', 'Alerts')[x].ID %} + {% set ns.ids = ns.ids + [id] %} + {% endfor -%} + {% set current_id_list = set(ns.ids) %} + {% set history_id_list = set( (states.sensor.nws_alerts_event_ids.attributes.values()| reject('match', 'nws_alerts_event_ids') | reject('match', 'None') | reject('match', 'unknown'))|list ) %} + {{ (current_id_list.difference(history_id_list)|list) | count > 0 }} + - condition: template + value_template: > + {{ (state_attr('sensor.nws_alerts', 'Alerts')[0].ID not in states.sensor.nws_alerts_event_ids.attributes.values()|list) + and (state_attr('sensor.nws_alerts', 'Alerts')[0].ID != states('sensor.nws_alerts_event_ids')) }} + - service: script.turn_on + continue_on_error: true + entity_id: script.notification_pushover_message + data: + variables: + target: Jeffs_phone + message: "NWS: More Than 3 New Alerts! Check your local National Weather Service for all current alerts!" + sound: echo + + + ############################## ANNOUNCEMENTS ############################################################## + +## this is where you will determine which alerts are announced instead of simply being sent to a notification. +## it will announce every new alert that matches the criteria in the "test_list" even if there are more than one of the same type of alert +## (i.e. if there are two tornado warnings issued at the same time then you will get two announcements in a row for a tornado warning). +## I only typically trigger announcements for dangerous weather (tornadoes & severe thunderstorms) but I've included others as examples. +## you will need to add or remove any events as needed in the "test_list" and also the "repeat" section as dsired. + +## also I am using the Alexa Media Player custom integration to play the announcement. If you don't use that integration you may have to adjust the service call for your system. + +## lastly, I am calling two different scripts for announcements - one for very dangedrous weather (i.e. tornoados) that will play throughtout the house including the bedrooms +## and a different one for everything else that won't wake people up for, for example, a heat advisory. +## modify this to your desired requirements. + + - alias: NWS Alerts Announce Weather Alert + id: nws_alerts_announce_weather_alert + initial_state: 'on' + trigger: + - platform: state + entity_id: sensor.nws_alerts + condition: + - "{{states('sensor.nws_alerts') | int > 0}}" + - condition: template + value_template: > + {% set ns = namespace(ids=[]) %} + {% for x in range(0,states('sensor.nws_alerts')|int ) %} + {% set id = state_attr('sensor.nws_alerts', 'Alerts')[x].ID %} + {% set ns.ids = ns.ids + [id] %} + {% endfor -%} + {% set current_id_list = set(ns.ids) %} + {% set history_id_list = set( (states.sensor.nws_alert_event_ids.attributes.values()| reject('match', 'nws_alerts_event_ids') | reject('match', 'None') | reject('match', 'unknown'))|list ) %} + {{ (current_id_list.difference(history_id_list)|list) | count > 0 }} + - condition: template + value_template: > + {% set ns = namespace(events=[]) %} + {% for x in range(0,states('sensor.nws_alerts')|int ) %} + {% set event = state_attr('sensor.nws_alerts', 'Alerts')[x].Event %} + {% set ns.events = ns.events + [event] %} + {% endfor -%} + {% set alerts_list = set(ns.events) %} + {% set test_list = set(['Tornado Warning', 'Severe Thunderstorm Warning', 'Air Quality Alert', 'Heat Advisory', 'Severe Thunderstorm Watch', 'Excessive Heat Warning' ]) %} + {{ test_list.intersection(alerts_list) | count > 0 }} + action: + - repeat: + count: "{{ states('sensor.nws_alerts') | int }}" + sequence: + - if: + - condition: template + value_template: > + {% set ns_events = namespace(events=[]) %} + {% set ns_dict = namespace(events_dict=[]) %} + {% for x in range(0,states('sensor.nws_alerts')|int ) %} + {% set event = state_attr('sensor.nws_alerts', 'Alerts')[x].Event %} + {% set ns_events.events = ns_events.events + [event] %} + {% set event_dict_list = dict(event = state_attr('sensor.nws_alerts', 'Alerts')[x].Event, id=state_attr('sensor.nws_alerts', 'Alerts')[x].ID) %} + {% set ns_dict.events_dict = ns_dict.events_dict + [event_dict_list] %} + {% endfor -%} + {%- set history_id_list = set( (states.sensor.nws_alerts_announced_ids.attributes.values() | reject('match', 'nws_alerts_announced_ids') | reject('match', 'None') | reject('match', 'unknown'))|list ) %} + {{ ns_dict.events_dict[repeat.index - 1].event == 'Tornado Warning' and ns_dict.events_dict[repeat.index - 1].id not in history_id_list }} + then: + - service: script.turn_on + entity_id: script.nws_alerts_announce_tornado_warning + data: + variables: + message: Attention!!,,,Attention!!,,,The National Weather Service Has issued a tornado warning for our area! + - service: variable.update_sensor + target: + entity_id: sensor.nws_alerts_announced_ids + data: + value: > + {% set ns_dict = namespace(events_dict=[]) %} + {% for x in range(0,states('sensor.nws_alerts')|int ) %} + {% set event_dict_list = dict(event = state_attr('sensor.nws_alerts', 'Alerts')[x].Event, id=state_attr('sensor.nws_alerts', 'Alerts')[x].ID) %} + {% set ns_dict.events_dict = ns_dict.events_dict + [event_dict_list] %} + {% endfor -%} + {{ ns_dict.events_dict[repeat.index - 1].id}} + ## set this delay to ensure that any audio is completed before triggering the next announcement + - delay: + minutes: 1 + - repeat: + count: "{{ states('sensor.nws_alerts') | int }}" + sequence: + - if: + - condition: template + value_template: > + {% set ns_events = namespace(events=[]) %} + {% set ns_dict = namespace(events_dict=[]) %} + {% for x in range(0,states('sensor.nws_alerts')|int ) %} + {% set event = state_attr('sensor.nws_alerts', 'Alerts')[x].Event %} + {% set ns_events.events = ns_events.events + [event] %} + {% set event_dict_list = dict(event = state_attr('sensor.nws_alerts', 'Alerts')[x].Event, id=state_attr('sensor.nws_alerts', 'Alerts')[x].ID) %} + {% set ns_dict.events_dict = ns_dict.events_dict + [event_dict_list] %} + {% endfor -%} + {%- set history_id_list = set( (states.sensor.nws_alerts_announced_ids.attributes.values() | reject('match', 'nws_alerts_announced_ids') | reject('match', 'None') | reject('match', 'unknown'))|list ) %} + {{ ns_dict.events_dict[repeat.index - 1].event == 'Severe Thunderstorm Warning' and ns_dict.events_dict[repeat.index - 1].id not in history_id_list }} + then: + - service: script.turn_on + entity_id: script.nws_alerts_announce_thunderstorm_warning + data: + variables: + message: Attention!!,,,Attention!!,,,The National Weather Service Has issued a severe thunderstorm warning for our area! + - service: variable.update_sensor + target: + entity_id: sensor.nws_alerts_announced_ids + data: + value: > + {% set ns_dict = namespace(events_dict=[]) %} + {% for x in range(0,states('sensor.nws_alerts')|int ) %} + {% set event_dict_list = dict(event = state_attr('sensor.nws_alerts', 'Alerts')[x].Event, id=state_attr('sensor.nws_alerts', 'Alerts')[x].ID) %} + {% set ns_dict.events_dict = ns_dict.events_dict + [event_dict_list] %} + {% endfor -%} + {{ ns_dict.events_dict[repeat.index - 1].id}} + - delay: + minutes: 1 + - repeat: + count: "{{ states('sensor.nws_alerts') | int }}" + sequence: + - if: + - condition: template + value_template: > + {% set ns_events = namespace(events=[]) %} + {% set ns_dict = namespace(events_dict=[]) %} + {% for x in range(0,states('sensor.nws_alerts')|int ) %} + {% set event = state_attr('sensor.nws_alerts', 'Alerts')[x].Event %} + {% set ns_events.events = ns_events.events + [event] %} + {% set event_dict_list = dict(event = state_attr('sensor.nws_alerts', 'Alerts')[x].Event, id=state_attr('sensor.nws_alerts', 'Alerts')[x].ID) %} + {% set ns_dict.events_dict = ns_dict.events_dict + [event_dict_list] %} + {% endfor -%} + {%- set history_id_list = set( (states.sensor.nws_alerts_announced_ids.attributes.values() | reject('match', 'nws_alerts_announced_ids') | reject('match', 'None') | reject('match', 'unknown'))|list ) %} + {{ ns_dict.events_dict[repeat.index - 1].event == 'Heat Advisory' and ns_dict.events_dict[repeat.index - 1].id not in history_id_list }} + then: + - service: script.turn_on + entity_id: script.nws_alerts_announce + data: + variables: + message: Attention!!,,,Attention!!,,,The National Weather Service Has issued a heat advisory for our area! + - service: variable.update_sensor + target: + entity_id: sensor.nws_alerts_announced_ids + data: + value: > + {% set ns_dict = namespace(events_dict=[]) %} + {% for x in range(0,states('sensor.nws_alerts')|int ) %} + {% set event_dict_list = dict(event = state_attr('sensor.nws_alerts', 'Alerts')[x].Event, id=state_attr('sensor.nws_alerts', 'Alerts')[x].ID) %} + {% set ns_dict.events_dict = ns_dict.events_dict + [event_dict_list] %} + {% endfor -%} + {{ ns_dict.events_dict[repeat.index - 1].id}} + - delay: + minutes: 1 + - repeat: + count: "{{ states('sensor.nws_alerts') | int }}" + sequence: + - if: + - condition: template + value_template: > + {% set ns_events = namespace(events=[]) %} + {% set ns_dict = namespace(events_dict=[]) %} + {% for x in range(0,states('sensor.nws_alerts')|int ) %} + {% set event = state_attr('sensor.nws_alerts', 'Alerts')[x].Event %} + {% set ns_events.events = ns_events.events + [event] %} + {% set event_dict_list = dict(event = state_attr('sensor.nws_alerts', 'Alerts')[x].Event, id=state_attr('sensor.nws_alerts', 'Alerts')[x].ID) %} + {% set ns_dict.events_dict = ns_dict.events_dict + [event_dict_list] %} + {% endfor -%} + {%- set history_id_list = set( (states.sensor.nws_alerts_announced_ids.attributes.values() | reject('match', 'nws_alerts_announced_ids') | reject('match', 'None') | reject('match', 'unknown'))|list ) %} + {{ ns_dict.events_dict[repeat.index - 1].event == 'Severe Thunderstorm Watch' and ns_dict.events_dict[repeat.index - 1].id not in history_id_list }} + then: + - service: script.turn_on + entity_id: script.nws_alerts_announce_thunderstorm_warning + data: + variables: + message: Attention!!,,,Attention!!,,,The National Weather Service Has issued a severe thunderstorm watch for our area! + - service: variable.update_sensor + target: + entity_id: sensor.nws_alerts_announced_ids + data: + value: > + {% set ns_dict = namespace(events_dict=[]) %} + {% for x in range(0,states('sensor.nws_alerts')|int ) %} + {% set event_dict_list = dict(event = state_attr('sensor.nws_alerts', 'Alerts')[x].Event, id=state_attr('sensor.nws_alerts', 'Alerts')[x].ID) %} + {% set ns_dict.events_dict = ns_dict.events_dict + [event_dict_list] %} + {% endfor -%} + {{ ns_dict.events_dict[repeat.index - 1].id}} + - delay: + minutes: 1 + - repeat: + count: "{{ states('sensor.nws_alerts') | int }}" + sequence: + - if: + - condition: template + value_template: > + {% set ns_events = namespace(events=[]) %} + {% set ns_dict = namespace(events_dict=[]) %} + {% for x in range(0,states('sensor.nws_alerts')|int ) %} + {% set event = state_attr('sensor.nws_alerts', 'Alerts')[x].Event %} + {% set ns_events.events = ns_events.events + [event] %} + {% set event_dict_list = dict(event = state_attr('sensor.nws_alerts', 'Alerts')[x].Event, id=state_attr('sensor.nws_alerts', 'Alerts')[x].ID) %} + {% set ns_dict.events_dict = ns_dict.events_dict + [event_dict_list] %} + {% endfor -%} + {%- set history_id_list = set( (states.sensor.nws_alerts_announced_ids.attributes.values() | reject('match', 'nws_alerts_announced_ids') | reject('match', 'None') | reject('match', 'unknown'))|list ) %} + {{ ns_dict.events_dict[repeat.index - 1].event == 'Air Quality Alert' and ns_dict.events_dict[repeat.index - 1].id not in history_id_list }} + then: + - service: script.turn_on + entity_id: script.nws_alerts_announce_thunderstorm_warning + data: + variables: + message: Attention!!,,,Attention!!,,,The National Weather Service Has issued an air quality alert for our area! + - service: variable.update_sensor + target: + entity_id: sensor.nws_alerts_announced_ids + data: + value: > + {% set ns_dict = namespace(events_dict=[]) %} + {% for x in range(0,states('sensor.nws_alerts')|int ) %} + {% set event_dict_list = dict(event = state_attr('sensor.nws_alerts', 'Alerts')[x].Event, id=state_attr('sensor.nws_alerts', 'Alerts')[x].ID) %} + {% set ns_dict.events_dict = ns_dict.events_dict + [event_dict_list] %} + {% endfor -%} + {{ ns_dict.events_dict[repeat.index - 1].id}} + - delay: + minutes: 1 + - repeat: + count: "{{ states('sensor.nws_alerts') | int }}" + sequence: + - if: + - condition: template + value_template: > + {% set ns_events = namespace(events=[]) %} + {% set ns_dict = namespace(events_dict=[]) %} + {% for x in range(0,states('sensor.nws_alerts')|int ) %} + {% set event = state_attr('sensor.nws_alerts', 'Alerts')[x].Event %} + {% set ns_events.events = ns_events.events + [event] %} + {% set event_dict_list = dict(event = state_attr('sensor.nws_alerts', 'Alerts')[x].Event, id=state_attr('sensor.nws_alerts', 'Alerts')[x].ID) %} + {% set ns_dict.events_dict = ns_dict.events_dict + [event_dict_list] %} + {% endfor -%} + {%- set history_id_list = set( (states.sensor.nws_alerts_announced_ids.attributes.values() | reject('match', 'nws_alerts_announced_ids') | reject('match', 'None') | reject('match', 'unknown'))|list ) %} + {{ ns_dict.events_dict[repeat.index - 1].event == 'Excessive Heat Warning' and ns_dict.events_dict[repeat.index - 1].id not in history_id_list }} + then: + - service: script.turn_on + entity_id: script.nws_alerts_announce_thunderstorm_warning + data: + variables: + message: Attention!!,,,Attention!!,,,The National Weather Service Has issued an excessive heat warning for our area! + - service: variable.update_sensor + target: + entity_id: sensor.nws_alerts_announced_ids + data: + value: > + {% set ns_dict = namespace(events_dict=[]) %} + {% for x in range(0,states('sensor.nws_alerts')|int ) %} + {% set event_dict_list = dict(event = state_attr('sensor.nws_alerts', 'Alerts')[x].Event, id=state_attr('sensor.nws_alerts', 'Alerts')[x].ID) %} + {% set ns_dict.events_dict = ns_dict.events_dict + [event_dict_list] %} + {% endfor -%} + {{ ns_dict.events_dict[repeat.index - 1].id}} + - delay: + minutes: 1 + + - alias: NWS Alerts Update Announced Event ID History + id: nws_alerts_update_announced_event_id_history + initial_state: 'on' + trigger: + - platform: state + entity_id: sensor.nws_alerts_announced_ids + to: + action: + - service: variable.update_sensor + target: + entity_id: sensor.nws_alerts_announced_ids + data: + attributes: + history_1: "{{ states('sensor.nws_alerts_announced_ids') }}" + history_2: "{{ state_attr('sensor.nws_alerts_announced_ids', 'history_1') }}" + history_3: "{{ state_attr('sensor.nws_alerts_announced_ids', 'history_2') }}" + history_4: "{{ state_attr('sensor.nws_alerts_announced_ids', 'history_3') }}" + history_5: "{{ state_attr('sensor.nws_alerts_announced_ids', 'history_4') }}" + history_6: "{{ state_attr('sensor.nws_alerts_announced_ids', 'history_5') }}" + history_7: "{{ state_attr('sensor.nws_alerts_announced_ids', 'history_6') }}" + history_8: "{{ state_attr('sensor.nws_alerts_announced_ids', 'history_7') }}" + history_9: "{{ state_attr('sensor.nws_alerts_announced_ids', 'history_8') }}" + history_10: "{{ state_attr('sensor.nws_alerts_announced_ids', 'history_9') }}" + history_11: "{{ state_attr('sensor.nws_alerts_announced_ids', 'history_10') }}" + history_12: "{{ state_attr('sensor.nws_alerts_announced_ids', 'history_11') }}" + history_13: "{{ state_attr('sensor.nws_alerts_announced_ids', 'history_12') }}" + history_14: "{{ state_attr('sensor.nws_alerts_announced_ids', 'history_13') }}" + history_15: "{{ state_attr('sensor.nws_alerts_announced_ids', 'history_14') }}" + + +################################# SCRIPT ################################### + +script: + nws_alerts_persistent_notification: + alias: NWS Alerts Persistent Notifications + sequence: + - service: > + {% if states('sensor.nws_alerts') != '0' %} + persistent_notification.create + {% endif %} + data: + notification_id: "{{ notification_id }}" + message: "{{ message }}" + title: '{{ title }}' + +## you will need to modify the following two scripts for your system media players and if you use the provided example alert tone + + nws_alerts_announce_tornado_warning: + alias: NWS Alerts Announce Tornado Warning + sequence: + - service: media_player.volume_set + data: + entity_id: + - media_player.basement_dot + - media_player.bedroom_2_dot + - media_player.big_room_dot + - media_player.computer_room_dot + - media_player.garage_dot + - media_player.kitchen_dot + - media_player.livingroom_dot + - media_player.master_bedroom_dot + - media_player.sunroom_dot + - media_player.upper_hall_dot + volume_level: 0.9 + - service: notify.alexa_media + data: + target: + - media_player.basement_dot + - media_player.bedroom_2_dot + - media_player.big_room_dot + - media_player.computer_room_dot + - media_player.garage_dot + - media_player.kitchen_dot + - media_player.livingroom_dot + - media_player.master_bedroom_dot + - media_player.sunroom_dot + - media_player.upper_hall_dot + data: + type: tts + message: "{{ message }}" + - delay: '00:00:15' + - service: notify.alexa_media + data: + message: "