Past time of the last sensor update in minutes

Hello I would like to display in the dasboard of my Zigbee sensor the past time of the last sensor update in minutes.
Theoretically, I only need to send a timestamp when receiving a measured value. But how can I convert that so that I am displayed until there the past time in minutes?

Retrieve the last timestamp from a context variable (or a default for the first run). Subtract this from the current timestamp. Something like

const lastTime = flow.get('lastTime') || Date.now()
const thisTime = Date.now()

const minutesPast = Math.round((thisTime - lastTime) / 60 / 1000)

flow.set('lastTime', thisTime)
1 Like

This is a bit of a Hack job and uses a foreign node - node-red-contrib-moment - but it also works.

Replace the inject node with your signal.
Just the msg.payload has to be the timestamp (current time)

[{"id":"57c224ba9b5a2abb","type":"moment","z":"65c9b63cb09879a0","name":"","topic":"","input":"","inputType":"msg","inTz":"ETC/utc","adjAmount":0,"adjType":"days","adjDir":"add","format":"HH:mm:ss","locale":"utc","output":"","outputType":"msg","outTz":"ETC/utc","x":1880,"y":650,"wires":[["c1ce0a57e1338816"]]},{"id":"495a2f9ed7d86f04","type":"function","z":"65c9b63cb09879a0","name":"","func":"let now = msg.payload;\nlet past = 0;\nif (context.get(\"last\") > 0)\n{\n//    node.warn(\"calculating\");\n    last = context.get(\"last\");\n    context.set(\"last\",msg.payload);\n//    node.warn(\"Last = \" + last);\n//    node.warn(\"Now = \" + now);\n    past = now - last;\n//    node.warn(\"Past = \" + past);\n    msg.payload = past;\n    return msg;\n} else\ncontext.set(\"last\",msg.payload);\nreturn;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1690,"y":650,"wires":[["57c224ba9b5a2abb"]]},{"id":"c1ce0a57e1338816","type":"debug","z":"65c9b63cb09879a0","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2070,"y":650,"wires":[]},{"id":"f6e6e46a7a138ab9","type":"inject","z":"65c9b63cb09879a0","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1530,"y":650,"wires":[["495a2f9ed7d86f04"]]}]
1 Like

@Trying_to_learn This is already a very good approach and works so far.

I imagine it but actually so that when a signal arrives the time is set to 0 and as long as no signal arrives the time counts up automatically.

That's what mine does too.
Yes, you need an initial injection to set things up, but nothing's perfect.

I have now found a way which exactly meets my requirements.
For this you need the following palette: node-red-contrib-counter

[{"id":"2d8ceed6bfa41ca1","type":"counter","z":"b0d182c506b2db09","name":"","init":"0","step":"1","lower":null,"upper":null,"mode":"increment","outputs":"2","x":1200,"y":1660,"wires":[["3111fafde01202a4"],[]]},{"id":"4780e2a242c96333","type":"inject","z":"b0d182c506b2db09","name":"1 Sek. intervall","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":"","topic":"","payloadType":"date","x":1230,"y":1620,"wires":[["2d8ceed6bfa41ca1"]]},{"id":"1ea293ed3c7ee22d","type":"ui_text","z":"b0d182c506b2db09","group":"79ec8d7d3ac0561d","order":8,"width":0,"height":0,"name":"","label":"Aktualisiert vor","format":"{{msg.payload}} Minuten","layout":"row-spread","x":1540,"y":1660,"wires":[]},{"id":"94db9a98747d4248","type":"change","z":"b0d182c506b2db09","name":"ZurĂĽcksetzen","rules":[{"t":"set","p":"reset","pt":"msg","to":"1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1220,"y":1700,"wires":[["2d8ceed6bfa41ca1"]]},{"id":"3111fafde01202a4","type":"function","z":"b0d182c506b2db09","name":"","func":"msg.payload=msg.payload/60;\nmsg.payload = Number(msg.payload.toFixed(1));\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1360,"y":1660,"wires":[["1ea293ed3c7ee22d"]]},{"id":"79ec8d7d3ac0561d","type":"ui_group","name":"Aussentemperatur","tab":"43e39aa09af7bc24","order":1,"disp":true,"width":"6","collapse":false},{"id":"43e39aa09af7bc24","type":"ui_tab","name":"Temperatur","icon":"fa-thermometer-three-quarters","order":1,"disabled":false,"hidden":false}]

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