# How can I determine if a payload value is unchanged for five minutes?

**URL:** <https://discourse.nodered.org/t/how-can-i-determine-if-a-payload-value-is-unchanged-for-five-minutes/44512>\
**Category:** General\
**Created:** [21 April 2021 21:49 UTC](https://discourse.nodered.org/t/how-can-i-determine-if-a-payload-value-is-unchanged-for-five-minutes/44512 "2021-04-21T21:49:12Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![SteveMann](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stevemann/32/3810_2.png) [@SteveMann](https://discourse.nodered.org/u/SteveMann)\
**Post date:** [21 April 2021 21:49 UTC](https://discourse.nodered.org/t/how-can-i-determine-if-a-payload-value-is-unchanged-for-five-minutes/44512/1 "2021-04-21T21:49:13Z")

</div>

How would you determine if a sensor value is unchanged for a given time?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [21 April 2021 22:50 UTC](https://discourse.nodered.org/t/how-can-i-determine-if-a-payload-value-is-unchanged-for-five-minutes/44512/2 "2021-04-21T22:50:34Z")

</div>

Read it and store it in a flow or global variable, a few minutes later read it again and compare the two values. If they are the same quit, if they are different “sound the alarms” 🤣 and move the latest value to the flow/global variable.

---

<div class="post-metadata">

**Author:** ![SteveMann](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stevemann/32/3810_2.png) [@SteveMann](https://discourse.nodered.org/u/SteveMann)\
**Post date:** [22 April 2021 01:26 UTC](https://discourse.nodered.org/t/how-can-i-determine-if-a-payload-value-is-unchanged-for-five-minutes/44512/3 "2021-04-22T01:26:41Z")

</div>

I thought of that, but it's a continuous stream of data. About 5 seconds between readings. If I store a value and get, for example 100, and over the next few minutes the value goes up to 105 and down to 100 and the second reading is taken. The algorithm says "nothing changed" when the data really is changing.

But. It's better than nothing. Thanks for the tip.

---

<div class="post-metadata">

**Author:** ![wb666greene](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wb666greene/32/6534_2.png) [@wb666greene](https://discourse.nodered.org/u/wb666greene)\
**Post date:** [22 April 2021 02:47 UTC](https://discourse.nodered.org/t/how-can-i-determine-if-a-payload-value-is-unchanged-for-five-minutes/44512/4 "2021-04-22T02:47:53Z")

</div>

How does your data come in? Is it "pushed" -- say by an MQTT subscribe or a serial port message. Or is it "polled" where you read it periodically?

If its pushed, have an RBE node pass the data only if its changed. Then have this feed a trigger node set for 5 minutes in "watchdog" mode to send if its not been triggerd during the interval.

If its polled, you will probably need to decrease your polling interval and let it feed the RBE node to "create" the pushed data.

Put a function node in there with a stored variable and you can do pretty much anything you can dream up as zenofmud alludes.

---

<div class="post-metadata">

**Author:** ![SteveMann](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stevemann/32/3810_2.png) [@SteveMann](https://discourse.nodered.org/u/SteveMann)\
**Post date:** [22 April 2021 03:06 UTC](https://discourse.nodered.org/t/how-can-i-determine-if-a-payload-value-is-unchanged-for-five-minutes/44512/5 "2021-04-22T03:06:24Z")

</div>

I thought about RBE. It may yet be my solution.  
The data is polled.

Here is where I am at:  
I separate the max and min values using the smooth node, then test them. If they are the same then the value has not changed.

Here is where I am having a problem:

```auto
var a=flow.get('max') || 0;
var b=flow.get('min') || 0;

msg.payload = false;
if (a-b === 0 ){
    msg.payload = true;
}
return msg;

```

This function always returns "true" even if the vars are different.

---

<div class="post-metadata">

**Author:** ![wb666greene](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wb666greene/32/6534_2.png) [@wb666greene](https://discourse.nodered.org/u/wb666greene)\
**Post date:** [22 April 2021 03:14 UTC](https://discourse.nodered.org/t/how-can-i-determine-if-a-payload-value-is-unchanged-for-five-minutes/44512/6 "2021-04-22T03:14:08Z")

</div>

Where are you setting max and min? Can the smooth node set flow variables?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [22 April 2021 06:57 UTC](https://discourse.nodered.org/t/how-can-i-determine-if-a-payload-value-is-unchanged-for-five-minutes/44512/7 "2021-04-22T06:57:46Z")

</div>

You could try something like below the property msg.sameFive will be true if payload has been same for five minutes, else it returns false.

```auto
[{"id":"aeedaf20.c1b9f","type":"change","z":"5a245aa1.510164","name":"","rules":[{"t":"set","p":"sameFive","pt":"msg","to":"$millis() - $flowContext(\"previous.time\") > 300000 and payload = $flowContext(\"previous.payload\")","tot":"jsonata"},{"t":"set","p":"previous.time","pt":"flow","to":"payload != $flowContext(\"previous.payload\") ? $millis() : $flowContext(\"previous.time\")","tot":"jsonata"},{"t":"set","p":"previous.payload","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":2540,"wires":[["b0b8b129.ac52a8"]]},{"id":"8bd4840.459f","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":170,"y":2540,"wires":[["aeedaf20.c1b9f"]]},{"id":"ce7cd8ca.968f","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":180,"y":2580,"wires":[["aeedaf20.c1b9f"]]},{"id":"b0b8b129.ac52a8","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"sameFive","statusType":"msg","x":610,"y":2560,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![SteveMann](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stevemann/32/3810_2.png) [@SteveMann](https://discourse.nodered.org/u/SteveMann)\
**Post date:** [22 April 2021 18:12 UTC](https://discourse.nodered.org/t/how-can-i-determine-if-a-payload-value-is-unchanged-for-five-minutes/44512/8 "2021-04-22T18:12:53Z")

</div>

> [@wb666greene](#):
>
> Where are you setting max and min? Can the smooth node set flow variables?

They are coming from two smooth nodes. They are set to ten iterations and output either the highest or lowest value seen respectively.

My question is why is **if (a-b === 0 )** always true regardless of the values for a and b?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [22 April 2021 20:13 UTC](https://discourse.nodered.org/t/how-can-i-determine-if-a-payload-value-is-unchanged-for-five-minutes/44512/9 "2021-04-22T20:13:45Z")

</div>

> [@SteveMann](#):
>
> My question is why is **if (a-b === 0 )** always true regardless of the values for a and b?

Hard to tell as we have no idea what your context store values are, but do you ever, or have you ever set flow max or min to a value, if not the 0-0 === 0.

---

<div class="post-metadata">

**Author:** ![SteveMann](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stevemann/32/3810_2.png) [@SteveMann](https://discourse.nodered.org/u/SteveMann)\
**Post date:** [22 April 2021 22:07 UTC](https://discourse.nodered.org/t/how-can-i-determine-if-a-payload-value-is-unchanged-for-five-minutes/44512/10 "2021-04-22T22:07:53Z")

</div>

> [@E1cid](#):
>
> You could try something like below the property msg.sameFive will be true if payload has been same for five minutes, else it returns false.

Thanks- I haven't worked with JSonata before- now's a good time to learn.

---

<div class="post-metadata">

**Author:** ![SteveMann](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stevemann/32/3810_2.png) [@SteveMann](https://discourse.nodered.org/u/SteveMann)\
**Post date:** [22 April 2021 22:09 UTC](https://discourse.nodered.org/t/how-can-i-determine-if-a-payload-value-is-unchanged-for-five-minutes/44512/11 "2021-04-22T22:09:55Z")

</div>

> [@E1cid](#):
>
> Hard to tell as we have no idea what your context store values are, but do you ever, or have you ever set flow max or min to a value, if not the 0-0 === 0.

I checked a and b with debug nodes, they are numbers in the range of 100 to 200.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [22 April 2021 22:29 UTC](https://discourse.nodered.org/t/how-can-i-determine-if-a-payload-value-is-unchanged-for-five-minutes/44512/12 "2021-04-22T22:29:29Z")

</div>

> [@SteveMann](#):
>
> I checked a and b with debug nodes, they are numbers in the range of 100 to 200.

The deug will not hold the context values you need to check the context panel to see what flow.max and min's values are. As you are setting a and b to context values.  
[edit]  
try this

```auto
var a=flow.get('max') || 0;
var b=flow.get('min') || 0;
node.warn(`a equals ${a} and b equals ${b}`);
msg.payload = false;
if (a-b === 0 ){
    msg.payload = true;
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![SteveMann](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stevemann/32/3810_2.png) [@SteveMann](https://discourse.nodered.org/u/SteveMann)\
**Post date:** [23 April 2021 03:43 UTC](https://discourse.nodered.org/t/how-can-i-determine-if-a-payload-value-is-unchanged-for-five-minutes/44512/13 "2021-04-23T03:43:56Z")

</div>

Thanks for the tip- I have never used the node.warn node.  
But it helped me find my problem.  
 ![doh(300)](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/7/87843b5e2601623e7c23d314c2cb43b6da87c23a.jpeg)

I was saving my min and max in global.set, and in the function I was doing a flow.get. So, yes, they would always be zero.

---

<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:** [22 June 2021 03:44 UTC](https://discourse.nodered.org/t/how-can-i-determine-if-a-payload-value-is-unchanged-for-five-minutes/44512/14 "2021-06-22T03:44:54Z")

</div>

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