From 64df879f2b1e548c30e1ac9de452e34da03a5ba5 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 31 May 2026 03:01:51 -0700 Subject: [PATCH] fix: align hourly automation start times (#4059) Avoid returning off-minute hourly dtstart values as the next automation occurrence. --- src/shared/automation-schedules.test.ts | 10 ++++++++++ src/shared/automation-schedules.ts | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/shared/automation-schedules.test.ts b/src/shared/automation-schedules.test.ts index f6f269a00..104af2eab 100644 --- a/src/shared/automation-schedules.test.ts +++ b/src/shared/automation-schedules.test.ts @@ -32,6 +32,16 @@ describe('automation schedules', () => { expect(latest).toBe(new Date('2026-05-13T14:00:00').getTime()) }) + it('does not return a future hourly dtstart that is off the scheduled minute', () => { + const rrule = buildAutomationRrule({ preset: 'hourly', hour: 9, minute: 0 }) + const next = nextAutomationOccurrenceAfter( + rrule, + new Date('2026-05-13T10:30:00').getTime(), + new Date('2026-05-13T09:00:00').getTime() + ) + expect(next).toBe(new Date('2026-05-13T11:00:00').getTime()) + }) + it('computes weekday schedules without returning weekend candidates', () => { const rrule = buildAutomationRrule({ preset: 'weekdays', hour: 9, minute: 30 }) const next = nextAutomationOccurrenceAfter( diff --git a/src/shared/automation-schedules.ts b/src/shared/automation-schedules.ts index 5a477163d..b83e414d3 100644 --- a/src/shared/automation-schedules.ts +++ b/src/shared/automation-schedules.ts @@ -541,10 +541,10 @@ export function nextAutomationOccurrenceAfter( const base = new Date(start) base.setMinutes(rule.byMinute, 0, 0) let candidate = base.getTime() - if (candidate <= after) { + if (candidate <= after || candidate < dtstart) { candidate += HOUR_MS } - return Math.max(candidate, dtstart) + return candidate } const candidate = scanDayCandidates(rule, Math.max(dtstart - 1, after), 1) if (candidate === null) {