diff --git a/lib/sandbox.js b/lib/sandbox.js index f32c7f10b..3332c50b8 100644 --- a/lib/sandbox.js +++ b/lib/sandbox.js @@ -1638,6 +1638,7 @@ function sandBox(script, name, verbose, debug, context) { } // ensure events are calculated independent of current time + // TODO: use getAstroStartOfDay of adapter? const todayNoon = new Date(nowdate); todayNoon.setHours(12, 0, 0, 0); let ts = mods.suncalc.getTimes(todayNoon, adapter.config.latitude, adapter.config.longitude)[pattern.astro]; @@ -1658,12 +1659,12 @@ function sandBox(script, name, verbose, debug, context) { pattern.astro === 'nightEnd' || pattern.astro === 'nauticalDusk' ) { - ts.setMinutes(59); ts.setHours(23); + ts.setMinutes(59); ts.setSeconds(59); } else { - ts.setMinutes(59); ts.setHours(23); + ts.setMinutes(59); ts.setSeconds(58); } } @@ -1688,13 +1689,15 @@ function sandBox(script, name, verbose, debug, context) { sandbox.log(`More than ${sandbox.__engine.__schedules} schedules registered. Check your script!`, 'warn'); } + sandbox.verbose && sandbox.log(`schedule(astro=${pattern.astro}, offset=${pattern.shift}) is tomorrow, waiting until ${date.toISOString()}`, 'info'); + // Calculate new schedule in the next day sandbox.setTimeout(() => { if (sandbox.__engine.__schedules > 0) { sandbox.__engine.__schedules--; } sandbox.schedule(pattern, callback); - }, date.getTime() - nowdate.getTime()); + }, date.getTime() - Date.now()); return; } @@ -1719,9 +1722,9 @@ function sandBox(script, name, verbose, debug, context) { sandbox.schedule(pattern, callback); }, 2000); - }, ts.getTime() - nowdate.getTime()); + }, ts.getTime() - Date.now()); - sandbox.verbose && sandbox.log(`schedule(astro=${pattern.astro}, offset=${pattern.shift})`, 'info'); + sandbox.verbose && sandbox.log(`schedule(astro=${pattern.astro}, offset=${pattern.shift}) is today, waiting until ${ts.toISOString()}`, 'info'); } else { // fix a problem with sunday and 7 if (typeof pattern === 'string') { diff --git a/main.js b/main.js index 596de41a6..e005edc2d 100644 --- a/main.js +++ b/main.js @@ -1474,13 +1474,14 @@ async function sunTimeSchedules(adapter, context) { }, native: {}, }); - await adapter.setStateAsync(objId, { val: timeFormatted, c: calcDate.toISOString(), ack: true }); + await adapter.setStateAsync(objId, { val: timeFormatted, c: times[t].toISOString(), ack: true }); } const todayDate = new Date(); todayDate.setHours(0); todayDate.setMinutes(0); todayDate.setSeconds(1); + todayDate.setMilliseconds(0); todayDate.setDate(todayDate.getDate() + 1); adapter.log.debug(`[sunTimeSchedules] Next: ${todayDate.toISOString()}`);