Hi Andsta,
I am trying the same thing as in my country the power goes out all the time and if the Shelly is off while the cron scheduler is supposed to run, it comes on in the incorrect state. This also means that cell towers can go down. So what I am trying to get too is a script that continues to run on a timer and checks schedule and what state it is supposed to be in and corrects the state if needed.
The version of mJS they are running on the Shelly is very limited and does not have any date functions built in. You will have to use Math.floor to do the calculation. Here is a website to help you get the time out of the unix timestamp:
And here is a post on how to get the day of the week out of the unix timestamp:
https://stackoverflow.com/questions/3638…0on%20T%20first.)
Just in case, remember that Mod is % .
You are saying that you are receiving the timestamp via MQTT. What I tried first was using Schedule.List to get the programmed schedules and then do a comparison on that. I wanted to run the device independently. First problem for me was that if you have different schedules, E.g. some run in the morning only, some run at 30 past the hour, they run on different days, all these are then different strings you receive back:
0 30 8 * * SUN,WED
0 0 8 * * MON,TUE,THU,FRI
0 30 7 * * SAT
Now my coding skills are not at the level that I can go and decrypt each schedule string and use that to verify switch state, so I created a schedule in the script that uses floats to compare to the timestamp I created using the Math.floor as mentioned above. This does mean the schedule is hardcoded into the script, but this can at least be update remotely if needed.
E.g.
let mon_on = 8.00;
let mon_off = 18.00;
let tue_on = 8.30;
let tue_off = 18.00;
This runs very successfully if stable internet, but there is still a bug I need to figure out. The Shelly does not persist Time. So, when a Shelly powers off and comes back on it starts up at midnight UTC. Only once it has internet connection and calls the sntp server, then the time is updated and the schedule can actually work again. But, until then, the schedules don't work.
So, unfortunately, the devices are dependent on internet and cannot run truly on their own yet. Hopefully someone has an idea how to persist Time?
Good luck with your project!