Difference between two TimeStamp

Hello everyone,

I'm quite new to Node Red and starting to use it in an Industriel use case.
Here is my use case :

  • I connected a barcode flashing device (Keyence barcode reader) to my Node Red.
  • This device is sending to my Node Red a HeartBeat message every 30 seconds when it is ON.

I would like to test every 1 minute :

  • If the last HeartBeat has been sent in the last 5 minutes --> Then get a TRUE output
  • If the last HeartBeat has been sent more than 5 minutes ago --> Then get a FALSE Output

Do you think it could be done with as less JavaScript as possible ?

Thanks for your help !

JavaScript timestamps are really just numbers in milliseconds. So you can happily subtract them then divide the result by (1000*60) = 60,000 to get a number in minutes.

You can do this along in a function node or you could simply use a change node with a simple JSONata calculation followed by a switch node.

Or without either Javascript or Jsonata (but using a flow context variable), something like this

[{"id":"ced017293aa1331f","type":"inject","z":"44697d3f1dd2e45f","name":"Every minute","props":[{"p":"payload"}],"repeat":"60","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":80,"wires":[["6af37f61fc1aa754"]]},{"id":"283fc13806e225fd","type":"inject","z":"44697d3f1dd2e45f","name":"Barcode Heartbeat","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":180,"wires":[["7e03079522c0bc48"]]},{"id":"7e03079522c0bc48","type":"trigger","z":"44697d3f1dd2e45f","name":"","op1":"true","op2":"false","op1type":"bool","op2type":"bool","duration":"5","extend":true,"overrideDelay":false,"units":"min","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":350,"y":180,"wires":[["cb37dca247ce7a22"]]},{"id":"cb37dca247ce7a22","type":"change","z":"44697d3f1dd2e45f","name":"","rules":[{"t":"set","p":"gotBarcode","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":180,"wires":[[]]},{"id":"6af37f61fc1aa754","type":"switch","z":"44697d3f1dd2e45f","name":"","property":"gotBarcode","propertyType":"flow","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":330,"y":80,"wires":[["97df320f73dea79e"],["2f7d14330b089059"]]},{"id":"97df320f73dea79e","type":"debug","z":"44697d3f1dd2e45f","name":"All good","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":480,"y":60,"wires":[]},{"id":"2f7d14330b089059","type":"debug","z":"44697d3f1dd2e45f","name":"Barcode not received","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":520,"y":100,"wires":[]}]

Thanks a lot ! Very nice to use the Trigger Node, I really didn't think about that. This perfect !
And working perfectly !
Thanks

1 Like

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