How to sync timestamp value with machine time during runtime of node-red?

I have a project which involves RTC connected to Raspberry Pi. I update the RTC periodically like once a month or week if it drifts significantly / when I am moving to a new timezone ( travelling ) . The machine is designed to be completely off the Internet so I can't use NTP to sync time . I am using the exec nodes to set the user fed time and date (using timedatectl ) and I am syncing the local system time to my rtc , the problem is once its done the timestamp output of node-red doesn't change.

  1. Is there a way to dynamically sync timestamp time during run-time?
  2. what system time does time-stamp ( node-red ) use during init - local-time or universal-time?
  3. At worst case what is the easiest way to restart node-red for my time change to take effect

How are you displaying the system time and the node-red time to show they are not the same?

Node-RED uses node.js which should use the OS's time data. So if you need to change the time for the device running Node-RED, you need to work out how to change the system time. Since that is dynamic, Node-RED will follow.

Depends firstly on your OS. What does your OS say?

Then it depends on what functions you are using.

The general recommendation with any IT system is to work in Zulu time (aka UTC, aka GMT). That is because it avoids all of the messiness of transitions to/from daylight savings time and timezones. Only convert to local time when displaying to a real person and only convert from local to UTC when asking for input from a user. In each case, the local time will be local to the users browser and not your system (unless you are the user and you are using the same device as Node-RED of course).

You don't need to.

Thanks for the reply,, I am able to change the system time via exec node ( I am using a raspberry-pi ) . I use timedatectl command to change the system time to the one entered by the user via node-red gui. The problem is the system time gets changed, I am checking that via running timedatectl command in the terminal of my RPI, but the timestamp value that node-red gives is still the value that the system had before it had been changed. for example say the time is 2 hours slower than actual time in the system and I start node-red then , later the user changes the system time via node-red UI now the system time is proper time.. but the node-red time (timestamp value) is still 2 hours behind.

The OS I am using in Raspberry pi is RASPBERRY PI OS (32-bit) . Its a Linux (Debian) based OS .
yes I thought of working in UTC but since my system ( rpi ) will not be connected to internet , I have attached an RTC and I am syncing the RTC time to my local time each time to keep it in sync..

is there any way to make the timestamp of node-red sync again with the system clock ?

Hi , thanks for the reply..
I am using timemdatectl to display the system time, and I'm getting node-red time from timestamps node..

Do you mean an inject node set to send a timestamp? If so I think that is a known issue with that node that has an outstanding issue against it. I am not at my computer at the moment will check whether it is that later. In the meantime please feed an inject node into a debug node and show us what it shows, and tell us what it should show. Also tell us what timezone the node red server is set to and also the machine running the browser if that is a different machine.

I had this issue,
I just copy the content of

Later i changed this and made it a symbolic link.

Hi , Will this make node-red sync with the system time always? what exactly does your fix do?

I am no expert in debian, but this should sync the system time to your local.

Hi thanks for explaining , I don't have problem with handling my time in the system When I update my local time and restart node-red it fetches the proper time but if its running its the time that I get from node-red timestamp node that doesn't get updated when I change my system time. will syncing the local time with system time update the node-red time stamp values too?

You still haven't told us what you mean by 'timestamp node'? Do you mean an inject node, or a contrib node you have installed, or what?

I'm sorry for that, yea its the inject node I am using to get time from node-red..

You don't appear to have shown us the results of that test.
If that is not how you are showing the time to check it then also tell us how you are doing it.

Hi Colin, I am currently away from my house , I can share the actual results tomorrow..
I am taking the current time from timestamp (inject node ) and converting it into human readable form and displaying it in UI that's it.
when I start node-red both the machine time and the timestamps provided by node-red (via inject node ) are same, but when I change to machine time via timedatectl command the machine time changes as expected.. but the node-red still gives me the older time. I think node-red gets machine time at start and runs it own clock.. I want to know whether there is some way to sync this clock with my machine clock periodically or at worst case at least when I change my machine time. if nothing works I want to know a way to restart node-red automatically.,

The inject node turns into timestamp when we " select inject once after __ seconds option " , that's the reason why i was referring to as timestamps , my bad

Example Scenario

inject node ->> epoch time = A at start of node-red say 1605791434174

machine ->> epoch time = A at start of node-red say 1605791434174

I change machine time for some reason

inject node ->> epoch time = A at present ( ** " I want this value to be same as machine time " **) Example: 1605791434274

machine ->> epoch time = B according to value set by user Example: 1605791435274

OK, understood. I did try to replicate exactly that problem but have not succeeded so far. I have thought of something else to try.

Well I can't replicate it. Does this flow show the problem for you? One branch shows the time produced by an Inject node, and the other shows that from the javascript Date constructor, and when I change the system time using something like
sudo date +%T -s "17:15:00"
then both change to show the system time. Does this flow show the problem on your system? If so can you post a screenshot showing what it gives please and tell us what it should show?
If not then can you post a flow that does show the problem please?

[{"id":"7a51dad2.97d45c","type":"inject","z":"31fa50e9.6a1698","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":150,"y":600,"wires":[["1d53d758.918031","19149eda.8d3c09"]]},{"id":"1d53d758.918031","type":"function","z":"31fa50e9.6a1698","name":"new Date(msg.payload)","func":"msg.payload = new Date(msg.payload)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":350,"y":600,"wires":[["2744411a.290e26"]]},{"id":"2744411a.290e26","type":"debug","z":"31fa50e9.6a1698","name":"From inject","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":590,"y":600,"wires":[]},{"id":"19149eda.8d3c09","type":"function","z":"31fa50e9.6a1698","name":"new Date()","func":"msg.payload = new Date()\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":310,"y":680,"wires":[["745dd9da.406128"]]},{"id":"745dd9da.406128","type":"debug","z":"31fa50e9.6a1698","name":"From system","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":590,"y":680,"wires":[]}]
1 Like

Hi, I managed to get some screenshots to exactly show my problem

[{"id":"6197dbc7.793da4","type":"inject","z":"e10131f9.ffd3c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":210,"y":660,"wires":[["2866fc3.ff31204","c4a4d668.6e8458"]]},{"id":"2866fc3.ff31204","type":"moment","z":"e10131f9.ffd3c","name":"","topic":"","input":"payload","inputType":"msg","inTz":"Asia/Calcutta","adjAmount":0,"adjType":"days","adjDir":"add","format":"HH:mm","locale":"en-in","output":"payload","outputType":"msg","outTz":"Asia/Calcutta","x":520,"y":640,"wires":[["7b670309.1b296c"]]},{"id":"7b670309.1b296c","type":"ui_text","z":"e10131f9.ffd3c","group":"64eefd31.f05964","order":17,"width":16,"height":1,"name":"","label":"","format":"{{msg.payload}}","layout":"row-left","x":870,"y":640,"wires":[]},{"id":"c4a4d668.6e8458","type":"moment","z":"e10131f9.ffd3c","name":"","topic":"","input":"payload","inputType":"msg","inTz":"Asia/Calcutta","adjAmount":0,"adjType":"days","adjDir":"add","format":"dddd, DD MMMM","locale":"en-in","output":"payload","outputType":"msg","outTz":"Asia/Calcutta","x":520,"y":720,"wires":[["6e6f36f.c59e0c8","f4e1617f.01de"]]},{"id":"fa18163.3a903e8","type":"ui_text","z":"e10131f9.ffd3c","group":"64eefd31.f05964","order":21,"width":16,"height":1,"name":"","label":"","format":"<font size=50>{{msg.payload}}","layout":"row-left","x":870,"y":720,"wires":[]},{"id":"d226573d.a0b7f8","type":"ui_template","z":"e10131f9.ffd3c","group":"64eefd31.f05964","name":"","order":2,"width":1,"height":1,"format":"<style>\n\n  #Home_Default {\n  background: transparent;\n  border: transparent; \n  font-size: 100px;\n  font-family: Segoe UI Light;\n  \n}\n\n</style>\n","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":false,"templateScope":"local","x":720,"y":640,"wires":[[]]},{"id":"f4e1617f.01de","type":"ui_template","z":"e10131f9.ffd3c","group":"64eefd31.f05964","name":"","order":3,"width":1,"height":1,"format":"<style>\n\n\n  #Home_Default {\n  background: transparent;\n  border: transparent; \n  font-size: 150px;\n  font-family: Segoe UI Light;\n  \n}\n    \n</style>\n","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":false,"templateScope":"local","x":720,"y":720,"wires":[["fa18163.3a903e8"]]},{"id":"64eefd31.f05964","type":"ui_group","name":"Default","tab":"7fa2758c.04062c","order":1,"disp":false,"width":30,"collapse":false},{"id":"7fa2758c.04062c","type":"ui_tab","name":"Home","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

Even if I refresh the Dashboard page the time doesn't change..

I tried out the flow you gave @Colin and It worked like a charm ty :smiley:, I was able to observe the time change when I change the local sys time.. what could be wrong with my flow? I am using the same timestamp value to display time.

Did you realise the top template in the flow is not connected into the wires? However I don't think that is anything to do with the problem. Your flow seems to be working for me.

It is always best to simplify a problem down to the minimum number of nodes in order to work out where the problem is. Try this one. Capture the debug o/p before you change the time and after it in a single screenshot please, if possible. I don't need to see the command, just the debug showing the value from the moment node not changing.

[Edit] Oops, forget the flow:


[{"id":"6197dbc7.793da4","type":"inject","z":"77ba025c.6fa374","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":220,"wires":[["2866fc3.ff31204","e9266fc.4d3a19","b55b118e.e992d8"]]},{"id":"2866fc3.ff31204","type":"moment","z":"77ba025c.6fa374","name":"Time formatted","topic":"","input":"payload","inputType":"msg","inTz":"Asia/Calcutta","adjAmount":0,"adjType":"days","adjDir":"add","format":"HH:mm","locale":"en-in","output":"payload","outputType":"msg","outTz":"Asia/Calcutta","x":300,"y":180,"wires":[["729ee615.4cf0a8"]]},{"id":"729ee615.4cf0a8","type":"debug","z":"77ba025c.6fa374","name":"From moment","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":540,"y":180,"wires":[]},{"id":"e9266fc.4d3a19","type":"function","z":"77ba025c.6fa374","name":"new Date(msg.payload)","func":"msg.payload = new Date(msg.payload)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":280,"wires":[["42cc97ee.58291"]]},{"id":"42cc97ee.58291","type":"debug","z":"77ba025c.6fa374","name":"From inject","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":570,"y":280,"wires":[]},{"id":"b55b118e.e992d8","type":"function","z":"77ba025c.6fa374","name":"new Date()","func":"msg.payload = new Date()\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":290,"y":360,"wires":[["97c7f099.be3bb8"]]},{"id":"97c7f099.be3bb8","type":"debug","z":"77ba025c.6fa374","name":"From system","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":570,"y":360,"wires":[]}]

Yea this is part of a bigger system so the wire that ran across was for other purpose. I tried your flow and it works perfectly as I want it , but the node that I use to display in dashboard still displays the wrong time..

Update: I tried displaying the output from your flow to the Dashboard and it seems the output doesn't change even when the value from the debug has changed.. Could this be a Dashboard UI issue? I tried refreshing the Dashboard but it didn't help

OK, now we are getting somewhere, so the problem is nothing to do with the timestamp, but a problem getting the data to the dashboard. Hence the importance of analysing a problem down to to the individual node. Did you see my comment that the template node is not in the flow at all. Move it and you will see what I mean.

What version of node-red, the dashboard and nodejs are you using? The node-red version is shown at the bottom of the hamburger menu dropdown and you can get the dashboard version from Manage Palette. Run this command to get the nodejs version
node -v

[Edit] Also what version of the moment node?