# How to compare two message - times and see if they are different?

**URL:** <https://discourse.nodered.org/t/how-to-compare-two-message-times-and-see-if-they-are-different/79516>\
**Category:** General\
**Created:** [30 June 2023 22:57 UTC](https://discourse.nodered.org/t/how-to-compare-two-message-times-and-see-if-they-are-different/79516 "2023-06-30T22:57:40Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [30 June 2023 22:57 UTC](https://discourse.nodered.org/t/how-to-compare-two-message-times-and-see-if-they-are-different/79516/1 "2023-06-30T22:57:40Z")

</div>

I am not really sure what I am doing, but to _keep the hamster happy_ upstairs, I am just asking this.

I have a RasPi that is MY NTP server. All my devices use it.  
It has a RTC also. Not sure how that is important just now. But I'll mention it here/now.

I know there are NTP nodes out there but they seem to be overkill for what I want to do - I think.  
Basically I want a _daily_ NTP check that the clock isn't _wonky_.

A machine - not sure if it will be THAT one, or another one on the network.  
The _other machine_ will get it's time - which I now see is not going to work too well given the scenario I want to test.  
I know there is a problem here with what I am about to say/ask.  
Ok, two problems. (_ARGH!_)

I don't get/understand the background workings of NTP, HW and SW clocks and who is taking to whom to get their times.

So I am already in heavy fog for what I am doing.

I have 3 clocks: NTP, HW and SW.  
If one of them goes _off on an excursion_ I want to be informed.

So say every 6 hours it looks at the 3 clocks and hope they all match - within a given margin.  
If one is wrong, it tells me (sends a message).

The NTP node I have won't poll that slowly.  
So I have a command I use:  
`timedatectl status | grep Local`  
and I get the local time given to me.  
Put that through a `string` node and it looks nice:  
`"Sat 2023-07-01 08:35:49 AEST"`  
Local time. I know that is no nice, but to establish a base line, I'll talk local for now.  
Excuse me for now if this is not _THE WAY_.

I then read the HW and SW clocks and see if the new reading is within 30 minutes of the last time.  
If it was a `timestamp` I got back would be a lot easier I know.

Any thoughts on how I can make it easier?  
Oh, the exciting code I use to check the NTP side is working:

```auto
[{"id":"ce5ac6a3c140825d","type":"exec","z":"675e227d.d158b4","command":"timedatectl status | grep Local","addpay":"","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":530,"y":3000,"wires":[["dff7cbdba175c41a","b1e36e2fa2775905"],[],[]]},{"id":"994fd43d97ac2b98","type":"inject","z":"675e227d.d158b4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":310,"y":3000,"wires":[["ce5ac6a3c140825d"]]},{"id":"dff7cbdba175c41a","type":"debug","z":"675e227d.d158b4","name":"debug 271","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":470,"y":2960,"wires":[]},{"id":"b1e36e2fa2775905","type":"string","z":"675e227d.d158b4","name":"","methods":[{"name":"collapseWhitespace","params":[]},{"name":"delLeftMost","params":[{"type":"str","value":": "}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":350,"y":3070,"wires":[["62557d9dbcb19e34"]]},{"id":"62557d9dbcb19e34","type":"debug","z":"675e227d.d158b4","name":"debug 272","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":370,"y":3110,"wires":[]}]

```

(Subflow and `gate` nodes in here)  
And this is how I read the SW and HW clocks.  
I send a pulse in and get the two times sent out to the `debug` nodes here. They are really `text` nodes, but to reduce the junk I am posting....

```auto
[{"id":"8d34d18c.a5c1d8","type":"subflow","name":"Time Stamp","info":"","category":"","in":[{"x":80,"y":100,"wires":[{"id":"e238745c.805ad"}]}],"out":[{"x":780,"y":180,"wires":[{"id":"e238745c.805ad","port":0},{"id":"f4d2e3a6.04b528","port":0}]},{"x":780,"y":230,"wires":[{"id":"e238745c.805ad","port":0},{"id":"f24c822.a42738","port":0}]},{"x":780,"y":280,"wires":[{"id":"5da44411.ed02ac","port":0},{"id":"e238745c.805ad","port":0}]}],"env":[],"color":"#FF8888","outputLabels":["For logging use","msg.time","For filename use"],"icon":"node-red/timer.svg"},{"id":"f4d2e3a6.04b528","type":"moment","z":"8d34d18c.a5c1d8","name":"","topic":"","input":"","inputType":"msg","inTz":"Australia/Sydney","adjAmount":0,"adjType":"days","adjDir":"add","format":"YYYY-MM-DD HH:mm:ss","locale":"en_AU","output":"","outputType":"msg","outTz":"Australia/Sydney","x":400,"y":180,"wires":[["5da44411.ed02ac","3a4de0cb.6235d"]]},{"id":"5da44411.ed02ac","type":"string","z":"8d34d18c.a5c1d8","name":"","methods":[{"name":"replaceAll","params":[{"type":"str","value":":"},{"type":"str","value":""}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":450,"y":280,"wires":[[]]},{"id":"3a4de0cb.6235d","type":"change","z":"8d34d18c.a5c1d8","name":"TOPIC","rules":[{"t":"move","p":"payload","pt":"msg","to":"time","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":230,"y":230,"wires":[["f24c822.a42738"]]},{"id":"e584e350.1d8a48","type":"change","z":"8d34d18c.a5c1d8","name":"Save","rules":[{"t":"set","p":"origPayload","pt":"msg","to":"payload","tot":"msg","dc":true}],"action":"","property":"","from":"","to":"","reg":false,"x":230,"y":140,"wires":[["3411b340.b65a04"]]},{"id":"f24c822.a42738","type":"change","z":"8d34d18c.a5c1d8","name":"Get","rules":[{"t":"set","p":"payload","pt":"msg","to":"origPayload","tot":"msg","dc":true}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":230,"wires":[[]]},{"id":"e238745c.805ad","type":"switch","z":"8d34d18c.a5c1d8","name":"check topic","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"TIMESTAMP","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":210,"y":100,"wires":[[],["e584e350.1d8a48"]]},{"id":"3411b340.b65a04","type":"change","z":"8d34d18c.a5c1d8","name":"TimeStamp","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":210,"y":180,"wires":[["f4d2e3a6.04b528"]]},{"id":"596dfbde.39bc6c","type":"exec","z":"e2bd5a4e.5597e8","g":"9e69bb336a453bf0","command":"sudo hwclock -r","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"HWC time","x":630,"y":2240,"wires":[["f90ccbc3.25a8b","8601b654.799c6"],[],[]]},{"id":"f90ccbc3.25a8b","type":"string","z":"e2bd5a4e.5597e8","g":"9e69bb336a453bf0","name":"","methods":[{"name":"replaceAll","params":[{"type":"str","value":"-"},{"type":"str","value":"/"}]},{"name":"left","params":[{"type":"num","value":"19"}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":800,"y":2300,"wires":[["478e7ad6.3b69fc"]]},{"id":"29b57fb9.8ba4f","type":"subflow:8d34d18c.a5c1d8","z":"e2bd5a4e.5597e8","g":"9e69bb336a453bf0","name":"","x":630,"y":2190,"wires":[["e53ec78c.5d5ce8","8532dba.67d7e28"],[],[]]},{"id":"8532dba.67d7e28","type":"string","z":"e2bd5a4e.5597e8","g":"9e69bb336a453bf0","name":"","methods":[{"name":"left","params":[{"type":"num","value":"19"}]},{"name":"replaceAll","params":[{"type":"str","value":"-"},{"type":"str","value":"/"}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":800,"y":2220,"wires":[["539fda71.578aa4"]]},{"id":"539fda71.578aa4","type":"gate","z":"e2bd5a4e.5597e8","g":"9e69bb336a453bf0","name":"","controlTopic":"control","defaultState":"open","openCmd":"go","closeCmd":"stop","toggleCmd":"toggle","defaultCmd":"default","persist":false,"x":930,"y":2220,"wires":[["c10d8191.fe707","e582752f.3763f","edb7d713.13151","f205ad3c.2c3aa8"]]},{"id":"478e7ad6.3b69fc","type":"gate","z":"e2bd5a4e.5597e8","g":"9e69bb336a453bf0","name":"","controlTopic":"control","defaultState":"open","openCmd":"go","closeCmd":"stop","toggleCmd":"toggle","defaultCmd":"default","persist":false,"x":930,"y":2300,"wires":[["fb254740.543b38","789410be.8b6d78","ffff48cd.81dd","6fc84240.f1f1dc"]]},{"id":"e582752f.3763f","type":"debug","z":"e2bd5a4e.5597e8","g":"9e69bb336a453bf0","name":"Real Time (Now) with /","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1160,"y":2200,"wires":[]},{"id":"789410be.8b6d78","type":"debug","z":"e2bd5a4e.5597e8","g":"9e69bb336a453bf0","name":"HWC Time (Now) with /","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1170,"y":2280,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [1 July 2023 00:55 UTC](https://discourse.nodered.org/t/how-to-compare-two-message-times-and-see-if-they-are-different/79516/2 "2023-07-01T00:55:40Z")

</div>

NTP servers are the backbone of the internet. They are within milliseconds of "the time" which is the atomic clock in the US. All clocks reference this time, or they used to. That said there are dozens of NTP servers you can interrogate. There are lists of these servers on the web. Hardware clocks especially in electronic devices such as pc's are a dedicated chip that goes out and talks to a NTP server. Software clocks are a piece of software that does the same thing as a hardware clock.  
What I don't understand is why you are using three clocks. Is there no way to MQTT or something to send the time to your devices? I use a Nodemcu to talk to a NTP server and send the time to my devices. If you have a pi in your mass of devices it will automatically ping the NTP server. You wouldn't need a NTP node simply use a function node to get system time. Apparently your devices talk to each other, I'm not understanding your angst. You mention a 30 minute difference, my systems are within milliseconds of each other. Perhaps I am not getting your point.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 July 2023 04:13 UTC](https://discourse.nodered.org/t/how-to-compare-two-message-times-and-see-if-they-are-different/79516/3 "2023-07-01T04:13:22Z")

</div>

Hi @gerry

(longish story)

Way back when I first got into the RasPi, I had NO internet.  
Each time the machine was turned on I had to set the time.  
After a while this became painfiul.  
I **THINK** the RTC came next. Saved a lot of grief in that part.  
My network then grew and it got things that were done at/on a time schedule.  
The RTC was great as if a reboot or power failure happened things wouldn't happen at the wrong time/s.

Slowly I got an internet connection. (Woo hoo!) And I joined here.

The intricacies of the structure of the _TIME_ is still beyond me.  
All/most of this is done from curiosity.

But for the sake of explaining my _theory_:  
~~The RTC is THE clock. It will survive a reboot/power failure etc.  
The SW clock gets it's time from it and is used on THAT machine.  
Other devices on my network have their NTP server set to the afore mentioned machine.~~

_(after rethink)_  
The system boots (MY NTP server) it gets its time from the HWC.  
_SYSTEM TIME_ is set from that and the NTP server broadcasts that time.  
My network is SINGLE SITED and I don't need absolute millisecond accuracy..  
But it is important that 05:00 is 05:00 and not 13:00! Or vice versa.

So:  
System running and the power goes.  
Comes back.  
Machines boot.  
MY (LOCAL) NTP server broadcasts (or has ready) it's time and all devices get its time.  
All is good.  
Every _hour or so_ it would be nice that my local NTP server talk back to a T2 (or 1) server and fixes any problems.  
But I do NOT want the EXTERNAL NTP to be THE GO TO as if the internet is down, all bets are off to what time the devices think it is.

Is that clearer?

I guess I have a _background task_ that _every now and then_ syncs the RTC (Hardware) clock on my RasPi to the external L2 (or 1) NTP sever.

If there is a difference, it adjusts my local time accordingly.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [1 July 2023 05:29 UTC](https://discourse.nodered.org/t/how-to-compare-two-message-times-and-see-if-they-are-different/79516/4 "2023-07-01T05:29:24Z")

</div>

Doesn't the output of **timedatectl status**"System clock synchronized: yes/no" confirm if a device is synchronised with your NTP server without the need to compare time strings?

---

<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:** [1 July 2023 08:39 UTC](https://discourse.nodered.org/t/how-to-compare-two-message-times-and-see-if-they-are-different/79516/5 "2023-07-01T08:39:06Z")

</div>

> [@Trying\_to\_learn](#):
>
> Way back when I first got into the RasPi, I had NO internet.

Do you now have internet? If so then get rid of your local NTP server and let the OS sort it out for you using public servers.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 July 2023 09:13 UTC](https://discourse.nodered.org/t/how-to-compare-two-message-times-and-see-if-they-are-different/79516/6 "2023-07-01T09:13:11Z")

</div>

Thanks for that idea.

I _wasn't seeing_ that because I was filtering for the local time, rather than a _This time is good_ flag.

So to check that I would just do something like:  
`timedatectl | grep syncronized` and act on the output being `yes` or `no`. (Sorry my mistake on the first post)  
(All the tricks I missed)

😉

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 July 2023 09:20 UTC](https://discourse.nodered.org/t/how-to-compare-two-message-times-and-see-if-they-are-different/79516/7 "2023-07-01T09:20:18Z")

</div>

In a perfect world maybe. I live in Oz and it is far from.

I had (sorry side story) an old electronic lock that would wake me up to go to work of a morning.  
It was old and destined for scrap, but was still working.

Winter, cold dark.  
ALARM goes off.  
I struggle up. _Gee it IS dark_.  
Look at my watch. (#!&\*&($!  
Alarm set for 05:15. It is 02:xx! The alarm clock says 05:15  
Rest the time, go back to bed and sleep.

Next day: Same thing happened.  
This happened for abut 4 days and the alarm clock was consigned to the bin.  
I guess 30 years of service is pretty good.  
Had to use my phone for a few weeks while I learnt how to program Arduinos.

Relying on having an internet connection is not a good idea for me to get the time.  
If I have a local RTC - yes, which can fail - is better than hoping the internet is working after a power failure and setting system scheduled tasks.

I've been down a similar rabbit hole where things were getting incorrectly scheduled. But that is a WHOLE OTHER story.  
Having my RasPi as my local NTP server is/should be more reliable than expecting the internet to be there when I REALLY need it.

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [1 July 2023 10:56 UTC](https://discourse.nodered.org/t/how-to-compare-two-message-times-and-see-if-they-are-different/79516/8 "2023-07-01T10:56:32Z")

</div>

Back in the day clocks drifted a lot. But today even cheap clocks hold the time pretty well. If you can get the pi to sync up to NTP once I'd say it would be within seconds over a year running on it's internal clock. I'm in north America so we have WWV, Does Europe not have something similar? Do you have any kind of UPS for your pi? Even if it only syncs to NTP every month I would think it would be far more accurate than you'd need.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [1 July 2023 11:00 UTC](https://discourse.nodered.org/t/how-to-compare-two-message-times-and-see-if-they-are-different/79516/9 "2023-07-01T11:00:35Z")

</div>

The Raspberry pi doesn’t have an onboard rtc so has to sync to something on every boot.

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [1 July 2023 11:17 UTC](https://discourse.nodered.org/t/how-to-compare-two-message-times-and-see-if-they-are-different/79516/10 "2023-07-01T11:17:15Z")

</div>

Which is why I said if you can get it to sync once then if you have a ups to get past the power failure you should be able to get stable time. The Pi will keep time on its own, it just won't survive a boot up.  
[Edit]  
It doesn't have an rtc as such but it does have a counter per say so it keeps track of how much time has elapsed and it does it fairly accurately.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [1 July 2023 11:37 UTC](https://discourse.nodered.org/t/how-to-compare-two-message-times-and-see-if-they-are-different/79516/11 "2023-07-01T11:37:46Z")

</div>

For completely offline use, if you have visibility of the sky, then you can use a cheap GPS dongle to feed a local time service. (Nothing to do with Node-RED of course) - [How to Add a GPS Time Source to ntpd | Rapid7 Blog](https://www.rapid7.com/blog/post/2015/07/27/adding-a-gps-time-source-to-ntpd/)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 July 2023 20:54 UTC](https://discourse.nodered.org/t/how-to-compare-two-message-times-and-see-if-they-are-different/79516/12 "2023-07-01T20:54:40Z")

</div>

For clarity:  
There IS a RTC in/on THIS RasPi.

And I shall take it as being a good time source.

So as @gerry said: It only should need syncing every month or so.  
(Or say daily - just for the fun of it to help prevent any confusion when Daylight Savings kicks in)

Not every 10 minutes.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 July 2023 23:09 UTC](https://discourse.nodered.org/t/how-to-compare-two-message-times-and-see-if-they-are-different/79516/13 "2023-07-01T23:09:34Z")

</div>

# All:

Thanks for the help.

This has given me more than enough to go through and work on.

I'll close it now because I think it has kind of been resolved.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [15 July 2023 23:10 UTC](https://discourse.nodered.org/t/how-to-compare-two-message-times-and-see-if-they-are-different/79516/14 "2023-07-15T23:10:18Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
