still having an issue here, because after the actual DST at 3 o’clock last night, it still shows:
the 2 top lines are ok, the 2 bottom lines aren’t. They say:
next change will take place tonight!
clock will then advance 1 hour and we loose an hour…
{% set x = ['unknown','unavailable'] %}
{% if states('sensor.daylight_savings_times') not in x %}
<img src = {{state_attr('binary_sensor.wintertijd','entity_picture')
if is_state('binary_sensor.wintertijd','on')
else state_attr('binary_sensor.zomertijd','entity_picture') }} width='80'>
**Zomertijd - Wintertijd**
DST is {{'niet actief' if state_attr('sensor.daylight_savings_times','dst_active')
== false else 'actief'}} en het is {{'Wintertijd' if is_state('binary_sensor.wintertijd','on')
else 'Zomertijd'}}.
{% set count = state_attr('sensor.daylight_savings_times','next')
.days_to_event %}
{% set datum = as_timestamp(states.sensor.daylight_savings_times.attributes.next.event,0)
|timestamp_custom('%d %B %Y',default=0) %}
De volgende wissel vindt
{%- if count == 0 %} vannacht plaats!
{%- elif count == 1 %} morgen plaats op {{datum}}.
{%- elif count == 2 %} overmorgen plaats op {{datum}}.
{%- elif count > 2 %} plaats over {{count}} dagen op {{datum}}.
{%- endif %}
De klok gaat dan 1 uur {{state_attr('sensor.daylight_savings_times','next')
.clock}} en we {{state_attr('sensor.daylight_savings_times','next').phrase}}.
{% else %} Calculating
{% endif %}
ofc, this is built upon the bigger dst template, which I now have to be:
sensor:
- unique_id: daylight_savings_times
name: Daylight savings times
device_class: timestamp
state: >
{%- set ns = namespace(previous=3,spring=none,fall=none) %}
{%- set today = strptime(states('sensor.date'),'%Y-%m-%d').astimezone().replace(hour=ns.previous) %}
{%- for i in range(365) %}
{%- set day = (today + timedelta(days=i)).astimezone() %}
{%- if ns.previous - day.hour == -1 %}
{%- set ns.spring = today + timedelta(days=i) %}
{%- elif ns.previous - day.hour == 1 %}
{%- set ns.fall = today + timedelta(days=i) %}
{%- endif %}
{%- set ns.previous = day.hour %}
{%- endfor %}
{{([ns.spring,ns.fall]|min).isoformat()}}
# {{as_timestamp([ns.spring,ns.fall]|min)|timestamp_custom('%A %-d %B %Y at %-H am')}}
icon: >
mdi:{{(now().timetuple().tm_isdst == 1)|iif('update','history')}}
attributes:
icon_color: >
{{'gold' if is_state('binary_sensor.zomertijd','on') else 'steelblue'}}
# https://pythontic.com/datetime/datetime/timetuple
dst_active: >
{{now().timetuple().tm_isdst == 1}}
dst_change_tomorrow: >
{% set dt = now() + timedelta(days=1) %}
{{now().astimezone().tzinfo != dt.astimezone().tzinfo}}
dst_changed_today: >
{% set dt = now() + timedelta(days=-1) %}
{{now().astimezone().tzinfo != dt.astimezone().tzinfo}}
next: >
{%- set ns = namespace(previous = 3,spring=none,fall=none) %}
{%- set today = strptime(states('sensor.date'),'%Y-%m-%d').astimezone().replace(hour=ns.previous) %}
{%- for i in range(365) %}
{%- set day = (today + timedelta(days=i)).astimezone() %}
{%- if ns.previous - day.hour == -1 %}
{%- set ns.spring = today + timedelta(days=i) %}
{%- elif ns.previous - day.hour == 1 %}
{%- set ns.fall = today + timedelta(days=i) %}
{%- endif %}
{%- set ns.previous = day.hour %}
{%- endfor %}
{%- set next = [ns.spring, ns.fall]|min %}
{%- set phrase = 'verliezen een uur' if next == ns.spring else 'krijgen een uur extra' %}
{%- set clock = 'vooruit' if next == ns.spring else 'terug' %}
{"spring": "{{ns.spring.isoformat()}}",
"fall": "{{ns.fall.isoformat()}}",
"event": "{{next.isoformat()}}",
"days_to_event":{{(next-today).days}},
"phrase": "{{phrase}}",
"clock":"{{clock}}"}
this is triggered per hour, but the attribute next
items change per day. Meaning, if the event has happened, and it is still this day, the attributes dont change. Which is a bit of a nasty detail really. Now have to wait a full day for this to become correct again.
current output in dev tools:
- unique_id: daylight_savings_times
name: Daylight savings times
device_class: timestamp
state: >
2023-03-26T03:00:00+01:00
# Sunday 26 March 2023 at 4 am
icon: >
mdi:update
attributes:
icon_color: >
gold
# https://pythontic.com/datetime/datetime/timetuple
dst_active: >
True
dst_change_tomorrow: >
False
dst_changed_today: >
True
next: >
{"spring": "2023-03-26T03:00:00+01:00",
"fall": "2023-10-29T03:00:00+01:00",
"event": "2023-03-26T03:00:00+01:00",
"days_to_event":0,
"phrase": "verliezen een uur",
"clock":"vooruit"}
fwiw, the mentioned binary_sensors inside the other templates are:
binary_sensor:
- unique_id: zomertijd_binary
name: Zomertijd
picture: /local/season/summer.png
state: >
{{now().timetuple().tm_isdst == 1}}
- unique_id: wintertijd_binary
name: Wintertijd
picture: /local/season/winter.png
state: >
{{now().timetuple().tm_isdst == 0}}
so always spot on