node-red-nodes
node-red-nodes copied to clipboard
node-red timeswitch sunrise in UTC despite TZ
Which node are you reporting an issue on?
timeswitch
What are the steps to reproduce?
Configure a timeswitch node leaving the Time On as the default 'Sunrise' where I live and deploy (I know this must my local issue but...)
What happens?
The node says "off until time", where `time' seems to be Zulu time for sunrise but is instead in my timezone (UTC-4).
What do you expect to happen?
The node should be on since time -4 hours.
Please tell us about your environment:
- [x] Node-RED version: v2.2.2
- [x] node.js version: v14.18.2
- [x] npm version: 6.14.15
- [x] Platform/OS: Linux 5.15.0-27-generic x64 LE (docker/docker-compose running on Ubuntu 22.04)
- [x] Browser: Firefox 99.01
As suggested in #640 I pass the TZ variable in the docker-compose.yaml file, and I can echo $TZ inside the container with the correct result of America/Guyana. https://github.com/node-red/node-red-nodes/issues/177#issuecomment-185309456 suggests there's a way to make node-red print what it thinks Dawn/Dusk are, but I've set logging to "debug" in settings.js and still don't see it.
I think this screenshot captures my issue:

- On the left, the top timeswitch node says
off until 09:40. The Properties panel is shown for this node is displayed in the center panel withOn Sunrise, my correct timezone, Lat/Lon, and no Offsets. - On the left again, the second timeswitch node shows
on until 09:15. This is configured to be on at 09:00, off at 09:15. The Debug output panel on the right shows the output for this node at 09:08:13 (2nd entry) with a payload of1, indicating thattimeswitchknows what time it is here and is working with it. - On the left again, the
Sun rise/setnode is from suncalc. According to my reading of the documentation, this node and timeswitch both use thenpm suncalcmodule. The Debug panel on the right shows the output of this node at 09:08:12 localtime (1st entry) with 'start' as09:40:00.000Zindicating sunrise at 09:40 UTC, which is 05:40 localtime. The payload is1and the node showsdayat the bottom.
So, I think the first timeswitch node should be On since 05:40 localtime, but timeswitch isn't correcting the suncalc reported value for my timezone..
i am also having the same problem, it looks like it is somethign to do with the way timeswitch and the suncalc library and the naitive JA date objects handle timezones (or dont handle them as the case may be).
i tried to debug it but i cant for the life of me work out how the code here is meant to work. so i gave up on it and rewrote it to remove timezone support alltogether.
if you want to use this workaround find the timeswitch.js file (this will depend on your install, for my docker it was in the container at /data/node_modules/node-red-node-timeswitch/) and replace linse 47 throu 66 (inclusive) with the code below
var now = new Date();
var today = now.getHours()*60 + now.getMinutes();
var starttime = Number(node.startt);
var endtime = Number(node.endt);
if ((starttime >= 5000) || (endtime == 5000) || (endtime == 6000)) {
var times = SunCalc.getTimes(now, node.lat, node.lon);
var dawn = times[node.start].getHours()*60 + times[node.start].getMinutes() + Number(node.dawnoff);
var dusk = times[node.end].getHours()*60 + times[node.end].getMinutes() + Number(node.duskoff);
if (starttime == 5000) { starttime = dawn; }
if (starttime == 6000) { starttime = dusk; }
if (endtime == 5000) { endtime = dawn; }
if (endtime == 6000) { endtime = dusk; }
node.debug("Dawn " + parseInt(dawn / 60) + ":" + dawn % 60 + " - Dusk " + parseInt(dusk / 60) + ":" + dusk % 60);
}
this will eliminate all refrances to the timezone selector and do all calculations in the server's timezone.
p.s. this will also fix the debug output to print based on the log setting not the verbose flag that is a pain to set in docker (it looks like this flag is being migrated away from anyway - see https://github.com/node-red/node-red/pull/3300)
also for debugging it would help a lot to know how it is meant to handle having sunrise/sunset and a time with a diffrent timezone. if you have 2 time entries then it esentualy just check the time against the time in the set time zone. but should the sun time be converted then checked or just checked as if it was a manuly entered time?
@pmacostapdi any input on how this should be handled?
Not sure what is happening here. In this node it uses correctly 'new Date()' for determining the now, but it returns a time with two hours difference (UTC probably) in the msg. If I use the 'new Date()' in a function node, it returns the correct time (Central European) as is configured on the raspberry pi it is running on.