Ok, I don’t post this here because I’m particularly proud of it, in fact I wholeheartedly welcome improvements…
This is a sensor with a state of the number of current weather warnings with the attribute ‘warnings’ giving a list of current warnings and the validity period in natural language.
For example currently my sensor shows this:
As I said I just post it here in case it is interesting to anyone, not as an example of great coding!
I use it for phone notifications. Persistent notifications and as others do here, a Lovelace card.
sensor:
#================================================================================
#=== RSS Feed for Met Office Severe weather warnings (London and the South East)
#=== Note that these warnings do not all necessarily affect 'Greater London'
#================================================================================
- platform: feedparser
name: Met Office RSS Feed South East Weather Warnings
feed_url: 'http://metoffice.gov.uk/public/data/PWSCache/WarningsRSS/Region/se'
date_format: '%a, %b %d %I:%M %p'
template:
#=== Met Office London Weather Warnings
- trigger:
- platform: state
entity_id: sensor.met_office_rss_feed_south_east_weather_warnings
attribute: entries
- platform: homeassistant
event: start
- platform: state
entity_id: sensor.date
sensor:
name: Met Office London Weather Warnings
state: >
{% set ns = namespace(warning_count = 0) %}
{% for entries in state_attr('sensor.met_office_rss_feed_south_east_weather_warnings', 'entries') %}
{% if 'Greater London' in states.sensor.met_office_rss_feed_south_east_weather_warnings.attributes.entries[loop.index - 1].summary %}
{% set ns.warning_count = ns.warning_count + 1 %}
{% endif %}
{% endfor %}
{{ ns.warning_count }}
attributes:
warnings: >
{% set warning_count = states('sensor.met_office_london_weather_warnings') %}
{% set ns = namespace(json = '[') %}
{% for entries in states.sensor.met_office_rss_feed_south_east_weather_warnings.attributes.entries %}
{% if 'Greater London' in states.sensor.met_office_rss_feed_south_east_weather_warnings.attributes.entries[loop.index - 1].summary %}
{% set ns.json = ns.json + '{"warning": "' + states.sensor.met_office_rss_feed_south_east_weather_warnings.attributes.entries[loop.index - 1].title + '", "period": "' %}
{% set summary = states.sensor.met_office_rss_feed_south_east_weather_warnings.attributes.entries[loop.index - 1].summary %}
{% set valid_period = summary.split(' valid from ')[1] %}
{% set valid_from = valid_period.split(' to ')[0] %}
{% set valid_to = valid_period.split(' to ')[1] %}
{% set days_map = { 'Sat': 'Saturday', 'Sun': 'Sunday', 'Mon': 'Monday', 'Tue': 'Tuesday',
'Wed': 'Wednesday', 'Thu': 'Thursday', 'Fri': 'Friday'} %}
{% set months_map = { 'Jan': 'January', 'Feb': 'February', 'Mar': 'March', 'Apr': 'April',
'May': 'May', 'Jun': 'June', 'Jul': 'July', 'Aug': 'August',
'Sep': 'September', 'Oct': 'October', 'Nov': 'November', 'Dec': 'December'} %}
{# #}
{#-- Macro to build the valid from and to phrases --#}
{# -------------------------------------------- #}
{# #}
{% macro valid_from_to(validity_time) %}
{# #}
{#-- Extract hour and minutes --#}
{# #}
{% set validity_hour = validity_time.split(' ')[0][0:2] | int %}
{% set validity_minutes = validity_time.split(' ')[0][2:4] | int %}
{# #}
{#-- Get period of the day --#}
{# #}
{% if validity_hour == 12 %}
{% set validity_day_period = '' %}
{% elif validity_hour == 0 %}
{% set validity_day_period = '' %}
{% elif validity_hour < 12 %}
{% set validity_day_period = "morning" %}
{% elif validity_hour > 16 %}
{% set validity_day_period = "evening" %}
{% else %}
{% set validity_day_period = "afternoon" %}
{% endif %}
{# #}
{#-- Convert time to 12 hour format and get AM/PM --#}
{# #}
{% set am_pm = '' %}
{% if validity_hour == 12 and validity_minutes == 0 %}
{% set validity_hour = 'noon' %}
{% elif validity_hour == 11 and (50 < validity_minutes <= 59) %}
{% set validity_hour = 'noon' %}
{% elif validity_hour == 0 and validity_minutes == 0 %}
{% set validity_hour = 'the beginning of ' %}
{% elif validity_hour == 23 and (50 < validity_minutes <= 59) %}
{% set validity_hour = 'the end of ' %}
{% elif validity_hour > 12 %}
{% set am_pm = 'pm' %}
{% set validity_hour = validity_hour - 12 %}
{% else %}
{% set am_pm = 'am' %}
{% endif %}
{# #}
{#-- Extract minutes --#}
{# #}
{% if (0 <= validity_minutes < 10) or (50 < validity_minutes <= 59) %}
{% set validity_minutes = am_pm %}
{% else %}
{% if validity_minutes < 10 %}
{% set validity_minutes = '0' ~ validity_minutes %}
{% endif %}
{% set validity_minutes = ':' ~ validity_minutes ~ am_pm %}
{% endif %}
{# #}
{#-- Extract day, month and date --#}
{# #}
{% set validity_day = days_map[validity_time.split(' ')[1][0:3]] %}
{% set validity_month = months_map[validity_time.split(' ')[-1]] %}
{% set validity_date = validity_time.split(' ')[-2] | int | string %}
{# #}
{#-- Get suffix for date --#}
{# #}
{% if validity_date in ('1', '21', '31') %}
{% set suffix = 'st ' %}
{% elif validity_date in ('2', '22') %}
{% set suffix = 'nd ' %}
{% elif validity_date in ('3', '23') %}
{% set suffix = 'rd ' %}
{% else %}
{% set suffix = 'th ' %}
{% endif %}
{# #}
{#-- Build the final phrase --#}
{#-- First we need to zero pad the date --#}
{# #}
{% set zero_padded_date = '{:02}'.format(validity_time[5:].split(' ')[1] | int) %}
{% set zero_padded_date = validity_time[5:].split(' ')[0] + ' ' + zero_padded_date + ' ' + validity_time[5:].split(' ')[2] %}
{% if as_timestamp(now()) | timestamp_custom('%a %d %b') == zero_padded_date %}
{% if validity_hour == 'noon' %}
{% set phrase = validity_hour ~ ' today' %}
{% elif validity_hour == 'the beginning of ' %}
{% set phrase = validity_hour ~ ' tomorrow' %}
{% else %}
{% set phrase = validity_hour ~ validity_minutes ~ ' this ' ~ validity_day_period %}
{% endif %}
{% elif now().day + 1 == validity_date | int %}
{% set phrase = validity_hour ~ validity_minutes ~ ' tomorrow ' ~ validity_day_period %}
{% elif now().day - 1 == validity_date | int %}
{% set phrase = validity_hour ~ validity_minutes ~ ' yesterday ' ~ validity_day_period %}
{% else %}
{% if validity_hour == 'the beginning of ' or
validity_hour == 'the end of ' %}
{% set phrase = validity_hour ~ validity_minutes ~ validity_day ~ ' the ' ~ validity_date ~ suffix ~ 'of ' ~ validity_month %}
{% else %}
{% set phrase = validity_hour ~ validity_minutes ~ ' on ' ~ validity_day ~ ' the ' ~ validity_date ~ suffix ~ 'of ' ~ validity_month %}
{% endif %}
{% endif %}
{{ phrase }}
{% endmacro %}
{# #}
{#-- End of Macro --#}
{# ------------ #}
{# #}
{# #}
{#-- Get the valid to and valid from phrases #}
{# #}
{% if valid_to[:2] == '00' %}
{% if valid_to[2:4] == '00' %}
{% set to = 'the beginning of ' ~ days_map[valid_to[5:8]] %}
{% else %}
{% set to = valid_to[2:4] ~ ' minutes past midnight on' ~ valid_to[4:] %}
{% endif %}
{% else %}
{% set to = valid_from_to(valid_to) %}
{% endif %}
{% if valid_from[:2] == '00' %}
{% if valid_from[2:4] == '00' %}
{% set from = 'the beginning of ' ~ days_map[valid_to[5:8]] %}
{% else %}
{% set from = valid_from[2:4] ~ ' minutes past midnight on' ~ valid_from[4:] %}
{% endif %}
{% else %}
{% set from = valid_from_to(valid_from) %}
{% endif %}
{% if 'the beginning of' in from and
'the end of' not in to %}
{% if now().strftime('%A') in from %}
{% set from = 'the beginning of today' %}
{% elif (as_timestamp(now()) + 86400) | timestamp_custom('%A') in from %}
{% set from = 'the beginning of tomorrow' %}
{% else %}
{% endif %}
{% elif 'the beginning of' not in to and
'the end of' in to %}
{% if now().strftime('%A') in from %}
{% set to = 'the end of today' %}
{% elif (as_timestamp(now()) + 86400) | timestamp_custom('%A') in from %}
{% set to = 'the end of tomorrow' %}
{% else %}
{% endif %}
{% elif 'the beginning of' in from and
'the end of' in to %}
{% if now().strftime('%A') in from %}
{% set from = 'All day today' %}
{% elif (as_timestamp(now()) + 86400) | timestamp_custom('%A') in from %}
{% set from = 'All day tomorrow' %}
{% else %}
{% set from = from.replace('the beginning of', 'All day on') + '."' %}
{% endif %}
{% endif %}
{% set ns.json = ns.json + 'Valid from ' + from | trim + ' until ' + to | trim + '."' %}
{% set ns.json = ns.json + '}, ' %}
{% endif %}
{% endfor %}
{% if ns.json == '[' %}
{% set ns.json = ns.json + ']' %}
{% else %}
{% set ns.json = ns.json[0:-3] + '}]' %}
{% endif %}
{{ ns.json }}