# Waiting time function

**URL:** <https://discourse.nodered.org/t/waiting-time-function/24483>\
**Category:** General\
**Created:** [8 April 2020 19:47 UTC](https://discourse.nodered.org/t/waiting-time-function/24483 "2020-04-08T19:47:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mustafa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mustafa/32/538_2.png) [@mustafa](https://discourse.nodered.org/u/mustafa)\
**Post date:** [8 April 2020 19:47 UTC](https://discourse.nodered.org/t/waiting-time-function/24483/1 "2020-04-08T19:47:09Z")

</div>

Hi  
I have a project of distance measuring by HCR-04 and the output of this sensor sometime debouching  
so I want to do two things:  
first I want when the distance number fall below for example 25 ( better to be adjustable by slider or numeric) to stay for 5 seconds then the function will gives me output to trigger ON switch  
second if the distance exceed 98 ( also this adjustable ) and stays for 5 second then trigger OFF the same switch.  
any idea please for the above.  
below is the distance measurement flow:  
`[{"id":"25fb1bb5.549004","type":"rpi-srf","z":"a4cfb93a.9280e8","name":"Sensor","topic":"Calculate Percentage","pulse":"0.5","pins":"37,38","x":79,"y":60,"wires":[["ba72a0ea.33a89"]]},{"id":"ba72a0ea.33a89","type":"function","z":"a4cfb93a.9280e8","name":"Calculate Percentage","func":"var distance = parseInt(msg.payload);\nvar EmptyDist = 120 -distance;\nmsg.payload = parseInt((EmptyDist/100)*100);\n//msg.payload =(100- parseInt((temp/125)*100));\nreturn msg;\n","outputs":1,"noerr":0,"x":280,"y":60,"wires":[["3e0bf29c.4fedbe","3f62703.719d39"]]},{"id":"3e0bf29c.4fedbe","type":"ui_gauge","z":"a4cfb93a.9280e8","name":"","group":"b3442ac2.24b5b8","order":0,"width":"6","height":"6","gtype":"gage","title":"level","label":"Percentage","format":"{{value}}","min":0,"max":"100","colors":["#b52100","#e6e600","#42ca38"],"seg1":"","seg2":"","x":590,"y":60,"wires":[]},{"id":"b3442ac2.24b5b8","type":"ui_group","z":"","name":"Mustafa","tab":"fc4b1c12.afee3","disp":false,"width":"6","collapse":false},{"id":"fc4b1c12.afee3","type":"ui_tab","z":"","name":"Home","icon":"my dash","order":2}]`

---

<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:** [8 April 2020 22:18 UTC](https://discourse.nodered.org/t/waiting-time-function/24483/2 "2020-04-08T22:18:22Z")

</div>

Hi.

Suggestion: Replace the node with `inject` nodes that send the same message/s because people may not have that node you are using.

That makes it difficult to help you.

> [@mustafa](#):
>
> first I want when the distance number fall below for example 25 ( better to be adjustable by slider or numeric) to stay for 5 seconds then the function will gives me output to trigger ON switch  
> second if the distance exceed 98 ( also this adjustable ) and stays for 5 second then trigger OFF the same switch.

Could you try explaining that again?

I can't separate the two points and you have (ok, I get it) included too much speculation (or examples) of what you want.

I appreciate it means you are open to change, but it doesn't help someone understand the problem. (Well, more so: me)

So you want if that if the value is less than 25 ......... Then I get lost.

Could you put it something like:  
if value \< 25 .......  
if value \> 98 .......

It would help (me) a lot in parsing what you are wanting.

---

<div class="post-metadata">

**Author:** ![rromele](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rromele/32/18431_2.png) [@rromele](https://discourse.nodered.org/u/rromele)\
**Post date:** [8 April 2020 22:36 UTC](https://discourse.nodered.org/t/waiting-time-function/24483/3 "2020-04-08T22:36:18Z")

</div>

Hi,  
perhaps the hysteresis node could be useful.

---

<div class="post-metadata">

**Author:** ![mustafa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mustafa/32/538_2.png) [@mustafa](https://discourse.nodered.org/u/mustafa)\
**Post date:** [9 April 2020 10:21 UTC](https://discourse.nodered.org/t/waiting-time-function/24483/4 "2020-04-09T10:21:16Z")

</div>

Hi  
let me explain more clear as@Trying\_to\_learn  
I want to put a control for min and max level to my distance sensor which its output is a number and this number can reach to min and max lavel:  
if sensor output is \<25 for 5 second then the output must be string(A)  
if sensor output is \> 98 for 5 second then the output must be string (B)  
note:the 25 and 89 can be changed by slider switch or by numeric so it is not fix all the time

---

<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:** [9 April 2020 10:23 UTC](https://discourse.nodered.org/t/waiting-time-function/24483/5 "2020-04-09T10:23:23Z")

</div>

Ok, that helps.

But it is still vague because you haven't declared / specified the frequency the readings are taken.

It if is 5 seconds, it is a bit ..... academic.

If it is 100ms, it is another story.

---

<div class="post-metadata">

**Author:** ![mustafa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mustafa/32/538_2.png) [@mustafa](https://discourse.nodered.org/u/mustafa)\
**Post date:** [9 April 2020 10:28 UTC](https://discourse.nodered.org/t/waiting-time-function/24483/6 "2020-04-09T10:28:05Z")

</div>

Hi  
I don't know and that I'm asking for help  
the point is the sensor sometimes spike from max to min in one second and therefore I need the output to be in steady state for 5 second  
and same for max

---

<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:** [9 April 2020 10:32 UTC](https://discourse.nodered.org/t/waiting-time-function/24483/7 "2020-04-09T10:32:15Z")

</div>

You should see how often the _thing_ is spitting out messages.  
(thing / node.)

---

<div class="post-metadata">

**Author:** ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)\
**Post date:** [9 April 2020 10:32 UTC](https://discourse.nodered.org/t/waiting-time-function/24483/8 "2020-04-09T10:32:31Z")

</div>

> [@mustafa](#):
>
> the point is the sensor sometimes spike from max to min in one second and therefore I need the output to be in steady state for 5 second

Have a look at [node-red-contrib-aggregator (node) - Node-RED](https://flows.nodered.org/node/node-red-contrib-aggregator) node. It should be able to achieve what you want.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [9 April 2020 10:35 UTC](https://discourse.nodered.org/t/waiting-time-function/24483/9 "2020-04-09T10:35:51Z")

</div>

Or smooth node

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/a/ea2c713e3a9056300738b0897bae549b25ed8144.png)

---

<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:** [9 April 2020 11:09 UTC](https://discourse.nodered.org/t/waiting-time-function/24483/11 "2020-04-09T11:09:21Z")

</div>

Ok, fully working and tested flow:

Instructions:  
Press the two `inject` nodes (top left) to set the MAX and MIN values.  
They are set to nominal values just now.

All things now, press the `1` button (one of the test buttons)  
Wait 5 seconds and see if you get an `A` output.  
Press the `1` button again but then (before 5 seconds) press either the `12` or `25` button.  
(I'd suggest the `12` for now)  
You should not get an `A` output.

Repeat with the `25` button and get a `B` output.

The same with pressing either the `1` or `12` buttons to see you don't get the `B` output.  
Change the times (in the two `trigger` nodes to `minutes` rather than `seconds`.

Adjust the maximum and minimum values and things should work.

Another note!

Connect the _previous_ node's output (the one giving you the data) to **both** of the `switch` nodes.

If you don't there will be problems.

Code:

```auto
[{"id":"934a8949.41b7b8","type":"inject","z":"b4f1ccd4.5e8d1","name":"Maximum","topic":"maximum","payload":"20","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":2250,"wires":[["c6e005d8.acb5c"]]},{"id":"93b3be77.77b3d8","type":"inject","z":"b4f1ccd4.5e8d1","name":"Minimum","topic":"minimum","payload":"5","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":2290,"wires":[["f99d3ae2.d55358"]]},{"id":"c6e005d8.acb5c","type":"change","z":"b4f1ccd4.5e8d1","name":"Set MIN MAX","rules":[{"t":"set","p":"maximum","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":2250,"wires":[[]]},{"id":"8e9dfb73.3735f","type":"switch","z":"b4f1ccd4.5e8d1","name":"Min / Max","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"minimum","vt":"flow"},{"t":"gt","v":"maximum","vt":"flow"}],"checkall":"true","repair":false,"outputs":2,"x":620,"y":2370,"wires":[["85becf12.fdeae8"],["9cdd1163.6adbd"]],"outputLabels":["< min","> max"]},{"id":"85becf12.fdeae8","type":"trigger","z":"b4f1ccd4.5e8d1","op1":"","op2":"A","op1type":"nul","op2type":"str","duration":"5","extend":false,"units":"s","reset":"","bytopic":"all","name":"Less","x":840,"y":2320,"wires":[["f25bd8fc.36c8c8"]]},{"id":"9cdd1163.6adbd","type":"trigger","z":"b4f1ccd4.5e8d1","op1":"","op2":"B","op1type":"nul","op2type":"str","duration":"5","extend":false,"units":"s","reset":"","bytopic":"all","name":"More","x":840,"y":2420,"wires":[["5230155b.899454"]]},{"id":"45b88282.3cca84","type":"change","z":"b4f1ccd4.5e8d1","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":870,"y":2460,"wires":[["9cdd1163.6adbd"]]},{"id":"ac462799.3a9b18","type":"change","z":"b4f1ccd4.5e8d1","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":870,"y":2280,"wires":[["85becf12.fdeae8"]]},{"id":"f25bd8fc.36c8c8","type":"debug","z":"b4f1ccd4.5e8d1","name":"A","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1050,"y":2320,"wires":[]},{"id":"5230155b.899454","type":"debug","z":"b4f1ccd4.5e8d1","name":"B","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1050,"y":2420,"wires":[]},{"id":"d7d3c289.41ea28","type":"comment","z":"b4f1ccd4.5e8d1","name":"ADJUST TIMES AS NEEDED","info":"","x":910,"y":2370,"wires":[]},{"id":"5d2ab10f.124d08","type":"comment","z":"b4f1ccd4.5e8d1","name":"From sliders","info":"","x":150,"y":2210,"wires":[]},{"id":"a6ae9175.a6975","type":"inject","z":"b4f1ccd4.5e8d1","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":420,"y":2420,"wires":[["8e9dfb73.3735f","2adccf7.151e3b"]]},{"id":"f99d3ae2.d55358","type":"change","z":"b4f1ccd4.5e8d1","name":"Set MIN MAX","rules":[{"t":"set","p":"minimum","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":2290,"wires":[[]]},{"id":"dbb4ed7f.aab9f8","type":"inject","z":"b4f1ccd4.5e8d1","name":"","topic":"","payload":"12","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":420,"y":2460,"wires":[["8e9dfb73.3735f","2adccf7.151e3b"]]},{"id":"2adccf7.151e3b","type":"switch","z":"b4f1ccd4.5e8d1","name":"Min / Max","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"minimum","vt":"flow"},{"t":"lte","v":"maximum","vt":"flow"}],"checkall":"true","repair":false,"outputs":2,"x":620,"y":2410,"wires":[["ac462799.3a9b18"],["45b88282.3cca84"]],"outputLabels":["< min","> max"]},{"id":"d7185427.2e5e7","type":"inject","z":"b4f1ccd4.5e8d1","name":"","topic":"","payload":"25","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":420,"y":2500,"wires":[["8e9dfb73.3735f","2adccf7.151e3b"]]},{"id":"fea3378d.50a37","type":"comment","z":"b4f1ccd4.5e8d1","name":"For testing only","info":"","x":440,"y":2540,"wires":[]}]

```

---

<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:** [8 June 2020 11:09 UTC](https://discourse.nodered.org/t/waiting-time-function/24483/12 "2020-06-08T11:09:24Z")

</div>

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