Waiting time function

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}]

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.

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.

Hi,
perhaps the hysteresis node could be useful.

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

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.

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

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

Have a look at node-red-contrib-aggregator (node) - Node-RED node. It should be able to achieve what you want.

1 Like

Or smooth node

2 Likes

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:

[{"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":[]}]
1 Like

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