Script for Shelly Pro 2PM that overwrites a schedule

567 Views Asked by At

I am trying to create a script for Shelly Pro 2PM that overwrites the following schedule:

if (sunrise +15min) > 5am : Turn on @5am, Turn off @ (sunrise +15min), else: do not turn on @5am if (sunset -15min) < 7pm : Turn on @ (sunset - 15min), turn off @ 7pm else: do not turn on @ (sunset -15min)

I understand the script needs to be done in Espruino, which I tried, but I`m failing:

function getSunrise() {
  // Implement the logic to retrieve the sunrise time
  // and return it as a Date object
  // ...
}
function getSunset() {
  // Implement the logic to retrieve the sunset time
  // and return it as a Date object
  // ...
}
function turnOn() {
  // Implement the logic to turn on the device
  // ...
}
function turnOff() {
  // Implement the logic to turn off the device
  // ...
}
function scheduleJobs() {
  let sunrise = getSunrise();
  let sunset = getSunset();
  let turnOnTime = new Date();
  turnOnTime.setHours(5, 0, 0); // Set to 5:00 AM
  let turnOffTime = new Date(sunrise.getTime() + 15 * 60 * 1000); // Add 15 minutes to sunrise
  let sunsetTimeMinus15 = new Date(sunset.getTime() - 15 * 60 * 1000); // Subtract 15 minutes from sunset
  let turnOffTimeFixed = new Date();
  turnOffTimeFixed.setHours(19, 0, 0); // Set to 7:00 PM
  if (turnOffTimeFixed.getTime() > sunsetTimeMinus15.getTime()) {
    // Turn on @ (sunset - 15min), turn off @ 7pm
    let timeout = sunsetTimeMinus15.getTime() - Date.now();
    if (timeout > 0) {
      setTimeout(turnOn, timeout);
    } else {
      turnOn();
    }
    setTimeout(turnOff, 7 * 60 * 60 * 1000); // 7 hours after turn on
  } else if (turnOnTime.getTime() < sunrise.getTime() && sunrise.getTime() < turnOffTimeFixed.getTime()) {
    // Turn on @ 5am, turn off @ (sunrise + 15min)
    let timeout = turnOnTime.getTime() - Date.now();
    if (timeout > 0) {
      setTimeout(turnOn, timeout);
      setTimeout(turnOff, turnOffTime.getTime() - turnOnTime.getTime());
    } else {
      turnOn();
      setTimeout(turnOff, turnOffTime.getTime() - Date.now());
    }
  }
}
// Call scheduleJobs() to start scheduling the jobs
scheduleJobs();

When I enable the script and Start it, I get an error at line 31: let TurnOnTime - new Date();

Could anyone help me with a script for this, please?

Thank you in advance.

1

There are 1 best solutions below

1
On

I believe Shelly added support for Date object in Version !.0 Have you tried this version of Firmware