Use day time in function nodes

Hi Java Script heroes :slight_smile: I have a really simple question but I just don't find any input on this in the forum. Here is what I would like to achieve: Within a function node, I would like to initiate different actions based on the time of day. Example: If time is between 9:00am and 7pm, activate Scene "Daylight" in living room. Would be nice if someone could show me a simple example how to do this that would be really nice :hugs:

You should read up on the date() function in JavaScript. A good place to start is
https://www.w3schools.com/js/js_dates.asp

A simple demo...

var now = new Date();
var hour = now.getHours();
if(hour >= 7 && hour < 19) {
  return msg;
}
return null; //halt flow, time must be before 7am or after 7pm

Alternative simple demo with 3 outputs (before 7am, between 7am~7pm, after 7pm)...

var now = new Date();
var hour = now.getHours();
if (hour < 7) {
  return [msg, null, null]; // it is before 7am - fire output 1
} else if(hour >= 7 && hour < 19 {
  return [null, msg, null] // it is between 7am~7pm - fire output 2
} else {
  return [null, null, msg];  // it must be after 7pm - fire output 3
}

Alternative: Using cron-plus to initiate the actions at whatever time you wish (graphically or programmatically - see built in demos & built in documentation)

As always, perfectly efficient answers from Mr. @Steve-Mcl :man_superhero:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.