From a4cdb294b1576edf4852c4dfa6f7d9f176dcaa43 Mon Sep 17 00:00:00 2001 From: karwosts <32912880+karwosts@users.noreply.github.com> Date: Sun, 9 Mar 2025 23:46:05 -0700 Subject: [PATCH] Fix some issues with energy period update scheduling (#24563) --- src/data/energy.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/data/energy.ts b/src/data/energy.ts index 0b375a6995..b60b299ae4 100644 --- a/src/data/energy.ts +++ b/src/data/energy.ts @@ -711,8 +711,10 @@ export const getEnergyDataCollection = ( ); scheduleUpdatePeriod(); }, - addHours(calcDate(now, endOfDay, hass.locale, hass.config), 1).getTime() - - Date.now() // Switch to next day an hour after the day changed + addHours( + calcDate(new Date(), endOfDay, hass.locale, hass.config), + 1 + ).getTime() - Date.now() // Switch to next day an hour after the day changed ); }; scheduleUpdatePeriod(); @@ -721,19 +723,19 @@ export const getEnergyDataCollection = ( collection.prefs = undefined; }; collection.setPeriod = (newStart: Date, newEnd?: Date) => { + if (collection._updatePeriodTimeout) { + clearTimeout(collection._updatePeriodTimeout); + collection._updatePeriodTimeout = undefined; + } collection.start = newStart; collection.end = newEnd; if ( collection.start.getTime() === calcDate(new Date(), startOfDay, hass.locale, hass.config).getTime() && collection.end?.getTime() === - calcDate(new Date(), endOfDay, hass.locale, hass.config).getTime() && - !collection._updatePeriodTimeout + calcDate(new Date(), endOfDay, hass.locale, hass.config).getTime() ) { scheduleUpdatePeriod(); - } else if (collection._updatePeriodTimeout) { - clearTimeout(collection._updatePeriodTimeout); - collection._updatePeriodTimeout = undefined; } }; collection.setCompare = (compare: boolean) => {