DMX Posted June 29 Share Posted June 29 Hey everyone, I've been using the "restore-schedule.js" script from library, and it works great for restoring the last active schedule when the Shelly device is powered back on after being cut off. However, I'm facing an issue where this only functions correctly if the device has been power cycled. I need to enhance the script to also check and restore the recent schedule that should be active even if the Shelly device stays powered on continuously. The scenario I'm dealing with is when the lights are turned on or off using a Wi-Fi app without cutting the power. In such cases, the script doesn't currently check or update the light state. Does anyone have suggestions or modifications to make the script respond to light state changes while the device remains connected to power? Thanks in advance for your help! Code: // This script will run once after device boot // will scan schedules that are set up for every day of the week // and will ensure that the scheduled task that was to be run // just before device booted is executed let CONFIG = { scheduleSpecMatch: '* * SUN,MON,TUE,WED,THU,FRI,SAT', }; let hour = null; let minutes = null; function restoreSchedule() { if (hour === null || minutes === null) return; //States - 0 - seconds, 1 - minutes, 2 - hours Shelly.call('Schedule.List', {}, function (result) { let best_diff = 1440; let scheduledRPC = null; for (let id in result.jobs) { if (result.jobs[id].timespec.indexOf(CONFIG.scheduleSpecMatch)) { let buf = ''; let s_seconds = 0, s_minutes = 0, s_hour = 0; let state = 0; for (let i = 0; i < result.jobs[id].timespec.length; i++) { if (result.jobs[id].timespec[i] !== ' ') { buf = buf + result.jobs[id].timespec[i]; } else { if (state === 0) { s_seconds = JSON.parse(buf); buf = ''; state = 1; } else if (state === 1) { s_minutes = JSON.parse(buf); buf = ''; state = 2; } else if (state === 2) { s_hour = JSON.parse(buf); break; } } } let c_diff = (hour - s_hour) * 60 + (minutes - s_minutes); if (c_diff > 0 && c_diff < best_diff && result.jobs[id].enable) { best_diff = c_diff; scheduledRPC = result.jobs[id].calls[0]; } } } if (scheduledRPC !== null) { console.log('Executing last active schedule before now...', JSON.stringify(scheduledRPC)); Shelly.call(scheduledRPC.method, scheduledRPC.params); } }); } Shelly.call('Sys.GetStatus', {}, function (status) { hour = JSON.parse(status.time.slice(0, 2)); minutes = JSON.parse(status.time.slice(3, 5)); restoreSchedule(); }); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.