I've built my own scheduler in node-red for controlling stuff around the house.
The basic data structure is an array of entries like:
[actionID, time, recurrence, parameters]
[10, 1648395000000, 86400000,{which:"fish", state:"on"}]
Above should turn on the fish tank light at 16:30 every day.
The time is stored as MS since 1-1-1970 00:00:00 UTC.
Today the clocks here in London went forward to summer time; but all my events need to respect local time.
I can fudge the schedule by adding 60000 onto all the times (then taking it off in 6 months time); but I'd like to do the job properly. So if you schedule something at 22:00 it happens at 22:00 all year long.
Any suggestions of how best to implement this?
You can use
const offset = new Date().getTimezoneOffset()
Which will give you the current timezone offset in minutes. You can use that to offset the time you are comparing against.
I found using "hh:mm", not timestamps much easier to deal with when constructing daily schedules.
Schedule:
[ {"id":0,"to":"02:00","temp":19.5}, {"id":1,"to":"10:00","temp":20.5}, ...] //id not essential if array is always sorted
function getTemp(sched) {
//return the scheduled temp for this time of day
const now = moment();
let test = moment();
return sched.find(el => {
const hhmm = el.to.split(":");
//change midnight this morning (00:00) to midnight tonight (23:59:59)
if (el.to == "00:00") test.hour(23).minute(59).second(59);
else test.hour(hhmm[0]).minute(hhmm[1]);
return now.isSameOrBefore(test, "second");
}).temp;
}
This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.