This question is specifically about the node-red-contrib-time-range-switch node. I've successfully implemented it to control a flow restricted to a time range (I'm on the USA east coast). I've set the range to 7:30 (AM) to 22:00 . Seemed to work fine at first, but then I see that sometime during the afternoon or early evening, the date has cycled to the next day. So, when I checked it at about 9PM (21:00) today, the node thinks it's filtering from 7:30 to 22:00 on the following day.
I was presuming it would base its time on my Windows system clock. No mention anywhere about where any of the available time-related nodes get their time from. So, how do I get around this? If it's running off GMT, I just need to know that.
Thanks
I think the node use moment() to get the current time, which is the local time
Please correct me if i'm wrong
Please read:
* `moment(...)` is local mode. Ambiguous input (without offset) is assumed to be local time. Unambiguous input (with offset) is adjusted to local time.
If the end time has already passed, it shifts the entire window to the next day.
i changed my local timezone with sudo dpkg-reconfigure tzdata
, but it doesen't affect the ouput of the node , so i can't reproduce it.
For more control what happen, i asked chatgpt to wirte me a simple function... et voilà
function isNowBetween(startTimeStr, endTimeStr) {
const now = new Date();
const nowMinutes = now.getHours() * 60 + now.getMinutes();
const [startHours, startMinutes] = startTimeStr.split(':').map(Number);
const [endHours, endMinutes] = endTimeStr.split(':').map(Number);
const startTotalMinutes = startHours * 60 + startMinutes;
const endTotalMinutes = endHours * 60 + endMinutes;
if (startTotalMinutes <= endTotalMinutes) {
// Normal range (e.g., 08:00–17:00)
return nowMinutes >= startTotalMinutes && nowMinutes < endTotalMinutes;
} else {
// Overnight range (e.g., 22:00–06:00)
return nowMinutes >= startTotalMinutes || nowMinutes < endTotalMinutes;
}
}
msg.payload = isNowBetween("05:00", "19:00")
node.send(msg)
msg.payload = isNowBetween("19:00", "05:00")
node.send(msg)
return null;
maybe this serve the purpose?