Using the time in node red

Hi all, Ive been playing with node red for a while to set up to home automation. Its all going well and is up and running but I noticed this morning things happening at an unexpected time and I found this is due to the time in node red not being the same time zone as the raspberry pi that its running on.

I found the following discussion and followed the guide in the last post, I have added
process.env.TZ = "Europe/London";

to the settings.js file and this didnt resolve it.

I have copied the function to a simple page to post here. Hopefully this is formatted correctly.

Thanks

[{"id":"52175ebb.b4914","type":"tab","label":"Time Test","disabled":false,"info":""},{"id":"6bbeb4e0.742adc","type":"function","z":"52175ebb.b4914","name":"time","func":"var now = msg.now;\n\nvar dateTime = new Date(now).toLocaleString();\nvar dateTimeStamp = new Date(now);\n\nmsg.r = {\n time_locale : dateTime,\n time_timestamp : dateTimeStamp\n}\nreturn msg;","outputs":1,"noerr":0,"x":550,"y":300,"wires":[["bd43b9f6.9e6fa8"]]},{"id":"58325b00.b3d8a4","type":"inject","z":"52175ebb.b4914","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":300,"wires":[["5bd53355.d3c3fc"]]},{"id":"bd43b9f6.9e6fa8","type":"debug","z":"52175ebb.b4914","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":730,"y":300,"wires":[]},{"id":"5bd53355.d3c3fc","type":"change","z":"52175ebb.b4914","name":"timestamp","rules":[{"t":"set","p":"now","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":300,"wires":[["6bbeb4e0.742adc"]]}]

Also I have just checked the settings.js file again and I added that line right at the end outside of the moduel.exports obect. I dont know if thats the correct place for it to go.

And this is the output I get from the time stamps

{
"time_locale":"10/16/2019, 9:28:01 AM",
"time_timestamp":"2019-10-16T08:28:01.624Z"
}

So currently in the UK there is British Summer Time.
So the time locale is 9 something
and the Zulu time (UTC/GMT) is 8 something.
(the Z at the end indicates it is UTC)

What did you expect to see?

I worked out that that was the issue, but I dont know how to set up the zone correctly in node red. I thought it would take the time from the pi itself but the time in the pi is correct.

If I take a now timestamp from inside node red and make a date object out of it, its an hour behind.

How do I correct the time zone so that the now timestamp is accurate?

What makes you think it is wrong? The two times you posted are the same time, 9:28 local time and 8:28 UTC are just different ways of displaying the same time.

node red thinks its an hour earlier than it is, so the schedule using this time starts and stops an hour earlier than it is set to.

I think I have misunderstood the timestamp, it is a time since the start of the epoch, so that wont change based on locale. I think thats where Im getting confused. Im looking at a representation of that time based on locale and seeing the difference but it isnt incorrect.

I want the to use the local time, not the unix time, so I will need to convert it first and then make use of it. I think....

What do you want to do with the time?

Its going into a schedule function to set an occupancy state.

I have worked around it now. Ive checked the Date object for a timezone offset, if it exists it will offset the schedule. This is all working by the look of things. I'm using schedex module which has offsets available to modify. I have it working now, and have tested with some fixed dates at GMT and BST and not seeing any errors with it.

The schedex node (I presume you mean node-red-contrib-schedex) should know about the local time zone and any times you enter in the config are expected to be in local time, if it isn't then something odd is going on. Are you sure the time on the pi is correct? What does
date
show?

It shows the correct local time in BST. I rememeber being very thorough to make sure this was correct before I started using node red, knowing that the time was going to be a part of what I wanted to use.

What exactly does date show and what is the problem you are having with the schedex node? Export a mini flow here showing the problem if possible.

[Edit] Also what version of nodejs are you using? Run node -v to find out. In fact if you post the output you get when you start node red in a terminal that might be helpful.

in the flow function called 'time' I manually set the offset variable to 0 to simulate GMT or set a fixed date in dt.

pi@raspberrypi:~ $ date
Wed 16 Oct 11:39:34 BST 2019
pi@raspberrypi:~ $ node -v
v10.15.3
[{"id":"52175ebb.b4914","type":"tab","label":"Time Test","disabled":false,"info":""},{"id":"1e252ea2.2d5891","type":"inject","z":"52175ebb.b4914","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":300,"wires":[["f1ee5f0.0ed11a"]]},{"id":"f1ee5f0.0ed11a","type":"function","z":"52175ebb.b4914","name":"time","func":"var now = new Date();\nvar offset = now.getTimezoneOffset();\n//offset = 0;\n\nif (offset !== 0){\n    offset = Math.abs(offset);\n}\n\nmsg.payload = {\n    onoffset : offset,\n    offoffset : offset\n}\n\nmsg.now = now.getTime();\n\nreturn msg;","outputs":1,"noerr":0,"x":490,"y":300,"wires":[["d989ed91.8ad29","b0c1d50.9e6ff28"]]},{"id":"d989ed91.8ad29","type":"debug","z":"52175ebb.b4914","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":650,"y":300,"wires":[]},{"id":"f4d3ac93.0baea","type":"inject","z":"52175ebb.b4914","name":"","topic":"","payload":"info","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":490,"y":200,"wires":[["b0c1d50.9e6ff28"]]},{"id":"31172db9.2cc632","type":"inject","z":"52175ebb.b4914","name":"","topic":"","payload":"off","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":490,"y":160,"wires":[["b0c1d50.9e6ff28"]]},{"id":"9dcfd8b0.78a708","type":"inject","z":"52175ebb.b4914","name":"","topic":"","payload":"on","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":490,"y":120,"wires":[["b0c1d50.9e6ff28"]]},{"id":"b0c1d50.9e6ff28","type":"schedex","z":"52175ebb.b4914","name":"occupation","suspended":false,"lat":"0","lon":"0","ontime":"06:00","ontopic":"","onpayload":"true","onoffset":0,"onrandomoffset":0,"offtime":"21:30","offtopic":"","offpayload":"false","offoffset":0,"offrandomoffset":0,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"sun":true,"x":770,"y":160,"wires":[["27d32f62.f970c"]]},{"id":"27d32f62.f970c","type":"debug","z":"52175ebb.b4914","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":940,"y":100,"wires":[]}]

Can you post a flow that doesn't do what you expect? To investigate a possible bug it is necessary to have a failing condition.
I am also in the UK and I just changed the Off time in the schedex node in your flow to 12:03 and let it run without clicking any of the inject nodes and it triggered at 12:03 local time as expected.
Are you using the latest version of schedex (1.6.2)?

Ive looked a bit further and found that the schedex module is outputting a time that is an hour behind, but does actually work when it is set to. Something else occurred unrelated to the schedule, but I thought it was the schedule because it happened an hour earlier than expected.

Im logging a few more things to keep track of everything but thanks for the help.

I don't understand what you mean by that. Do you mean that when you send it "info" in the payload the information it emits shows the schedule times in GMT? If so then that is exactly what it says it should do in the info tab for the node. So for the example I mentioned above where I have configured it to trigger at 12:03 local time (UTC+1) it shows "Fri, 18 Oct 2019 11:03:00 GMT" which is correct. If you want it to send it in local time then you can use "info_local" which for that example shows "2019-10-18T12:03:00.000+01:00" which is also correct.

I totally missed that. So it is all good, and I see the correct time if I send info_local.

I jumped to a conclusion about what the issue was and started looking in the wrong place. But anyway, learned a bit more and found the issue elsewhere.

Thanks

I have to point out that you were seeing the correct time even when you asked for info rather than info_local. 11:03:00 GMT is exactly the same time as 12:03:00.000+01:00, it is just displayed in a different way.

Yes I did realise that eventually. A made the silly mistake of making assumptions before fully understanding what I was looking at so went down the wrong path straight away.

hello my friend can i ask you how did you get time and date to a text node using a function node when working on raspberry pi ? thanks.