Repository navigation
Conversation
custom-components#649) The time trigger cycle computes time_next_adj so that (time_next_adj - now) is the real time to wait across a DST change, but after sleeping it re-checked that target against naive local time. When the clocks go back, local time repeats an hour, so the re-check slept that hour again: period() triggers stopped for an hour and cron() triggers crossing the change fired an hour late. Measure the remaining wait in UTC instead.
craigbarratt
left a comment
There was a problem hiding this comment.
Thanks @benbraun, this is a great write-up and a clean fix!
Re-checking the wait against a UTC deadline is the right change. time_next_adj already gets the first wait right, and only the naive re-check after the sleep repeated the hour. I merged this onto current master: all 209 tests pass, and ruff, pylint and mypy are clean. With the naive re-check put back, both fall-back tests fail and the spring-forward test still passes, just as you described. The fake clock is a nice way to make the DST cases fast and deterministic. With this, the code now does what the DST section of the reference docs already says.
There's no need to change the legacy paths, here or in a follow-up. The legacy decorators are going to be removed after the next major release.
Fixes #649.
When the clocks go back,
@time_triggerloses an hour. Aperiod()trigger stops for an hour, and acron()trigger whose next run crosses the change fires an hour late. I hit both on 2026-10-25 (Europe/Berlin) on 2.1.0. Aperiod(now, 5s)trigger went quiet from 02:59:44 CEST to 03:00:14 CET. Acron(*/5 * * * *)trigger went from 02:55 CEST straight to 04:00 CET instead of 03:00 CET.Cause
TimeTriggerDecorator._cyclegetstime_next_adjfromtimer_trigger_next. That value already accounts for the DST change, sotime_next_adj - nowis the real time to wait. After sleeping, though, the cycle re-checks the target against naive local time (dt_now()). When the clocks go back, naive local time repeats an hour. The re-check then finds the target an hour away and sleeps that hour again.cron(*/5)at 02:55 CEST: the next run is 03:00 CET, 65 minutes away. After 65 minutes the local clock reads 03:00, buttime_next_adjis02:55 + 65 min = 04:00. The trigger waits another hour.period(..., 5s)at 02:59:59 CEST: the next slot is naive 03:00:04, 5 s away. After 5 s the local clock reads 02:00:04 CET, so the trigger waits until the naive clock reaches 03:00:04 again.Change
The cycle now records a UTC deadline when it starts sleeping and re-checks the remaining wait against UTC. The loop still guards against the sleep returning slightly early. UTC doesn't repeat, so the computed wait is used once. Once a trigger fires,
nowis the repeated local time, soperiod()keeps its interval through the repeated hour.cron()behaviour across the change is otherwise unchanged. As the existing DST tests expect,*/5still doesn't fire during the repeated hour itself. It now fires once the clocks reach 03:00 CET.Tests
tests/test_time_trigger_dst.pyruns the cycle with a fake clock whose local reading follows America/Los_Angeles, like the existing DST tests.asyncio.sleepadvances the fake clock instead of sleeping. The tests check:period(now, 5s)keeps 5 s gaps across the fall-back. Without the fix there is a 3605 s gap.cron(*/5 * * * *)fires at 08:55, 10:00 and 10:05 UTC. Without the fix the second run is at 11:00.period(now, 5s)keeps 5 s gaps across the spring-forward. This passes before and after the fix and guards against regressions.All 205 tests pass. Ruff, ruff format and pylint are clean on the changed files.
Not changed
legacy_decorators: true,TrigInfointrigger.py) and the legacytask.wait_untilre-check againsttime_nextrather thantime_next_adj. So they don't double-wait forcron(), but they still stallperiod()for the repeated hour. The same UTC deadline would fix them. I left them alone to keep this change small. Happy to add them here or in a follow-up.period()whose start time falls inside the repeated hour, such as a script loaded at 01:30 on the first pass, still waits for that start time to come round again after the change. That's a separate naive-time limitation oftimer_trigger_next.