# Control the rules of Switch node with input from dashboard.

**URL:** <https://discourse.nodered.org/t/control-the-rules-of-switch-node-with-input-from-dashboard/31948>\
**Category:** General\
**Created:** [24 August 2020 12:45 UTC](https://discourse.nodered.org/t/control-the-rules-of-switch-node-with-input-from-dashboard/31948 "2020-08-24T12:45:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kushal](https://avatars.discourse-cdn.com/v4/letter/k/35a633/32.png) [@Kushal](https://discourse.nodered.org/u/Kushal)\
**Post date:** [24 August 2020 12:45 UTC](https://discourse.nodered.org/t/control-the-rules-of-switch-node-with-input-from-dashboard/31948/1 "2020-08-24T12:45:57Z")

</div>

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.

 ![flow example](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/6/f68853d751e3ab567c1ab19323ff3143d37e9f0d.png) ![Switch node settings](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/8/78c417875a935bd7edc9612d41d7ed6ac3d3d1f2.png)

---

<div class="post-metadata">

**Author:** ![harelabb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/harelabb/32/27450_2.png) [@harelabb](https://discourse.nodered.org/u/harelabb)\
**Post date:** [24 August 2020 20:22 UTC](https://discourse.nodered.org/t/control-the-rules-of-switch-node-with-input-from-dashboard/31948/2 "2020-08-24T20:22:09Z")

</div>

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.

```auto
[{"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}]

```

 ![minmax](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/9/49e134c3c4c2620ea901810c0a35abb90a54e3b0.png)

Some more nodes than you hoped for....

---

<div class="post-metadata">

**Author:** ![Kushal](https://avatars.discourse-cdn.com/v4/letter/k/35a633/32.png) [@Kushal](https://discourse.nodered.org/u/Kushal)\
**Post date:** [25 August 2020 02:38 UTC](https://discourse.nodered.org/t/control-the-rules-of-switch-node-with-input-from-dashboard/31948/3 "2020-08-25T02:38:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [25 August 2020 05:19 UTC](https://discourse.nodered.org/t/control-the-rules-of-switch-node-with-input-from-dashboard/31948/4 "2020-08-25T05:19:50Z")

</div>

> [@Kushal](#):
>
> 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 :

```auto
[{"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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/d/adfd17e007370620e1dc45ce80d111c321e79c52.png)

---

<div class="post-metadata">

**Author:** ![Kushal](https://avatars.discourse-cdn.com/v4/letter/k/35a633/32.png) [@Kushal](https://discourse.nodered.org/u/Kushal)\
**Post date:** [25 August 2020 09:57 UTC](https://discourse.nodered.org/t/control-the-rules-of-switch-node-with-input-from-dashboard/31948/5 "2020-08-25T09:57:18Z")

</div>

Ok. Thanks for such an elaborate explaination.

---

<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:** [24 October 2020 09:57 UTC](https://discourse.nodered.org/t/control-the-rules-of-switch-node-with-input-from-dashboard/31948/6 "2020-10-24T09:57:22Z")

</div>

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