# If the value is below a reference value for more than x hours, then do something "fail-safe"

**URL:** <https://discourse.nodered.org/t/if-the-value-is-below-a-reference-value-for-more-than-x-hours-then-do-something-fail-safe/65819>\
**Category:** General\
**Created:** [30 July 2022 17:50 UTC](https://discourse.nodered.org/t/if-the-value-is-below-a-reference-value-for-more-than-x-hours-then-do-something-fail-safe/65819 "2022-07-30T17:50:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![edstobi](https://avatars.discourse-cdn.com/v4/letter/e/aca169/32.png) [@edstobi](https://discourse.nodered.org/u/edstobi)\
**Post date:** [30 July 2022 17:50 UTC](https://discourse.nodered.org/t/if-the-value-is-below-a-reference-value-for-more-than-x-hours-then-do-something-fail-safe/65819/1 "2022-07-30T17:50:35Z")

</div>

if the value is below a reference value for more than x hours, then do something.  
Hello  
I have a question to the forum how you would approach it.  
I have the measured value, which comes from mqtt, written into a flow.set and compare this every few seconds with the latest value, if this is below my threshold I aktualisate my flow.set and write a second flow.set the time. Then I compare the time with now and check if 2 hours have passed. If yes an action is triggered, if no then everything starts again from the beginning with the next measured value of mqtt.  
But now I have set myself a trap, by restarting the PI or update IObroker..... .  
How would you proceed. Write these values every time in a DB and write every 10 seconds a new value. Write the data in a file on the PI or is there another variant to make the measurements "fail-safe"?

---

<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:** [30 July 2022 18:28 UTC](https://discourse.nodered.org/t/if-the-value-is-below-a-reference-value-for-more-than-x-hours-then-do-something-fail-safe/65819/2 "2022-07-30T18:28:33Z")

</div>

I think this could all be done using a Trigger node. Think of the problem the other way round, feed only values above the threshold to the Trigger, set to send nothing then wait x hours, then send the error. With Extend selected it will restart the timer every time a good value is seen.

---

<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:** [30 July 2022 18:46 UTC](https://discourse.nodered.org/t/if-the-value-is-below-a-reference-value-for-more-than-x-hours-then-do-something-fail-safe/65819/3 "2022-07-30T18:46:24Z")

</div>

If you need it to survive a restart then use the persistent storage feature which retains context variables over a restart. Every time you get a good value update a persistent flow value with the current time. When you get a bad value check the time against that flow variable and if greater than two hours trigger the error.

---

<div class="post-metadata">

**Author:** ![edstobi](https://avatars.discourse-cdn.com/v4/letter/e/aca169/32.png) [@edstobi](https://discourse.nodered.org/u/edstobi)\
**Post date:** [30 July 2022 18:51 UTC](https://discourse.nodered.org/t/if-the-value-is-below-a-reference-value-for-more-than-x-hours-then-do-something-fail-safe/65819/4 "2022-07-30T18:51:19Z")

</div>

Ahh!! THX  
persistent storage feature  
that's something new for me, I'll have to ask aunt google for more information.  
I haven't worked with it yet. This seems to be exactly what I am looking for

---

<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:** [30 July 2022 19:49 UTC](https://discourse.nodered.org/t/if-the-value-is-below-a-reference-value-for-more-than-x-hours-then-do-something-fail-safe/65819/5 "2022-07-30T19:49:34Z")

</div>

> [@A guide to understanding 'Persistent Context'](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115):
>
> This is something I wrote up after playing with the context options. I've filed a PR with some of this added to the Node-RED documentation but thought I'd release it here also. I hope people find it useful. Paul A guide to understanding 'Persistent Context' Prior to Node-RED v0.19.0, data could be stored in context as a global, flow or node. This was stored in memory and would be reset with a restart of Node-RED. As of v.0.19.0 you can now store node, flow and global context data in memory O…

---

<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:** [13 August 2022 19:49 UTC](https://discourse.nodered.org/t/if-the-value-is-below-a-reference-value-for-more-than-x-hours-then-do-something-fail-safe/65819/6 "2022-08-13T19:49:48Z")

</div>

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