Ahh gotcha. I just followed this topic, which is very detailed write up of JS Time of Day: Migrating DSL Rules to JSR223/Javascript with a case example
This is all still a work in progress FYI.
My version:
JSRule({
name: "Time of day",
description: "Calculates the time of day, depending on several triggers",
triggers: [
ChannelEventTrigger("astro:sun:local:rise#event", "START", "astroSunriseTrigger"),
ChannelEventTrigger("astro:sun:local:noon#event", "START", "astroNoonTrigger"),
ChannelEventTrigger("astro:sun:local:set#event", "START", "astroSunsetTrigger"),
ChannelEventTrigger("astro:sun:minus90:set#event", "START", "astroSunsetDelayTrigger"),
TimerTrigger("0 1 0 * * ? *"), // one minute after midnight so give Astro time to calculate the new day's times
TimerTrigger("0 0 8 * * ? *"),
TimerTrigger("0 0 23 * * ? *")
],
execute: function( module, input){
// Get the current time as number
var now = new Date().getTime();
//Get the vTimeOfDay item as json object
var currentTimeOfDay = getItem("vTimeOfDay");
logInfo("Calculating time of day...");
// Calculate the times for the static times
var morning_start = new Date().setHours(7, 59, 59, 0); // .setHours(Hour, Minute, Second, Millisecond)
var bed_start = new Date().setHours(0, 0, 0, 0);
// Get the astro calculated times as numbers for comparison
var day_start = new Date(getItem("vSunrise_Time").state).getTime();
var noon_start = new Date(getItem("vNoon_Time").state).getTime();
var afternoon_start = new Date(getItem("vEvening_Time").state).getTime();
var evening_start = new Date(getItem("vSunset_Time").state).getTime();
// Calculate the current time of day
var curr = "UNKNOWN"
if(now > bed_start) curr = "BED";
if(now > morning_start) curr = "MORNING";
//if(now > day_start) curr = "DAY";
if(now > noon_start) curr = "NOON";
if(now > afternoon_start) curr = "AFTERNOON";
if(now > evening_start) curr = "NIGHT";
// Publish the current state
if(currentTimeOfDay.state != curr) {
logInfo("Current time of day is now " + curr);
sendCommand(currentTimeOfDay, curr);
}
}
});
And here is a rule based on update to the vTimeOfDay Event. IIRC, there is no “noon” in the Time of Day and that is something I added.
JSRule({
name: "JM Blinds and Curtains Control",
description: "Opens + Closes on Schedule",
triggers: [
CommandEventTrigger("vTimeOfDay", null, "TOD_Trigger")
],
execute: function (module, input) {
var currentTimeOfDay = items["vTimeOfDay"].toString();
if (items["JM_Sleeping_In"] !== ON) {
switch (currentTimeOfDay) {
case 'BED':
sendCommand("JohnSBlinds_", 0);
sendCommand("JM_Curtains_CommandString", "CLOSE");
logInfo("Blinds Control - Bed Complete");
break;
case 'MORNING':
sendCommand("JohnSBlinds_", 85);
sendCommand("JM_Curtains_CommandString", "OPEN");
logInfo("Blinds Control - Morning Complete");
break;
case 'NOON':
sendCommand("JohnSBlinds_", 30);
logInfo("Blinds Control - Noon Complete");
break;
case 'AFTERNOON':
sendCommand("JohnSBlinds_", 50);
logInfo("Blinds Control - Afternoon Complete");
break;
case 'NIGHT':
sendCommand("JohnSBlinds_", 85);
logInfo("Blinds Control - Night Complete");
break;
default:
logInfo(currentTimeOfDay + " - I did nothing.");
return;
}
logInfo("Blinds Control - vTime is " + currentTimeOfDay);
return;
} else {
logInfo("John is sleeping in.");
}
}
});