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

**URL:** <https://discourse.nodered.org/t/how-to-sync-timestamp-value-with-machine-time-during-runtime-of-node-red/35980>\
**Category:** General\
**Created:** [16 November 2020 03:49 UTC](https://discourse.nodered.org/t/how-to-sync-timestamp-value-with-machine-time-during-runtime-of-node-red/35980 "2020-11-16T03:49:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![madironman](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@madironman](https://discourse.nodered.org/u/madironman)\
**Post date:** [16 November 2020 03:49 UTC](https://discourse.nodered.org/t/how-to-sync-timestamp-value-with-machine-time-during-runtime-of-node-red/35980/1 "2020-11-16T03:49:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [16 November 2020 09:32 UTC](https://discourse.nodered.org/t/how-to-sync-timestamp-value-with-machine-time-during-runtime-of-node-red/35980/2 "2020-11-16T09:32:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [17 November 2020 00:02 UTC](https://discourse.nodered.org/t/how-to-sync-timestamp-value-with-machine-time-during-runtime-of-node-red/35980/3 "2020-11-17T00:02:00Z")

</div>

> [@madironman](#):
>
> Is there a way to dynamically sync timestamp time during run-time?

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.

> [@madironman](#):
>
> what system time does time-stamp ( node-red ) use during init - local-time or universal-time?

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).

> [@madironman](#):
>
> At worst case what is the easiest way to restart node-red for my time change to take effect

You don't need to.

---

<div class="post-metadata">

**Author:** ![madironman](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@madironman](https://discourse.nodered.org/u/madironman)\
**Post date:** [19 November 2020 05:58 UTC](https://discourse.nodered.org/t/how-to-sync-timestamp-value-with-machine-time-during-runtime-of-node-red/35980/4 "2020-11-19T05:58:55Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![madironman](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@madironman](https://discourse.nodered.org/u/madironman)\
**Post date:** [19 November 2020 06:00 UTC](https://discourse.nodered.org/t/how-to-sync-timestamp-value-with-machine-time-during-runtime-of-node-red/35980/5 "2020-11-19T06:00:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 November 2020 07:26 UTC](https://discourse.nodered.org/t/how-to-sync-timestamp-value-with-machine-time-during-runtime-of-node-red/35980/6 "2020-11-19T07:26:15Z")

</div>

> [@madironman](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [19 November 2020 07:43 UTC](https://discourse.nodered.org/t/how-to-sync-timestamp-value-with-machine-time-during-runtime-of-node-red/35980/7 "2020-11-19T07:43:36Z")

</div>

I had this issue,  
I just copy the content of  
/usr/share/zoneinfo/myregion/mycity  
inside  
/etc/localtime

Later i changed this and made it a symbolic link.

---

<div class="post-metadata">

**Author:** ![madironman](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@madironman](https://discourse.nodered.org/u/madironman)\
**Post date:** [19 November 2020 08:17 UTC](https://discourse.nodered.org/t/how-to-sync-timestamp-value-with-machine-time-during-runtime-of-node-red/35980/8 "2020-11-19T08:17:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [19 November 2020 08:32 UTC](https://discourse.nodered.org/t/how-to-sync-timestamp-value-with-machine-time-during-runtime-of-node-red/35980/9 "2020-11-19T08:32:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![madironman](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@madironman](https://discourse.nodered.org/u/madironman)\
**Post date:** [19 November 2020 08:39 UTC](https://discourse.nodered.org/t/how-to-sync-timestamp-value-with-machine-time-during-runtime-of-node-red/35980/10 "2020-11-19T08:39:03Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 November 2020 10:05 UTC](https://discourse.nodered.org/t/how-to-sync-timestamp-value-with-machine-time-during-runtime-of-node-red/35980/11 "2020-11-19T10:05:43Z")

</div>

> [@madironman](#):
>
> the time that I get from node-red timestamp node

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?

---

<div class="post-metadata">

**Author:** ![madironman](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@madironman](https://discourse.nodered.org/u/madironman)\
**Post date:** [19 November 2020 10:08 UTC](https://discourse.nodered.org/t/how-to-sync-timestamp-value-with-machine-time-during-runtime-of-node-red/35980/12 "2020-11-19T10:08:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 November 2020 10:59 UTC](https://discourse.nodered.org/t/how-to-sync-timestamp-value-with-machine-time-during-runtime-of-node-red/35980/13 "2020-11-19T10:59:14Z")

</div>

> [@Colin](#):
>
> please feed an inject node into a debug node and show us what it shows, and tell us what it should show.

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.

---

<div class="post-metadata">

**Author:** ![madironman](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@madironman](https://discourse.nodered.org/u/madironman)\
**Post date:** [19 November 2020 13:06 UTC](https://discourse.nodered.org/t/how-to-sync-timestamp-value-with-machine-time-during-runtime-of-node-red/35980/14 "2020-11-19T13:06:27Z")

</div>

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**

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 November 2020 13:32 UTC](https://discourse.nodered.org/t/how-to-sync-timestamp-value-with-machine-time-during-runtime-of-node-red/35980/15 "2020-11-19T13:32:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 November 2020 17:29 UTC](https://discourse.nodered.org/t/how-to-sync-timestamp-value-with-machine-time-during-runtime-of-node-red/35980/16 "2020-11-19T17:29:02Z")

</div>

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?  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/5/85fe1df9a5460013f6dbb4279881b8582d816fca.png)

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![madironman](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@madironman](https://discourse.nodered.org/u/madironman)\
**Post date:** [20 November 2020 08:37 UTC](https://discourse.nodered.org/t/how-to-sync-timestamp-value-with-machine-time-during-runtime-of-node-red/35980/17 "2020-11-20T08:37:53Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/8/1814d2d3f5df53a2aca1603f676659f1e8f21ebc.jpeg)

```auto
[{"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}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/7/87e0d5e2cde503bdaf4a5aff8bb5191d0c3a3d0d.png)  
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 😃, 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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 November 2020 11:13 UTC](https://discourse.nodered.org/t/how-to-sync-timestamp-value-with-machine-time-during-runtime-of-node-red/35980/18 "2020-11-20T11:13:45Z")

</div>

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:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/3/c3d13b2f5ef9a0d00fd39edf7e2d0a23ea071569.png)

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![madironman](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@madironman](https://discourse.nodered.org/u/madironman)\
**Post date:** [20 November 2020 12:17 UTC](https://discourse.nodered.org/t/how-to-sync-timestamp-value-with-machine-time-during-runtime-of-node-red/35980/19 "2020-11-20T12:17:12Z")

</div>

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..  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/c/1cd60648261249c8c626793791efaefc7a7251f1.png)

**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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 November 2020 13:33 UTC](https://discourse.nodered.org/t/how-to-sync-timestamp-value-with-machine-time-during-runtime-of-node-red/35980/20 "2020-11-20T13:33:46Z")

</div>

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?

[Next page](https://discourse.nodered.org/t/how-to-sync-timestamp-value-with-machine-time-during-runtime-of-node-red/35980.md?page=2)
