Time stamp difference

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

[{"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

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

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

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;

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

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

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)

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

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

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

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