# Past time of the last sensor update in minutes

**URL:** https://discourse.nodered.org/t/past-time-of-the-last-sensor-update-in-minutes/51517
**Category:** General
**Created:** [25 September 2021 19:00 UTC](https://discourse.nodered.org/t/past-time-of-the-last-sensor-update-in-minutes/51517 "2021-09-25T19:00:14Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/learn/32/48281_2.png) [@Learn](https://discourse.nodered.org/u/Learn)
#### Post date: [25 September 2021 19:00 UTC](https://discourse.nodered.org/t/past-time-of-the-last-sensor-update-in-minutes/51517/1 "2021-09-25T19:00:14Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)
#### Post date: [25 September 2021 21:02 UTC](https://discourse.nodered.org/t/past-time-of-the-last-sensor-update-in-minutes/51517/2 "2021-09-25T21:02:48Z")

</div>

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

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

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

flow.set('lastTime', thisTime)

```

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [27 September 2021 06:15 UTC](https://discourse.nodered.org/t/past-time-of-the-last-sensor-update-in-minutes/51517/3 "2021-09-27T06:15:57Z")

</div>

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)

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

```

---

<div class="post-metadata">

### Author: ![Learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/learn/32/48281_2.png) [@Learn](https://discourse.nodered.org/u/Learn)
#### Post date: [27 September 2021 18:38 UTC](https://discourse.nodered.org/t/past-time-of-the-last-sensor-update-in-minutes/51517/4 "2021-09-27T18:38:22Z")

</div>

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

 ![Sensor last time](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/2/b266f6d836cda0013f3c51ab0bce4c9e8f225f1a.png)

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.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [28 September 2021 05:24 UTC](https://discourse.nodered.org/t/past-time-of-the-last-sensor-update-in-minutes/51517/5 "2021-09-28T05:24:22Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/learn/32/48281_2.png) [@Learn](https://discourse.nodered.org/u/Learn)
#### Post date: [21 November 2021 20:48 UTC](https://discourse.nodered.org/t/past-time-of-the-last-sensor-update-in-minutes/51517/6 "2021-11-21T20:48:56Z")

</div>

I have now found a way which exactly meets my requirements.  
For this you need the following palette: [node-red-contrib-counter](https://flows.nodered.org/node/node-red-contrib-counter)

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

```

 ![1 Minutenintervall](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/a/4a62b0a270422d2e1fbea2fb6c2e52fd23f3d9f3.png)

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [5 December 2021 20:49 UTC](https://discourse.nodered.org/t/past-time-of-the-last-sensor-update-in-minutes/51517/7 "2021-12-05T20:49:40Z")

</div>

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