# Triggering output when a number changes by x within y time period

**URL:** <https://discourse.nodered.org/t/triggering-output-when-a-number-changes-by-x-within-y-time-period/53223>\
**Category:** General\
**Created:** [4 November 2021 16:26 UTC](https://discourse.nodered.org/t/triggering-output-when-a-number-changes-by-x-within-y-time-period/53223 "2021-11-04T16:26:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![iderdik](https://avatars.discourse-cdn.com/v4/letter/i/5f8ce5/32.png) [@iderdik](https://discourse.nodered.org/u/iderdik)\
**Post date:** [4 November 2021 16:26 UTC](https://discourse.nodered.org/t/triggering-output-when-a-number-changes-by-x-within-y-time-period/53223/1 "2021-11-04T16:26:55Z")

</div>

I'm looking to turn on a fan when humidity rises 10% with a 5 minute time period. The humidity sensor might report the rise over multiple reports so I can't just compare it to the last change. Any ideas on how to accomplish this?

---

<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:** [4 November 2021 16:41 UTC](https://discourse.nodered.org/t/triggering-output-when-a-number-changes-by-x-within-y-time-period/53223/2 "2021-11-04T16:41:18Z")

</div>

Do you mean that if it keeps on rising at 9% every 5 minutes you don't want it reported? So, for example, starting at 50%, then after 5 mins it is at 59% and after another 5 it is at 68% and so on?

---

<div class="post-metadata">

**Author:** ![iderdik](https://avatars.discourse-cdn.com/v4/letter/i/5f8ce5/32.png) [@iderdik](https://discourse.nodered.org/u/iderdik)\
**Post date:** [4 November 2021 17:09 UTC](https://discourse.nodered.org/t/triggering-output-when-a-number-changes-by-x-within-y-time-period/53223/3 "2021-11-04T17:09:16Z")

</div>

That is correct, but if its 52% at minute 2 and then 62% at minutes 7, I do want the trigger to fire. So its a rolling window.

---

<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:** [4 November 2021 17:16 UTC](https://discourse.nodered.org/t/triggering-output-when-a-number-changes-by-x-within-y-time-period/53223/4 "2021-11-04T17:16:59Z")

</div>

I think you will need to implement a rolling buffer of values (with timestamps) in a function node. That can easily be done using an array and the push and shift methods ([Pop, Push, Shift and Unshift Array Methods in JavaScript](https://alligator.io/js/push-pop-shift-unshift-array-methods/)). Then you can check the current time against the first one in the buffer, check the rate, and drop ones off the front when they are over 5 minutes ago.

One more clarification you need to think about. What do you want to happen if it goes up 10% in 1 minute? The answer to that will affect the code in the function node.

---

<div class="post-metadata">

**Author:** ![iderdik](https://avatars.discourse-cdn.com/v4/letter/i/5f8ce5/32.png) [@iderdik](https://discourse.nodered.org/u/iderdik)\
**Post date:** [4 November 2021 17:27 UTC](https://discourse.nodered.org/t/triggering-output-when-a-number-changes-by-x-within-y-time-period/53223/5 "2021-11-04T17:27:15Z")

</div>

I'd expect it to trigger once the 10% was hit, as long as it was within 5 minutes. I wouldn't necessarily want to wait the full 5 minutes to trigger the fan.

---

<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:** [4 November 2021 18:15 UTC](https://discourse.nodered.org/t/triggering-output-when-a-number-changes-by-x-within-y-time-period/53223/6 "2021-11-04T18:15:07Z")

</div>

You need to work out a detailed algorithm before it can be coded. Forget about how to implement it and work out exactly what needs to be done each time you get a new sample.

---

<div class="post-metadata">

**Author:** ![iderdik](https://avatars.discourse-cdn.com/v4/letter/i/5f8ce5/32.png) [@iderdik](https://discourse.nodered.org/u/iderdik)\
**Post date:** [4 November 2021 18:29 UTC](https://discourse.nodered.org/t/triggering-output-when-a-number-changes-by-x-within-y-time-period/53223/7 "2021-11-04T18:29:26Z")

</div>

This seems to encapsulate what I want:

```auto
Current sample comes in
Remove any samples older than 5 minutes
for each stored sample
  if Current sample - stored sample >= 10 then 
    trigger output
    break out of loop
store current sample

```

I guess I can turn this into JS pretty easily. Will the list of stored samples persist between runs or do I need to keep it in a global variable?

---

<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:** [4 November 2021 21:44 UTC](https://discourse.nodered.org/t/triggering-output-when-a-number-changes-by-x-within-y-time-period/53223/8 "2021-11-04T21:44:36Z")

</div>

That seems reasonable.

> [@iderdik](#):
>
> Will the list of stored samples persist between runs or do I need to keep it in a global variable

It won't automatically be saved between messages in the Function node. To do that you have to use the node-red Context. Since this is private to this node you can use the node context. See the node red docs Working with Context, and also the one on Writing Functions to see how that is done.

Rather than loop through the array comparing each one you could find the minimum value in the array and compare that. To do that you can use the `Math.min()` function along with the array destructuring operator, so if the array is called `rollingBuffer` then you can say  
`let min = Math.min(...rollingBuffer)`  
which will give you the minimum value. Don't forget to check that there is something in the array before that, testing `rollingBuffer.length`

---

<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:** [18 November 2021 21:44 UTC](https://discourse.nodered.org/t/triggering-output-when-a-number-changes-by-x-within-y-time-period/53223/9 "2021-11-18T21:44:43Z")

</div>

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