Converting unixtimestamp to day of the week

  • I am currently working on a fail safe system in case the Shelly (Shelly PRO 4PM) loses connection to the MQTT-Broker.

    The fail safe does work so far and i am also getting the unixtimestemp from the shelly. But what i still need is the day of the week from the unixtimestamp.

    I need the day of the week because i want the script to change the status of the switches in regards to the time of the day and the day of the week.

    Code
    Shelly.call(
        "Shelly.GetStatus",
        {},
        function (response) {
          h = response.sys.unixtime;
          dateObject = new Date(h*1000;)
          humanDate = dateObject.getDay();
         },
    );

    The code above works fine until the "new Date" function. I know from the shelly doc that the "new" keyword doesn't work, so i am not surprised my code as a whole doesn't work.

    Is there a workaround or something i am missing?

    The "new Date" function is the only thing i've found in regards to transforming unix in JavaScript.

  • 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:

    Mastering Date and Time Calculation
    Forms rely on the Date Picker element a lot - whether you're: Assessing turnaround times of projectsDealing with registrationsHandling...
    www.jotform.com

    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!

  • Thank you for the help.

    I had other, more important stuff to do, which is way i am only answering now.

    Fortunately a complet loss of power is rare. So my FailSafe is in case of a loss of MQTT or in other words, when you can't controll him anymore remote.

    I have managed, with your links, to get the current day (Weekday). The Code looks like follows:

    In theory, it should also give me the correct month and correct day. Unfortunately the month is of by 1 and the day of the month is of by 2, meaning as of now the print looks like this:

    "Monday , 16 . 6 . 2022 12 : 24"

    My idea is, it has something to do with leap years but as of now i have no solution yet but i will keep looking for one and i will post it here once i have one.

    2 Mal editiert, zuletzt von andsta (26. Juli 2022 um 11:45) aus folgendem Grund: In the code Sunday is 0 not 7 if (dayofweek === 0) { day = "Sunday"; 2nd edit: created all variables outside of the timer, only changed them within the timer (no use of let within the timer) reason: you must create each variable first with let, if you define them in the timer or in an if cause, they will only exist within them and won't be usable in other parts of the script.