Today, (and maybe it’s always been this way), I’m getting the right date, but wrong day.
My sensor.weather_alerts
shows this"
entries:
- title: Yellow warning of thunderstorm affecting London & South East England
summary: >-
Yellow warning of thunderstorm affecting London & South East England:
Bracknell Forest, Brighton and Hove, Buckinghamshire, East Sussex, Greater
London, Hampshire, Isle of Wight, Kent, Medway, Milton Keynes, Oxfordshire,
Portsmouth, Reading, Slough, Southampton, Surrey, West Berkshire, West
Sussex, Windsor and Maidenhead, Wokingham valid from 1100 Sun 18 Jun to 2259
Sun 18 Jun
icon: mdi:rss
friendly_name: Weather Alerts
Sun 18 Jun is correct, but my markdown (below) shows Mon (not Sun)
{% for item in state_attr('sensor.weather_alerts','entries') %}
{% for type in [
('extreme heat'),('fog'),('ice'),('lightning'),('rain'),('rain, wind'),('snow'),('snow, ice'),('thunderstorm'),('thunderstorms'),('wind')]
if type == item.summary | regex_findall_index('.*warning of (.*) affecting.*', ignorecase=True) %}
{% set summary = item.summary | regex_findall_index('(.*) of (.*) affecting .* valid from (.*) to (.*)', ignorecase=True) %}
{% set time_from = as_timestamp(strptime(summary[2], "%H%M %a %d %b")) | timestamp_custom("%H:%M %a") %}
{% set time_to = as_timestamp(strptime(summary[3], "%H%M %a %d %b")) | timestamp_custom("%H:%M %a") %}
{% set message = summary[0] + ' | ' + summary[1] +'\nfrom: '+ time_from +'\nto: '+ time_to %}
{{ message }}
{% endfor %}
{% endfor %}
{% if state_attr('sensor.weather_alerts','entries') != 0 %}
{% for item in state_attr('sensor.weather_alerts','entries') %}
{% for type, icon in [('rain', 'weather-pouring'), ('thunderstorms', 'weather-lightning-rainy'),
('wind', 'weather-windy'), ('snow', 'weather-snowy-heavy'),
('snow, ice', 'weather-snowy-heavy'),
('lightning', 'weather-lightning'), ('ice', 'car-traction-control'),
('fog', 'weather-fog'), ('extreme heat', 'weather-sunny-alert'), ('thunderstorms', 'weather-lightning')] if type == item.summary | regex_findall_index('.*warning of (.*) affecting.*', ignorecase=True) %}
{% set color = item.summary.split(' ')[0] %}
{% set summary = item.summary | regex_findall_index('(.*) affecting (.*) valid from (.*) to (.*)', ignorecase=True) %}
{% set time_from = as_timestamp(strptime(summary[2], "%H%M %a %d %b")) | timestamp_custom("%H:%M %d/%m") %}
{% set time_to = as_timestamp(strptime(summary[3], "%H%M %a %d %b")) | timestamp_custom("%H:%M %d/%m") %}
{% set link = item.link %}
| | | |
| --- | --- | --- |
| <font color = {%- if 'Yellow' == color %}'gold'
{%- elif 'Amber' == color %}'darkorange'
{%- else %}'firebrick'
{%- endif %}><ha-icon icon={{ "'mdi:" + icon + "'" }}></ha-icon></font> | | **<a href="{{ link }}" target="_blank">{{ summary[0] | title }}</a>**<br />{{ time_from }} - {{ time_to }}<br />{{ summary[1] }} |
{% endfor %}
{% endfor %}
{% else %}
No warnings at present
{% endif %}
Edit:
It appears that strptime(summary[3], "%H%M %a %d %b")
results in a date in 1900 (1900-06-18 22:59:00
). I assume this is because there is no year in the original date (it only had 1100 Sun 18
).
Edit:
OK, here’s my fix using slicing - hope it helps people:
{% set time_from = summary[2][0:2] +':'+ summary[2][2:5] +'on '+ summary[2][5:8] +'-'+ summary[2][9:11] +'-'+ summary[2][12:] %}
{% set time_to = summary[3][0:2] +':'+ summary[3][2:5] +'on '+ summary[3][5:8] +'-'+ summary[3][9:11] +'-'+ summary[3][12:] %}
which gives the correct (as of today - Sun -18-Jun) dates of::
from: 11:00 on Sun-18-Jun
to: 22:59 on Sun-18-Jun
Don’t forget the time is displayed as GMT, so here’s a version with the ‘z’ for Zulu (as I know I’m going to forget this!).
{% set time_from = summary[2][0:2] +':'+ summary[2][2:4] +'z on '+ summary[2][5:8] +'-'+ summary[2][9:11] +'-'+ summary[2][12:] %}
{% set time_to = summary[3][0:2] +':'+ summary[3][2:4] +'z on '+ summary[3][5:8] +'-'+ summary[3][9:11] +'-'+ summary[3][12:] %}