# Time stamp difference

**URL:** <https://discourse.nodered.org/t/time-stamp-difference/52216>\
**Category:** General\
**Created:** [14 October 2021 14:00 UTC](https://discourse.nodered.org/t/time-stamp-difference/52216 "2021-10-14T14:00:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan95](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonathan95/32/16370_2.png) [@Jonathan95](https://discourse.nodered.org/u/Jonathan95)\
**Post date:** [14 October 2021 14:00 UTC](https://discourse.nodered.org/t/time-stamp-difference/52216/1 "2021-10-14T14:00:15Z")

</div>

Hi all,

I have an inverter that has a last connection date which I want to compare with the current  
node red server time then be able to give the difference in hours/days/months/years(if applicable).  
I have tried both ways of trying to generate a time stamp using a trigger node into a function node  
and tried generating the time stamp internally of the function node but cant quiet work out how to  
get the server time stamp to calculate and minus the last connected time stamp. Id been scouring  
over the last few days trying to get this right. I am not sure what I am doing wrong.  
Id really like to somehow create an alert of some kind if its not been connected for over 5 hours  
and then id set it to send another alert after a couple hours if the message is still over that time.

Here is my code currently

```auto
[{"id":"93852779.708bb","type":"function","z":"fad30e4e.c7f78","name":"","func":"let LastCon = msg.payload.last_timestamp;\nlet SrvCon = Date.now()\n(LastCon * 1000) - SrvCon = msg.payload;\n//msg.topic = \"Inv-LastCon\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":880,"y":1340,"wires":[["3388d803.9df328","9fb55a19.e5515"]]}]

```

a little extra

```auto
[{"id":"93852779.708bb","type":"function","z":"fad30e4e.c7f78","name":"","func":"let LastCon = msg.payload.last_timestamp;\nlet SrvCon = Date.now()\n(LastCon * 1000) - SrvCon = msg.payload;\n//msg.topic = \"Inv-LastCon\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":880,"y":1340,"wires":[["3388d803.9df328","9fb55a19.e5515"]]},{"id":"3388d803.9df328","type":"debug","z":"fad30e4e.c7f78","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1110,"y":1340,"wires":[]},{"id":"8cbcf487.a0bc28","type":"debug","z":"fad30e4e.c7f78","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1110,"y":1380,"wires":[]},{"id":"641be725.eca6e8","type":"trigger","z":"fad30e4e.c7f78","name":"Current","op1":"","op2":"","op1type":"nul","op2type":"date","duration":"100","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":880,"y":1380,"wires":[["8cbcf487.a0bc28"]]}]

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [14 October 2021 14:51 UTC](https://discourse.nodered.org/t/time-stamp-difference/52216/2 "2021-10-14T14:51:24Z")

</div>

In your function node can you explain what you are trying to do with this line

```auto
let LastCon = msg.payload.last_timestamp;
let SrvCon = Date.now()
(LastCon * 1000) - SrvCon = msg.payload;
//msg.topic = "Inv-LastCon";
return msg;

```

what are you trying to do with the line `(LastCon * 1000) - SrvCon = msg.payload;`

---

<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:** [14 October 2021 14:55 UTC](https://discourse.nodered.org/t/time-stamp-difference/52216/3 "2021-10-14T14:55:08Z")

</div>

Assuming msg.payload.last\_timestamp is a unix timestamp in seconds, here are 2 examples using function node and a change node.

```auto
[{"id":"7cf9553c.7ecdac","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload.last_timestamp","v":"1634221172","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":100,"y":4840,"wires":[["737968d2.bcc9b","185dfd54.33f073"]]},{"id":"737968d2.bcc9b","type":"function","z":"b779de97.b1b46","name":"","func":"let LastCon = msg.payload.last_timestamp * 1000;\nlet SrvCon = Date.now() - LastCon;\nmsg.payload = new Date(SrvCon).toUTCString().split(\" \")[4];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":4840,"wires":[["ae626c88.34cf68"]]},{"id":"185dfd54.33f073","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment($millis() - payload.last_timestamp*1000).tz(\"utc\").format(\"HH:mm:ss\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":4760,"wires":[["ae626c88.34cf68"]]},{"id":"ae626c88.34cf68","type":"debug","z":"b779de97.b1b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":560,"y":4840,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Jonathan95](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonathan95/32/16370_2.png) [@Jonathan95](https://discourse.nodered.org/u/Jonathan95)\
**Post date:** [14 October 2021 15:59 UTC](https://discourse.nodered.org/t/time-stamp-difference/52216/4 "2021-10-14T15:59:41Z")

</div>

well one of the issues is that the inverter time stamp needs to be multiplied by 1000.  
otherwise its a few digits short of the generated timestamp of the trigger node.

I thought by making the variable names for msg.payload (which becomes ? SrvCon respectively) from the trigger node and the other which is from the inverter web API msg.payload.last\_timestamp (which becomes? LastCon respectively) as i dont know how to write it so that the following happens  
msg.payload minus (msg.payload.last\_timestamp multiply by 1000)

---

<div class="post-metadata">

**Author:** ![Jonathan95](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonathan95/32/16370_2.png) [@Jonathan95](https://discourse.nodered.org/u/Jonathan95)\
**Post date:** [14 October 2021 16:23 UTC](https://discourse.nodered.org/t/time-stamp-difference/52216/5 "2021-10-14T16:23:44Z")

</div>

You were right it is coming out as a unix time stamp and worked well with your function node.  
what should I adjust if i want what remains of the difference between the 2 time stamps?

I figure I could use an if =\> over certain value pass on message. Does it work with dates aswel?  
using a switch node?

Thanks this has helped a ton

---

<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:** [14 October 2021 17:07 UTC](https://discourse.nodered.org/t/time-stamp-difference/52216/6 "2021-10-14T17:07:17Z")

</div>

Yes SrvCon ( I should have renamed it so it was clearer) holds the difference and should be a number , so \> or \< should be fine in a if statement.  
[edit] SrvCon is the remainder in milliseconds

---

<div class="post-metadata">

**Author:** ![Jonathan95](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonathan95/32/16370_2.png) [@Jonathan95](https://discourse.nodered.org/u/Jonathan95)\
**Post date:** [14 October 2021 17:13 UTC](https://discourse.nodered.org/t/time-stamp-difference/52216/7 "2021-10-14T17:13:48Z")

</div>

Oh no....hahaha thanks for clearing that up. I dont know how I didnt see that though....

---

<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:** [28 October 2021 17:14 UTC](https://discourse.nodered.org/t/time-stamp-difference/52216/8 "2021-10-28T17:14:03Z")

</div>

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