# How to track temperature trend

**URL:** https://discourse.nodered.org/t/how-to-track-temperature-trend/20546
**Category:** General
**Created:** [18 January 2020 20:30 UTC](https://discourse.nodered.org/t/how-to-track-temperature-trend/20546 "2020-01-18T20:30:36Z")
**Posts on this page:** 18
**Page:** 2

<div class="post-metadata">

### Author: ![mobamoba](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mobamoba/32/25401_2.png) [@mobamoba](https://discourse.nodered.org/u/mobamoba)
#### Post date: [20 January 2020 16:14 UTC](https://discourse.nodered.org/t/how-to-track-temperature-trend/20546/21 "2020-01-20T16:14:12Z")

</div>

Actually the place where I'm getting stuck is how to hold a changing variable at a specific point in time. My fridge temp is constantly updating and I can't figure out how to pull, say, the 9am reading and ignore all changes until 11am so I can compare the two.

---

<div class="post-metadata">

### Author: ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)
#### Post date: [20 January 2020 17:35 UTC](https://discourse.nodered.org/t/how-to-track-temperature-trend/20546/22 "2020-01-20T17:35:42Z")

</div>

If your incoming data does not provide a timestamp you have to generate one. Then compare the stored timestamp with the incoming. If none existent (1st measurement) store both (value and timestamp) in the context you prefer (i suggest flow) else determine the delta and if it exceed the duration you like to measure you are done.

I would solve this with a single function node but there are certainly solutions for this by using pure core nodes.

---

<div class="post-metadata">

### Author: ![mobamoba](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mobamoba/32/25401_2.png) [@mobamoba](https://discourse.nodered.org/u/mobamoba)
#### Post date: [20 January 2020 17:49 UTC](https://discourse.nodered.org/t/how-to-track-temperature-trend/20546/23 "2020-01-20T17:49:15Z")

</div>

Maybe I'm not understanding something about how variables are stored. Let's say the following:  
global.temp is updated every 5 minutes from a sensor reading  
I set a change node setting flow.temp equal to global.temp  
I link that change node to a delay node where I set a day for 120 minutes.  
At the end of that 120 minutes, will flow.temp be what it was 120 minutes ago or, because global.temp is updated every 5 minutes and flow.temp is set to global.temp, will flow.temp simply equal the current global.temp?

Basically I'm looking for a way to capture an ever-changing variable (temperature in my case) at a particular moment in time so I can compare it down the line.

---

<div class="post-metadata">

### Author: ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)
#### Post date: [20 January 2020 17:51 UTC](https://discourse.nodered.org/t/how-to-track-temperature-trend/20546/24 "2020-01-20T17:51:41Z")

</div>

Perhaps you can share the flow how far you got up to now. Quite difficult to help on a descriptive text.

---

<div class="post-metadata">

### Author: ![mobamoba](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mobamoba/32/25401_2.png) [@mobamoba](https://discourse.nodered.org/u/mobamoba)
#### Post date: [20 January 2020 18:16 UTC](https://discourse.nodered.org/t/how-to-track-temperature-trend/20546/25 "2020-01-20T18:16:53Z")

</div>

Sure:

```auto
[{"id":"e49a6050.5ee198","type":"inject","z":"9fb28e31.fdea6","name":"Check Fridge Temp","topic":"","payload":"FridgeTemp","payloadType":"global","repeat":"7200","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":320,"wires":[["28f695e0.321042"]]},{"id":"28f695e0.321042","type":"switch","z":"9fb28e31.fdea6","name":"Less then 40?","property":"FridgeTemp","propertyType":"global","rules":[{"t":"lte","v":"40","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":420,"y":200,"wires":[["69c427f3.962e38"],["9f586fc8.613798"]]},{"id":"9f586fc8.613798","type":"change","z":"9fb28e31.fdea6","name":"","rules":[{"t":"set","p":"FridgeTemp","pt":"flow","to":"FridgeTemp","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":400,"wires":[["741faea.899cad"]]},{"id":"741faea.899cad","type":"delay","z":"9fb28e31.fdea6","name":"Delay 119 minutes","pauseType":"delay","timeout":"119","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":450,"y":500,"wires":[["2d7e3761.bf6db"]]},{"id":"2d7e3761.bf6db","type":"switch","z":"9fb28e31.fdea6","name":"Did the temp go down?","property":"FridgeTemp","propertyType":"global","rules":[{"t":"lt","v":"FridgeTemp","vt":"flow"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":610,"y":400,"wires":[["69c427f3.962e38"],["953f1916.505778","cba2eaf.6ad3918"]]},{"id":"69c427f3.962e38","type":"change","z":"9fb28e31.fdea6","name":"","rules":[{"t":"set","p":"FridgeStatus","pt":"global","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":300,"wires":[["57a908e7.7f39c"]]},{"id":"953f1916.505778","type":"change","z":"9fb28e31.fdea6","name":"","rules":[{"t":"set","p":"FridgeStatus","pt":"global","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":480,"wires":[["57a908e7.7f39c"]]},{"id":"57a908e7.7f39c","type":"change","z":"9fb28e31.fdea6","name":"","rules":[{"t":"set","p":"FridgeStatus","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":880,"y":420,"wires":[["7b672fa3.77cb3"]]},{"id":"7b672fa3.77cb3","type":"ui_text","z":"9fb28e31.fdea6","group":"12015f39.142781","order":8,"width":0,"height":0,"name":"Fridge OK?","label":"Fridge Ok?","format":"{{msg.payload}}","layout":"row-spread","x":1100,"y":420,"wires":[]},{"id":"12015f39.142781","type":"ui_group","z":"","name":"Lights","tab":"8e0b5c7a.424a48","disp":true,"width":"6","collapse":false},{"id":"8e0b5c7a.424a48","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":1,"disabled":false,"hidden":false}]
```

---

<div class="post-metadata">

### Author: ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)
#### Post date: [20 January 2020 22:01 UTC](https://discourse.nodered.org/t/how-to-track-temperature-trend/20546/26 "2020-01-20T22:01:16Z")

</div>

perhaps like this? But be aware this is only a first layout to give you a little kickstart.

```auto
[{"id":"caf032d7.6095a","type":"function","z":"9684985b.d608d8","name":"30-50°","func":"msg.topic=msg.payload; \nmsg.label=msg.payload;\nmsg.payload=40+(20*Math.random()-10);\nreturn msg;","outputs":1,"noerr":0,"x":271,"y":102,"wires":[["52e5a428.3e305c"]]},{"id":"d2da1ba2.346478","type":"inject","z":"9684985b.d608d8","name":"","topic":"","payload":"inside","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":118,"y":102,"wires":[["caf032d7.6095a"]]},{"id":"5e871ac3.39f914","type":"switch","z":"9684985b.d608d8","name":"Less then 40?","property":"payload","propertyType":"msg","rules":[{"t":"lte","v":"40","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":624,"y":102,"wires":[["fa00994.be95b68"],["223c3640.aaca4a"]]},{"id":"fa00994.be95b68","type":"change","z":"9684985b.d608d8","name":"below 40°","rules":[{"t":"set","p":"FridgeStatus","pt":"flow","to":"all fine (<40)","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"below 40°","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":978,"y":68,"wires":[["6981ae15.47cb8","2bf4750a.98e8ea"]]},{"id":"223c3640.aaca4a","type":"delay","z":"9684985b.d608d8","name":"Delay 2 minutes","pauseType":"delay","timeout":"2","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":845,"y":170,"wires":[["38b05bc3.587cd4"]]},{"id":"52e5a428.3e305c","type":"change","z":"9684985b.d608d8","name":"save","rules":[{"t":"set","p":"recent","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":441,"y":102,"wires":[["5e871ac3.39f914"]]},{"id":"38b05bc3.587cd4","type":"switch","z":"9684985b.d608d8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"recent","vt":"flow"},{"t":"gte","v":"recent","vt":"flow"}],"checkall":"true","repair":false,"outputs":2,"x":1019,"y":170,"wires":[["90758b69.0e0678"],["175cc3f6.0e514c"]]},{"id":"90758b69.0e0678","type":"change","z":"9684985b.d608d8","name":"falling","rules":[{"t":"set","p":"FridgeStatus","pt":"flow","to":"temp falling","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"falling","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1155,"y":136,"wires":[["6981ae15.47cb8","2bf4750a.98e8ea"]]},{"id":"175cc3f6.0e514c","type":"change","z":"9684985b.d608d8","name":"rising","rules":[{"t":"set","p":"FridgeStatus","pt":"flow","to":"temp rising","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"rising","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1155,"y":204,"wires":[["6981ae15.47cb8","2bf4750a.98e8ea"]]},{"id":"6981ae15.47cb8","type":"ui_text","z":"9684985b.d608d8","group":"203494fb.e2be2c","order":8,"width":0,"height":0,"name":"Fridge OK?","label":"Fridge Ok?","format":"{{msg.payload}}","layout":"row-spread","x":1362,"y":68,"wires":[]},{"id":"2bf4750a.98e8ea","type":"debug","z":"9684985b.d608d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1362,"y":119,"wires":[]},{"id":"203494fb.e2be2c","type":"ui_group","z":"","name":"Lights","tab":"612f702e.cc40e","disp":true,"width":"6","collapse":false},{"id":"612f702e.cc40e","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

```

---

<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: [21 January 2020 07:27 UTC](https://discourse.nodered.org/t/how-to-track-temperature-trend/20546/27 "2020-01-21T07:27:30Z")

</div>

> [@Christian-Me](#):
>
> If your incoming data does not provide a timestamp you have to generate one.

_I'm not really 100% up to speed in the thread. It has got away on me._

Sorry, but I'll play "devil's advocate" in that if the data arrives at given intervals and you want to see if it has changed in 2 hours (120 minutes).... It is just simple maths.

Though I guess it could get difficult if there are 6 readings in 2 hours. (Every 20 minutes.)  
But time stamps are not really needed.

---

<div class="post-metadata">

### Author: ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)
#### Post date: [21 January 2020 07:41 UTC](https://discourse.nodered.org/t/how-to-track-temperature-trend/20546/28 "2020-01-21T07:41:14Z")

</div>

Certainly not that simple layout flow I got and replied. But I even don’t think that this will do the job very well, especially the false alarms will make the HFSD (human fan smacking device) going out of business very soon 🤪

---

<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: [21 January 2020 07:45 UTC](https://discourse.nodered.org/t/how-to-track-temperature-trend/20546/29 "2020-01-21T07:45:46Z")

</div>

🙂 Yeah. It isn't I meant to say you are/were wrong.

Sorry if that was how it sounded.

Believe me I am having lots of fun recently - as you may see by my posts.

---

<div class="post-metadata">

### Author: ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)
#### Post date: [21 January 2020 08:20 UTC](https://discourse.nodered.org/t/how-to-track-temperature-trend/20546/30 "2020-01-21T08:20:13Z")

</div>

No problem on my side 😃 We need more fun these days ❄❄❄

---

<div class="post-metadata">

### Author: ![mobamoba](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mobamoba/32/25401_2.png) [@mobamoba](https://discourse.nodered.org/u/mobamoba)
#### Post date: [21 January 2020 12:20 UTC](https://discourse.nodered.org/t/how-to-track-temperature-trend/20546/31 "2020-01-21T12:20:41Z")

</div>

I was trying to track trend over time rather than temperature at a given moment in order to avoid false alarms. In other words, if it's dropping from 50 to 48 over the two hour period, there should be no alarm even though the temp is above 40 because the trend is downward.

That's what I can't figure out how to do in Node Red - trend tracking.

And thanks for your flow but I'm pulling temp data from a sensor which updates itself every few minutes and I couldn't see where in the flow I'd insert that data to test it. Thanks.

Oh and the reason the HFSD can work 🙂 is because the F only conks out maybe once or twice a year. Believe me if the HFSD has to S on a regular basis the HFSD will be getting a new fridge or calling the repair person!

---

<div class="post-metadata">

### Author: ![pcmvanveen](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pcmvanveen/32/16373_2.png) [@pcmvanveen](https://discourse.nodered.org/u/pcmvanveen)
#### Post date: [21 January 2020 13:05 UTC](https://discourse.nodered.org/t/how-to-track-temperature-trend/20546/32 "2020-01-21T13:05:34Z")

</div>

Hi there, I use the avarage node to detect trends over a time periode. So I don't see spikes in the end result, just the trend.

---

<div class="post-metadata">

### Author: ![kwa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kwa/32/8835_2.png) [@kwa](https://discourse.nodered.org/u/kwa)
#### Post date: [22 January 2020 08:14 UTC](https://discourse.nodered.org/t/how-to-track-temperature-trend/20546/33 "2020-01-22T08:14:21Z")

</div>

> [@mobamoba](#):
>
> HA because, also in the spirit of home automation, I'm making do with the cheap stuff I have sitting around, one of which was a Zigbee temp/humidity sensor.

I don't mean to derail this topic, but out of interest what sensor are you using? I naively assumed a sensor in the fridge would struggle with signal quality.

---

<div class="post-metadata">

### Author: ![mobamoba](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mobamoba/32/25401_2.png) [@mobamoba](https://discourse.nodered.org/u/mobamoba)
#### Post date: [22 January 2020 17:52 UTC](https://discourse.nodered.org/t/how-to-track-temperature-trend/20546/34 "2020-01-22T17:52:46Z")

</div>

The Xiaomi Aqara. Haven't had a problem with the connection.

---

<div class="post-metadata">

### Author: ![yesdred](https://avatars.discourse-cdn.com/v4/letter/y/e274bd/32.png) [@yesdred](https://discourse.nodered.org/u/yesdred)
#### Post date: [25 January 2020 06:49 UTC](https://discourse.nodered.org/t/how-to-track-temperature-trend/20546/35 "2020-01-25T06:49:07Z")

</div>

A bit late to the party but I'd like to throw my two cents into the ring.

You seem to be interested more in the differential than the value. I'd store measurements in an InfluxDB and query and compare values from x minutes ago with the current value. To make sure I do not hit two short drops with normal behavior in between I'd do two or three such comparisons with minutes between them.

---

<div class="post-metadata">

### Author: ![mobamoba](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mobamoba/32/25401_2.png) [@mobamoba](https://discourse.nodered.org/u/mobamoba)
#### Post date: [27 January 2020 17:22 UTC](https://discourse.nodered.org/t/how-to-track-temperature-trend/20546/36 "2020-01-27T17:22:54Z")

</div>

Could it be as simple as set the msg.payload to the global Fridge Temp variable, send the msg.payload to a Delay node which waits 2 hours, then, 2 hours later, compare the msg.payload to the current global Fridge Temp?

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [27 January 2020 17:39 UTC](https://discourse.nodered.org/t/how-to-track-temperature-trend/20546/37 "2020-01-27T17:39:32Z")

</div>

It is easier than that. If you are only interested in looking every two hours then every two hours send the temperature to a function node. In the function node you will save the current value to a node context variable. That means that also in the function node you can pick up the previous value and do the comparisons required to decide what to do. Look at the docs on writing functions to see how to save a value to the node context.

---

<div class="post-metadata">

### Author: ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)
#### Post date: [27 January 2020 17:59 UTC](https://discourse.nodered.org/t/how-to-track-temperature-trend/20546/38 "2020-01-27T17:59:10Z")

</div>

Is the flow I posted a while ago not doing that? Even without the need of a function node?

> [@How to track temperature trend](https://discourse.nodered.org/t/how-to-track-temperature-trend/20546/26):
>
> perhaps like this? But be aware this is only a first layout to give you a little kickstart. [{"id":"caf032d7.6095a","type":"function","z":"9684985b.d608d8","name":"30-50°","func":"msg.topic=msg.payload; \nmsg.label=msg.payload;\nmsg.payload=40+(20\*Math.random()-10);\nreturn msg;","outputs":1,"noerr":0,"x":271,"y":102,"wires":[["52e5a428.3e305c"]]},{"id":"d2da1ba2.346478","type":"inject","z":"9684985b.d608d8","name":"","topic":"","payload":"inside","payloadType":"str","repeat":"","crontab":"","…

[Previous page](https://discourse.nodered.org/t/how-to-track-temperature-trend/20546.md?page=1)
