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.