I’m not sure if this is the answer to your question but I think so…
And as previously stated this was all written ages ago, it’s not great code but it works so I leave it alone.
I allow for up to five warnings at any one time;
#================
#=== Input Texts
#================
input_text:
weather_warning1:
min: 0
max: 255
... etc ...
weather_warning5:
min: 0
max: 255
I have the sensors as my previous post.
And I have this automation that saves the warnings:
#==============================
#=== Populate Weather Warnings
#==============================
- alias: Weather Populate Weather Warnings
id: weather_populate_weather_warnings
trigger:
- platform: state
entity_id: sensor.met_office_london_weather_warnings
- platform: homeassistant
event: start
action:
#=== Empty weather warning input texts
- service: input_text.set_value
data:
entity_id:
- input_text.weather_warning1
- input_text.weather_warning2
- input_text.weather_warning3
- input_text.weather_warning4
- input_text.weather_warning5
value: ""
#=== Check there are any weather warnings for London
- condition: numeric_state
entity_id: sensor.met_office_london_weather_warnings
above: 0
#=== Populate the input text for each warning
- repeat:
count: >
{{ states('sensor.met_office_london_weather_warnings') }}
sequence:
- service: input_text.set_value
data:
entity_id: input_text.weather_warning{{ repeat.index }}
value: >
{% set warning = state_attr('sensor.met_office_london_weather_warnings', 'warnings')[repeat.index-1].warning %}
{% set warning = warning.replace('affecting London & South East England)', 'affecting London') + '. ' %}
{% set period = state_attr('sensor.met_office_london_weather_warnings', 'warnings')[repeat.index-1].period %}
{{ warning + period }}
And this automation that handles the notifications (script.notify
is my notification engine):
#===============================
#=== Notify of weather warnings
#===============================
- alias: Notify Weather Warnings
id: notify_weather_warnings
trigger:
- platform: state
entity_id: sensor.met_office_london_weather_warnings
- platform: homeassistant
event: start
variables:
message: >
{% if states('sensor.met_office_london_weather_warnings') | int > 0 %}
{% for warning in state_attr('sensor.met_office_london_weather_warnings', 'warnings') %}
{%- set warning = state_attr('sensor.met_office_london_weather_warnings', 'warnings')[loop.index-1].warning %}
{%- set warning = warning.replace('affecting London & South East England', 'affecting London') + '. ' %}
{%- set period = state_attr('sensor.met_office_london_weather_warnings', 'warnings')[loop.index-1].period %}
{%- set message = warning ~ period %}
{%- if not loop.last %}
{% set message = message ~ '<br>' %}
{% endif %}
{{- message }}
{%- endfor %}
{% endif %}
action:
#=== Remove all current notifications
- service: persistent_notification.dismiss
data:
notification_id: weather_warning
- service: script.notify
data:
tell: me
mobile_app_tag: WEATHER_WARNING
message: clear_notification
#=== Continue if there are any weather warnings for London
- condition: numeric_state
entity_id: sensor.met_office_london_weather_warnings
above: 0
#=== Create HA Persistant notification
- service: script.notify
data_template:
show: True
notification_id: weather_warning
title: 🌩️ Met Office Weather Warnings
message: >
{%- set message = message | replace('Red','<font color=firebrick>RED</font>') %}
{%- set message = message | replace('Amber','<font color=DarkOrange>AMBER</font>') %}
{%- set message = message | replace('Yellow','<font color=yellow>YELLOW</font>') %}
{{ message }}
#=== Notify Me
- choose:
- conditions: >
{{ is_state('input_boolean.met_office_weather_warnings_notify_me', 'on') }}
sequence:
- service: script.notify
data:
tell: me
title: 🌩️ Met Office Weather Warnings
mobile_app_tag: WEATHER_WARNING
message: >
{%- set message = message | replace('Red','<b><span style="color: red">RED</span></b>') %}
{%- set message = message | replace('Amber','<b><span style="color: #FF8C00">AMBER</span></b>') %}
{%- set message = message | replace('Yellow','<b><span style="color: yellow">YELLOW</span></b>') %}
{{ message }}