Control the rules of Switch node with input from dashboard.

I have a switch node, which has 2 rules. Its checks whether

  1. Sensor value <= X(number)
  2. Sensor value >= Y(number)

I wish to set the X and Y with the Dashboard Numeric node. So that I will set the value of X and Y to any number I want from my dashboard.

Sorry if this is very basic, I just started learning node-red.
Please help. Thanks.

You can use flow variables to store the min/max values. And maybe a function node in stead of the switch node.
The min/max values can be initialised on flow start.

[{"id":"57da015.5e0b8","type":"change","z":"a42ecbde.f79ce8","name":"","rules":[{"t":"set","p":"min_value","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":770,"y":760,"wires":[[]]},{"id":"18fbc1fb.065b8e","type":"change","z":"a42ecbde.f79ce8","name":"","rules":[{"t":"set","p":"max_value","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":770,"y":800,"wires":[[]]},{"id":"11828295.edf81d","type":"function","z":"a42ecbde.f79ce8","name":"split","func":"min = flow.get(\"min_value\") || 0\nmax = flow.get(\"max_value\") || 0\nmsgout = [null, null]\nvalue = msg.payload\nmsg = {payload:value, min:min, max:max}\nif (value < min) msgout[0] = msg \nif (value > max) msgout[1] = msg\nreturn msgout;","outputs":2,"noerr":0,"x":530,"y":920,"wires":[["4181f21c.24eadc"],["e52558f.97d6fa8"]]},{"id":"3bb64593.60b48a","type":"ui_numeric","z":"a42ecbde.f79ce8","name":"","label":"Low limit","tooltip":"","group":"1594d936.db51f7","order":3,"width":0,"height":0,"wrap":false,"passthru":true,"topic":"","format":"{{value}}","min":0,"max":"30","step":1,"x":580,"y":760,"wires":[["57da015.5e0b8"]]},{"id":"8678ec7a.5b2f2","type":"ui_numeric","z":"a42ecbde.f79ce8","name":"","label":"High limit","tooltip":"","group":"1594d936.db51f7","order":4,"width":0,"height":0,"wrap":false,"passthru":true,"topic":"","format":"{{value}}","min":0,"max":"30","step":1,"x":580,"y":800,"wires":[["18fbc1fb.065b8e"]]},{"id":"4181f21c.24eadc","type":"debug","z":"a42ecbde.f79ce8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload & ' is smaller than ' & min","targetType":"jsonata","x":780,"y":900,"wires":[]},{"id":"3af1a035.cebc2","type":"inject","z":"a42ecbde.f79ce8","name":"","topic":"min_value","payload":"5","payloadType":"num","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":370,"y":760,"wires":[["3bb64593.60b48a"]]},{"id":"9aed0f01.33d07","type":"inject","z":"a42ecbde.f79ce8","name":"","topic":"max_value","payload":"10","payloadType":"num","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":380,"y":800,"wires":[["8678ec7a.5b2f2"]]},{"id":"f14fbdcf.308d","type":"inject","z":"a42ecbde.f79ce8","name":"","topic":"","payload":"4","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":350,"y":880,"wires":[["11828295.edf81d"]]},{"id":"7bac731f.bcd04c","type":"inject","z":"a42ecbde.f79ce8","name":"","topic":"","payload":"12","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":350,"y":960,"wires":[["11828295.edf81d"]]},{"id":"e52558f.97d6fa8","type":"debug","z":"a42ecbde.f79ce8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload &' is greater than '& max","targetType":"jsonata","x":780,"y":940,"wires":[]},{"id":"4edc8fb7.a24e8","type":"inject","z":"a42ecbde.f79ce8","name":"","topic":"","payload":"7","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":350,"y":920,"wires":[["11828295.edf81d"]]},{"id":"1594d936.db51f7","type":"ui_group","z":"","name":"PyExec","tab":"f49df05b.52ac2","order":1,"disp":true,"width":"6","collapse":false},{"id":"f49df05b.52ac2","type":"ui_tab","z":"","name":"PyExex","icon":"dashboard","disabled":false,"hidden":false}]

Some more nodes than you hoped for....

1 Like

Thank you so much! This is working fine! But are you sure we cannot do the same with the inbuilt switch node?

You can .. but you have so much more control with a function node in manipulating your output message when a condition triggers.

Expanding on @harelabb's solution and adding a switch node example :

[{"id":"1b50c155.683e1f","type":"change","z":"9f2f6aea.6d2be","name":"","rules":[{"t":"set","p":"min_value","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":140,"wires":[[]]},{"id":"3641afb1.e10378","type":"change","z":"9f2f6aea.6d2be","name":"","rules":[{"t":"set","p":"max_value","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":200,"wires":[[]]},{"id":"c8e7eb4e.bf4dd8","type":"function","z":"9f2f6aea.6d2be","name":"split","func":"min = flow.get(\"min_value\") || 0\nmax = flow.get(\"max_value\") || 0\nmsgout = [null, null]\nvalue = msg.payload\nmsg = {payload:value, min:min, max:max}\nif (value < min) msgout[0] = msg \nif (value > max) msgout[1] = msg\nreturn msgout;","outputs":2,"noerr":0,"initialize":"","finalize":"","x":490,"y":320,"wires":[["61719b0.302e064"],["2ca6ccab.231b1c"]]},{"id":"4b0357c1.25209","type":"ui_numeric","z":"9f2f6aea.6d2be","name":"","label":"Low limit","tooltip":"","group":"9a907f8e.042118","order":3,"width":0,"height":0,"wrap":false,"passthru":true,"topic":"","format":"{{value}}","min":0,"max":"30","step":1,"x":540,"y":140,"wires":[["1b50c155.683e1f"]]},{"id":"eae03fa8.a6b31","type":"ui_numeric","z":"9f2f6aea.6d2be","name":"","label":"High limit","tooltip":"","group":"9a907f8e.042118","order":4,"width":0,"height":0,"wrap":false,"passthru":true,"topic":"","format":"{{value}}","min":0,"max":"30","step":1,"x":540,"y":200,"wires":[["3641afb1.e10378"]]},{"id":"61719b0.302e064","type":"debug","z":"9f2f6aea.6d2be","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload & ' is smaller than ' & min","targetType":"jsonata","statusVal":"","statusType":"auto","x":740,"y":300,"wires":[]},{"id":"43a8592a.1eeb4","type":"inject","z":"9f2f6aea.6d2be","name":"","props":[{"p":"payload","v":"5","vt":"num"},{"p":"topic","v":"min_value","vt":"string"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"min_value","payload":"5","payloadType":"num","x":330,"y":140,"wires":[["4b0357c1.25209"]]},{"id":"e52576d8.8e664","type":"inject","z":"9f2f6aea.6d2be","name":"","repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"max_value","payload":"10","payloadType":"num","x":340,"y":200,"wires":[["eae03fa8.a6b31"]]},{"id":"58520c67.d2bb44","type":"inject","z":"9f2f6aea.6d2be","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"4","payloadType":"num","x":310,"y":280,"wires":[["c8e7eb4e.bf4dd8"]]},{"id":"99986001.319858","type":"inject","z":"9f2f6aea.6d2be","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"12","payloadType":"num","x":310,"y":360,"wires":[["c8e7eb4e.bf4dd8"]]},{"id":"2ca6ccab.231b1c","type":"debug","z":"9f2f6aea.6d2be","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload &' is greater than '& max","targetType":"jsonata","x":740,"y":340,"wires":[]},{"id":"94fcad7c.958ef8","type":"inject","z":"9f2f6aea.6d2be","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"7","payloadType":"num","x":310,"y":320,"wires":[["c8e7eb4e.bf4dd8"]]},{"id":"172166bc.cbe381","type":"switch","z":"9f2f6aea.6d2be","name":"","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"min_value","vt":"flow"},{"t":"gt","v":"max_value","vt":"flow"}],"checkall":"true","repair":false,"outputs":2,"x":550,"y":500,"wires":[["f8646a7a.3963b"],["492fb9e.3e090c8"]]},{"id":"658460da.742e48","type":"inject","z":"9f2f6aea.6d2be","name":"","props":[{"p":"payload","v":"4","vt":"num"},{"p":"topic","v":"","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"4","payloadType":"num","x":290,"y":460,"wires":[["172166bc.cbe381"]]},{"id":"3030af4d.3fd018","type":"inject","z":"9f2f6aea.6d2be","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"12","payloadType":"num","x":290,"y":540,"wires":[["172166bc.cbe381"]]},{"id":"fb32e41e.ec65a8","type":"inject","z":"9f2f6aea.6d2be","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"7","payloadType":"num","x":290,"y":500,"wires":[["172166bc.cbe381"]]},{"id":"f8646a7a.3963b","type":"debug","z":"9f2f6aea.6d2be","name":"less than min_value","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":800,"y":460,"wires":[]},{"id":"492fb9e.3e090c8","type":"debug","z":"9f2f6aea.6d2be","name":"more than max_value","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":800,"y":540,"wires":[]},{"id":"9a907f8e.042118","type":"ui_group","z":"","name":"PyExec","tab":"ef8f634b.f4335","order":1,"disp":true,"width":"6","collapse":false},{"id":"ef8f634b.f4335","type":"ui_tab","z":"","name":"PyExex","icon":"dashboard","disabled":false,"hidden":false}]

The important thing is to save your min_value and max_value as context flow data upon initialization of the flow otherwise neither the function node nor the switch node will know what min_value and max_value are

Screenshot_2020-08-25 Node-RED 192 168 1 160

Ok. Thanks for such an elaborate explaination.

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