Automatic shifting of some automations based on daylight savings time

First, I’ll start by saying I am very familiar with the sun element and use it extensively for some things like outdoor lighting.

However, I have some automations that trigger at specific times and I’d like them to auto adjust an hour based on the DST/PST setting of the day.

Here’s my general idea but I think there’s a more clever way to do it, that I’m hoping someone could share:

  1. Have a boolean helper that switches depending on the DST/PST setting (maybe a daily automation that runs at 2:01 to set this?)
  2. Somehow modify my automations to trigger on a time + offset based on #1.
    Currenty, one triggers via:
  • 8:00pm with a trigger ID
  • 9:00pm with a trigger ID
  • 10:00pm with a trigger ID
  1. So I’d like those to shift an hour with each time change.

I think #2 can be done with a template but I’m fairly new to that and am not quite sure how to code that part.

Thanks for any tips!

Hi,
Is the reason for wanting to shift the timing because of the shorter days in winter & the amount of light around?
I do some similar adjustments to when some automations are triggered, mainly around when lights might come one, based on lux from my outdoor weather station or indoor lux sensors.
Maybe that is something to consider, if you didn’t already.
Cheers
Nick

Yeah I thought about indoor lux readings from various motion sensors and such. I just haven’t taken the time to gather the data needed to do that, but that seems like a viable option.

I’ll consider that option and I’ll need to start tracking the data based on the lux values of things.

Hmm, darn, the motion sensors I use don’t share light level data. Doing some research into how to do this, with say, an ESP8266 and photo resister or something. Maybe that’s too complicated but I could measure different parts of a room with 2 or 3 sensors wired to the ESP.

Yep that’s true. Some observation of the lux readings at different times of the day/season & different cloud cover states etc will be needed.

My sensors are zigbee & many of the door/window sensors also have lux builtin. I also have a dedicated lux sensor in each room to use for this exact purpose. I’m very happy with my Xiaomi sensors, if you are looking to buy something.

Here’s a binary sensor that will switch on automatically when it is daylight savings:

template:
  - binary_sensor:
      - name: "Is DST"
        state: "{{ now().timetuple().tm_isdst == 1 }}"

Template triggers would be good for this.

How about using the sun’s azimuth, which is an attribute on sun.sun. That would be the same-ish time of the day, independent of daylight savings. I use this to close a blind for the period while the sun shines in on my laptop.

You can get the current time zone with this:

{{ now().astimezone().tzinfo }}

If you are in the Pacific Time Zone, it will return either PST or PDT. Try it out in the template editor in the developer tools.

But I’m not clear on what you’re trying to do. Do you want your automations to shift an hour during daylight savings to essentially negate the clock change? If so, you could just set your automations to happen at some offset from UTC, which isn’t impacted by any DST changes. A template trigger like the following would trigger after 10pm (22:00) PST during PST and after 9pm PDT during PDT:

{{ (utcnow().hour - 7 ) % 24 >= 22 }}

or

{{ (utcnow() - timedelta(hours=7)).hour >= today_at('22:00').hour }}

or simply

{{ utcnow().hour  >= 5 }}

Based on Tom’s suggestion, you could use this template trigger which would do the same as the ones I suggested but might be easier to read:

{{ now() >= today_at('22:00') - timedelta(hours=(now().timetuple().tm_isdst)) }}

It simply subtracts an hour off the trigger time during DST.

1 Like

For this the elevation would be more relevant, and is exactly what I use - and these days this is also a separate sensor (you may have to enable it).

1 Like

Where I live elevation varies too much between summer and winter, so using azimuth like a sundial works better. Depends on what you’re trying to achieve I guess.

Yeah, for blinds azimuth matters, but for light levels elevation usually matters more - unless you live in the shadow of mountains where a combo is needed.

First of all, thank you to everyone contributing to this thread, I appreciate all the ideas!

{{ now() >= today_at('22:00') - timedelta(hours=(now().timetuple().tm_isdst)) }}

I like this idea and will give it a try tonight. I’m trying to decide how the “template trigger” works though. When I test this in the developer tools, and pick a time a minute ahead, I see that when the time hits it changes from false to true but stays in that state.

Does a “template trigger” only fire when it a change in it’s state occurs?

Because your use of >= leaves the test true even after the time has passed.

I tried it with == and never saw it flip between false to true.

This is pretty close to what I was looking for.

Yes, the transition from false to true is what causes the trigger. Same for state and numeric_state triggers; they all trigger on the transition. It doesn’t matter how long the trigger remains satisfied. This is also the reason that creating a new automation with a trigger that is already satisfied will not cause the automation to run.

As to your other question: now() is accurate to milliseconds, and when you use it in templates, the templates will only get refreshed once per minute. So when you use == it is essentially impossible for that comparison to ever be true.

Ah, ok thanks for the clarification, that makes sense.

However, I am curious to when it will become false again? At midnight? Here’s my guess of what will happen:

At 11:59pm it’ll be true (11 > 5) but then at midnight it’ll reset to 00:00, turning it false. Make sense?

Note I was testing a time close to now (5ish am).

Yes, it will transition to false at midnight. That transition will have no effect on your automation.

OK, working on this. One more question though. I’ve got another automation that’s based on the sun object that I’d also like to offset by an hour based on DST.

trigger:
  - platform: sun
    event: sunset
    offset: "-01:15:00"

Can you help me with a template to offset that by either “-01:!5” or just “-00:15” based on the ```
tm_isdst() value ?

Creat two triggers. One for each offset.

Put in a template condition that only passes the one you want.

trigger:
  - platform: sun
    event: sunset
    offset: "-01:15:00"
    id: normal
  - platform: sun
    event: sunset
    offset: "-00:15:00"
    id: dst
condition:
  - condition: or
    conditions:
      - condition: template
        value_template: "{{ trigger.id == 'normal' and now().timetuple().tm_isdst == 0 }}"
      - condition: template
        value_template: "{{ trigger.id == 'dst' and now().timetuple().tm_isdst == 1 }}"
action:
...

Brilliant! Thank you so much and I hope this helps someone in the future…

1 Like