Elapsed Time

Shared in case is useful for someone else...

Function to count elapsed time between messages on same input independently of the topic.

There are 3 different outputs from top to bottom:

First will deliver a string with the count + the time base, it will be considered 3 decimals for seconds in order to reach milliseconds however will be only considered 1 decimal for minutes or hours or days.

Second will point directly the elapsed time as number considering same decimals than previous message.

Third will deliver a message with 3 objects in order to collect all details, one for the latest message received (payload,topic,timestamp), second object with same details for previous message and as last object with the elapsed time + units.

[{"id":"8c8b4fb5.726f8","type":"inject","z":"3a79879.af7b678","name":"","topic":"test","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":1700,"wires":[["1142e3fe.852c8c"]]},{"id":"1142e3fe.852c8c","type":"function","z":"3a79879.af7b678","name":"Time elapsed (same message)","func":"\nmsg1 = {};\nmsg2 = {};\nmsg3 = {};\n\nvar CurrentTime = new Date().getTime();\nvar PreviousTime = flow.get(\"PreviousTime\");\nvar PreviousMessage = flow.get(\"PreviousMessage\");\nvar PreviousTopic = flow.get(\"PreviousTopic\");\nvar LastMessage = msg.payload;\nvar LastTopic= msg.topic;\n\n if (PreviousTime === \"undefined\") {\nflow.set('PreviousTime',CurrentTime);\n\n   \n}\n\nelse {\nTimeElapsed = ((CurrentTime - PreviousTime)/1000);\nflow.set('PreviousTime',CurrentTime);\nflow.set('PreviousMessage',msg.payload);\nflow.set('PreviousTopic',msg.topic);\nmsg3.last = {\"lastTime\":CurrentTime,\"LastMessage\":LastMessage,\"LastTopic\":LastTopic};\nmsg3.Previous = {\"PreviousTime\":PreviousTime,\"PreviousMessage\":PreviousMessage,\"PreviousTopic\":PreviousTopic};\n\n\nif (TimeElapsed < 60){\nmsg1.payload = (TimeElapsed + \"  Seconds\");\ntemp = parseFloat(TimeElapsed.toFixed(3));\nmsg2.payload = temp;\nmsg3.payload = {\"Count\":temp,\"Units\":\"Seconds\"}; \n   \n}\n\n\nif ((TimeElapsed > 60) && (TimeElapsed < 3600)){\nmsg1.payload = ((parseFloat(TimeElapsed/60).toFixed(1)) + \"  Minutes\");\ntemp = parseFloat((TimeElapsed/60).toFixed(1));\nmsg2.payload = temp;\nmsg3.payload = {\"Count\":temp,\"Units\":\"Minutes\"};\n    \n}\n\n\nif ((TimeElapsed > 3600) && (TimeElapsed < 86400)){\nmsg1.payload = (parseFloat(TimeElapsed/3600).toFixed(1) + \"  Hours\");\ntemp = parseFloat((TimeElapsed/3600).toFixed(1));\nmsg2.payload = temp;\nmsg3.payload = {\"Count\":temp,\"Units\":\"Hours\"};  \n    \n}\n\n\nif ((TimeElapsed > 3600) && (TimeElapsed < 86400)){\nmsg1.payload= (parseFloat(TimeElapsed/86400).toFixed(1) + \"  Days\");\ntemp = parseFloat((TimeElapsed/86400).toFixed(1));\nmsg2.payload = temp;\nmsg3.payload = {\"Count\":temp,\"Units\":\"Days\"};   \n    \n}\n\n   }\n   \nreturn [msg1,msg2,msg3];\n \n\n\n","outputs":3,"noerr":0,"x":510,"y":1700,"wires":[["42455658.c2edc8"],["42455658.c2edc8"],["42455658.c2edc8"]],"outputLabels":["message","Elapsed Time","Units"],"icon":"node-red/timer.png"},{"id":"42455658.c2edc8","type":"debug","z":"3a79879.af7b678","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":810,"y":1700,"wires":[]},{"id":"7c21ea8b.8f3894","type":"comment","z":"3a79879.af7b678","name":"Time elapsed same message","info":"","x":160,"y":1620,"wires":[]}]

Flow Info

Created 7 years ago
Rating: 5 1

Owner

Actions

Rate:

Node Types

Core
  • comment (x1)
  • debug (x1)
  • function (x1)
  • inject (x1)

Tags

  • timer
  • elapsed
  • time
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option