This is just a contribution that is working for me. It’s based on the example that @123wrote a while back but I did a bit more so that my house can notify me (via TTS) to change the clocks.
- platform: template
sensors:
# Sensor to indicate if we are in DST
is_dst:
entity_id: sensor.date
value_template: "{{ now().timetuple().tm_isdst > 0 }}"
# Sensor to indicate if tomorrow the timezone changes (DST vs non DST)
dst_change_tomorrow:
entity_id: sensor.date
value_template: >
{% set dt = now() + timedelta(days=1) %}
{{ now().astimezone().tzinfo != dt.astimezone().tzinfo }}
Then I just have a schedule each day that checks if the dst_change_tomorrow is true and act upon it.
as an FYI, you no longer need to use the “entity_id:” to cause the sensor to update. now() is a valid means of entity updates and will update every minute.
as a matter of fact, you actually should be getting notifications in your log to that effect.
but thanks for this. I just saw someone else asking for this yesterday and I played around with it trying to figure out a way of doing it and couldn’t get it to work.
I must say that finding information on the different sub-routines/methods/filters for python datetimes is almost impossible without already knowing what you are looking for exists.
- alias: DST Warning
trigger:
- platform: time
at: '10:00:00'
- platform: time
at: '19:00:00'
condition:
- condition: template
value_template: >
{{ state_attr('sensor.dst', 'next').days_to_event in [7,1] }}
action:
- service: notify.homeowners
data:
message: >
{%- set next = state_attr('sensor.dst', 'next') %}
{%- set plural = 's' if next.days_to_event > 1 else '' %}
"Daylight savings in {{ next.days_to_event }} day{{plural}}, you will {{ next.phrase }}!"
EDIT 1:
It requires sensor.date to keep the calculations to once a day.
The automation says “Daylight savings in X days, you will gain an hour!” or “Daylight savings in X days, you will lose an hour!”
EDIT2:
It seems people dont have this setup properly. You have to change previous to the hour in which your daylight savings changes. For me, this occurs at 1:59.99 am, so I use 2 for previous. If your time occurs at 2:05 PM or 14:05, then you should use 15 for previous.
...
{%- set ns = namespace(previous = 15, spring=none, fall=none) %} # example with previous set to 15.
...
Keep in mind that you have to update it in 2 spots, the state and next
EDIT 3: I just made a new version that works everywhere in the world without altering. Seen here:
I like having everything in 1 place and 1 sensor. Reduces the number of entities and keeps the automations simple. Also, I wanted a datetime object to display the date in the UI and use an automation to trigger off it (using any number of days in advance).
don’t exactly remember where anymore, but had that invalid timestamp before. I think it was with several Astral sensors in the CC by Phil, on astral timings that were dependent on the suns elevation (which happened only on certain locations north of ours)
@petro, I want to know when daylight savings start and end in a time zone that is different to the HA system.
To explain more…
I am in Australia and have a HA system here and one in the UK. The timezone changes result in a time difference which varies between 9, 10 or 11 hours due to one of us being in summer whilst the other is in winter !
does the {{ now().timetuple().tm_isdst}} return the true time difference, or merely a 1 or 0 indicating yes /no for being in a dst time
I ask because the link to 123’s original post doesnt say so, and even he is not sure Id like to add this to the attributes, to indicate the difference to the time we’re in. -1, 0 or 1, not even sure either …
attribute_templates:
time difference: >
{{now().timetuple().tm_isdst}}
or would we need more than that.
btw I’ve never seen timetuple() being mentioned before, this is rather useful info: