Skip to content

fix: don't wait an extra hour in time triggers when the clocks go back - #908

Open
benbraun wants to merge 1 commit into
custom-components:masterfrom
benbraun:fix-time-trigger-dst-fallback
Open

benbraun wants to merge 1 commit into
custom-components:masterfrom
benbraun:fix-time-trigger-dst-fallback

Conversation

@benbraun

Copy link
Copy Markdown

Fixes #649.

When the clocks go back, @time_trigger loses an hour. A period() trigger stops for an hour, and a cron() trigger whose next run crosses the change fires an hour late. I hit both on 2026-10-25 (Europe/Berlin) on 2.1.0. A period(now, 5s) trigger went quiet from 02:59:44 CEST to 03:00:14 CET. A cron(*/5 * * * *) trigger went from 02:55 CEST straight to 04:00 CET instead of 03:00 CET.

Cause

TimeTriggerDecorator._cycle gets time_next_adj from timer_trigger_next. That value already accounts for the DST change, so time_next_adj - now is 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, but time_next_adj is 02: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, now is the repeated local time, so period() keeps its interval through the repeated hour.

cron() behaviour across the change is otherwise unchanged. As the existing DST tests expect, */5 still doesn't fire during the repeated hour itself. It now fires once the clocks reach 03:00 CET.

Tests

tests/test_time_trigger_dst.py runs the cycle with a fake clock whose local reading follows America/Los_Angeles, like the existing DST tests. asyncio.sleep advances 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

  • The legacy trigger path (legacy_decorators: true, TrigInfo in trigger.py) and the legacy task.wait_until re-check against time_next rather than time_next_adj. So they don't double-wait for cron(), but they still stall period() 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.
  • A 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 of timer_trigger_next.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Possible problems with time triggers and daylight saving time transition

1 participant