# How to change state of the switch

**URL:** <https://discourse.nodered.org/t/how-to-change-state-of-the-switch/59514>\
**Category:** General\
**Created:** [7 March 2022 20:42 UTC](https://discourse.nodered.org/t/how-to-change-state-of-the-switch/59514 "2022-03-07T20:42:36Z")\
**Posts on this page:** 18\
**Page:** 2

<div class="post-metadata">

**Author:** ![alda](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@alda](https://discourse.nodered.org/u/alda)\
**Post date:** [8 March 2022 09:21 UTC](https://discourse.nodered.org/t/how-to-change-state-of-the-switch/59514/21 "2022-03-08T09:21:25Z")

</div>

I tried implement a trigger reset :

```auto
[{"id":"984c039df8c2b7ef","type":"ui_switch","z":"59fb2f9263e14661","name":"","label":"safety lock","tooltip":"","group":"c7a27394.057878","order":14,"width":4,"height":1,"passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"x":250,"y":1800,"wires":[["cc7a0523bdcbe803","c1a1e7b6e84ae7b4"]]},{"id":"9671f89989381e8e","type":"trigger","z":"59fb2f9263e14661","name":"","op1":"","op2":"false","op1type":"nul","op2type":"bool","duration":"3","extend":false,"overrideDelay":false,"units":"s","reset":"RESET","bytopic":"all","topic":"topic","outputs":1,"x":360,"y":1680,"wires":[["920b730df4b2b2f9","984c039df8c2b7ef"]]},{"id":"084f1df60dac2f7a","type":"change","z":"59fb2f9263e14661","name":"disable ","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1120,"y":1720,"wires":[["984c039df8c2b7ef","34cbe6f2c4a86cf6"]]},{"id":"cc7a0523bdcbe803","type":"change","z":"59fb2f9263e14661","name":"","rules":[{"t":"set","p":"enabled","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":1780,"wires":[["19d3ed706256be59","d5b9c0262ae4515c"]]},{"id":"19d3ed706256be59","type":"ui_button","z":"59fb2f9263e14661","name":"","group":"c7a27394.057878","order":15,"width":5,"height":1,"passthru":false,"label":"Action","tooltip":"","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"topic","topicType":"msg","x":710,"y":1780,"wires":[["ee2091dddcbe9962","2e5f5d069b9662c2"]]},{"id":"8e9f451f6117af1d","type":"function","z":"59fb2f9263e14661","name":"actual action goes on","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1040,"y":1780,"wires":[["084f1df60dac2f7a"]]},{"id":"920b730df4b2b2f9","type":"debug","z":"59fb2f9263e14661","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":1680,"wires":[]},{"id":"34cbe6f2c4a86cf6","type":"debug","z":"59fb2f9263e14661","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1290,"y":1720,"wires":[]},{"id":"d5b9c0262ae4515c","type":"debug","z":"59fb2f9263e14661","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":1820,"wires":[]},{"id":"c1a1e7b6e84ae7b4","type":"switch","z":"59fb2f9263e14661","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":210,"y":1680,"wires":[["9671f89989381e8e"]]},{"id":"ee2091dddcbe9962","type":"change","z":"59fb2f9263e14661","name":"reset","rules":[{"t":"set","p":"payload","pt":"msg","to":"RESET","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":890,"y":1720,"wires":[["9671f89989381e8e"]]},{"id":"2e5f5d069b9662c2","type":"delay","z":"59fb2f9263e14661","name":"","pauseType":"delay","timeout":"8","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":860,"y":1780,"wires":[["8e9f451f6117af1d"]]},{"id":"c7a27394.057878","type":"ui_group","name":"Alternate","tab":"bb73106a674aef89","order":1,"disp":true,"width":"9","collapse":false},{"id":"bb73106a674aef89","type":"ui_tab","name":"Button press examples","icon":"dashboard","order":41,"disabled":false,"hidden":false}]

```

added delay 8sec on the end to see how it works

---

<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 March 2022 09:22 UTC](https://discourse.nodered.org/t/how-to-change-state-of-the-switch/59514/22 "2022-03-08T09:22:35Z")

</div>

> [@alda](#):
>
> added delay 8sec on the end to see how it works

What does that achieve?

---

<div class="post-metadata">

**Author:** ![alda](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@alda](https://discourse.nodered.org/u/alda)\
**Post date:** [8 March 2022 09:24 UTC](https://discourse.nodered.org/t/how-to-change-state-of-the-switch/59514/23 "2022-03-08T09:24:01Z")

</div>

pocessing time is longer than time for reset to off when button isn't clicked  
so I can see that switch goes off when all is finished and reset of timer is working

---

<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 March 2022 09:25 UTC](https://discourse.nodered.org/t/how-to-change-state-of-the-switch/59514/24 "2022-03-08T09:25:09Z")

</div>

I fear you are making the easy more complicated than it needs to be.

---

<div class="post-metadata">

**Author:** ![alda](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@alda](https://discourse.nodered.org/u/alda)\
**Post date:** [8 March 2022 09:26 UTC](https://discourse.nodered.org/t/how-to-change-state-of-the-switch/59514/25 "2022-03-08T09:26:48Z")

</div>

☹  
No other chance how to reset trigger  
Delay 8 sec is only for debug

---

<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 March 2022 09:29 UTC](https://discourse.nodered.org/t/how-to-change-state-of-the-switch/59514/26 "2022-03-08T09:29:16Z")

</div>

![Screenshot from 2022-03-08 20-27-49](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/2/828729f2c85c49eb0f74176dd6484e3e880bbfcb.png)

It is going to be reset anyway because of the red line.

---

<div class="post-metadata">

**Author:** ![alda](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@alda](https://discourse.nodered.org/u/alda)\
**Post date:** [8 March 2022 09:34 UTC](https://discourse.nodered.org/t/how-to-change-state-of-the-switch/59514/27 "2022-03-08T09:34:33Z")

</div>

yes trigger must be reset because action (communication with main unit) started and switch state to off will be handled by function and disable on the end.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [8 March 2022 09:40 UTC](https://discourse.nodered.org/t/how-to-change-state-of-the-switch/59514/28 "2022-03-08T09:40:28Z")

</div>

The switch in this flow will enable or disable the button.  
Pressing the button will turn the switch off and so disable the button.  
After ten seconds the switch turns off anyway, disabling the button.

I think that's what you want, but you may need a ui-control node to ensure that the switch and button get initialised to OFF and disabled.

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/5/951c5ad3b2afb9e8bf46d45b144648907dfc8b63.jpeg)

```auto
[{"id":"0859c4d4a89178b4","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"95dec0d3de4bcbec","type":"group","z":"0859c4d4a89178b4","name":"These nodes use string ON/OFF","style":{"label":true},"nodes":["e1130c8f9458f535","5aaf17dda573ee51","ae99461167c10b19"],"x":114,"y":59,"w":212,"h":202},{"id":"1175c4771e01362c","type":"group","z":"0859c4d4a89178b4","name":"Convert to msg.enabled true/false","style":{"label":true},"nodes":["fb381298223f4873","b1ff96a3f055d01e"],"x":374,"y":119,"w":412,"h":82},{"id":"fb381298223f4873","type":"ui_button","z":"0859c4d4a89178b4","g":"1175c4771e01362c","name":"","group":"33a8383128087223","order":1,"width":0,"height":0,"passthru":false,"label":"button","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"Pressed","payloadType":"str","topic":"topic","topicType":"msg","x":710,"y":160,"wires":[["ae99461167c10b19","033e3fc568c6299b"]]},{"id":"e1130c8f9458f535","type":"ui_switch","z":"0859c4d4a89178b4","g":"95dec0d3de4bcbec","name":"","label":"switch","tooltip":"","group":"33a8383128087223","order":2,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"enable","topicType":"msg","style":"","onvalue":"ON","onvalueType":"str","onicon":"","oncolor":"","offvalue":"OFF","offvalueType":"str","officon":"","offcolor":"","animate":false,"className":"","x":230,"y":160,"wires":[["5aaf17dda573ee51","b1ff96a3f055d01e"]]},{"id":"5aaf17dda573ee51","type":"trigger","z":"0859c4d4a89178b4","g":"95dec0d3de4bcbec","name":"Switch TImeout","op1":"","op2":"OFF","op1type":"nul","op2type":"str","duration":"10","extend":false,"overrideDelay":false,"units":"s","reset":"OFF","bytopic":"all","topic":"topic","outputs":1,"x":220,"y":100,"wires":[["e1130c8f9458f535"]]},{"id":"ae99461167c10b19","type":"change","z":"0859c4d4a89178b4","g":"95dec0d3de4bcbec","name":"Disable both","rules":[{"t":"set","p":"payload","pt":"msg","to":"OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":230,"y":220,"wires":[["e1130c8f9458f535","b1ff96a3f055d01e"]]},{"id":"b1ff96a3f055d01e","type":"change","z":"0859c4d4a89178b4","g":"1175c4771e01362c","name":"ON/OFF to true/false","rules":[{"t":"move","p":"payload","pt":"msg","to":"enabled","tot":"msg"},{"t":"change","p":"enabled","pt":"msg","from":"ON","fromt":"str","to":"true","tot":"bool"},{"t":"change","p":"enabled","pt":"msg","from":"OFF","fromt":"str","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":160,"wires":[["fb381298223f4873"]]},{"id":"033e3fc568c6299b","type":"debug","z":"0859c4d4a89178b4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":890,"y":160,"wires":[]},{"id":"33a8383128087223","type":"ui_group","name":"Group 1","tab":"1377960af0a72398","order":1,"disp":true,"width":6},{"id":"1377960af0a72398","type":"ui_tab","name":"OSTATNÍ ZAŘÍZENÍ","icon":"dashboard","order":3,"disabled":false,"hidden":false}]

```

Edit - the wire between "Disable both" and "ON/OFF to true/false" is unnecessary.

---

<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 March 2022 09:40 UTC](https://discourse.nodered.org/t/how-to-change-state-of-the-switch/59514/29 "2022-03-08T09:40:45Z")

</div>

![Screenshot from 2022-03-08 20-39-04](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/8/181e04e0045ec6e48200c851f8d480b41fd74274.png)

That node is not correctly named. Not a big deal, but it isn't doing that.  
All that node is doing is sending a `false` payload back to the `switch` node to turn it off.

---

<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 March 2022 09:42 UTC](https://discourse.nodered.org/t/how-to-change-state-of-the-switch/59514/30 "2022-03-08T09:42:52Z")

</div>

He can't disable the `switch` node. Because if that happens, it's all locked and you can't do anything.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [8 March 2022 09:47 UTC](https://discourse.nodered.org/t/how-to-change-state-of-the-switch/59514/31 "2022-03-08T09:47:03Z")

</div>

The switch is not disabled, it's turned off. Try it and see.  
There just wasn't enough space to name the node "Disable button and turn switch off"

---

<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 March 2022 09:49 UTC](https://discourse.nodered.org/t/how-to-change-state-of-the-switch/59514/32 "2022-03-08T09:49:12Z")

</div>

Ok, sorry, I was just looking at the picture.

That you called the `change` node below the `switch` `Disable both` rather than `turn both off` threw me.

---

<div class="post-metadata">

**Author:** ![alda](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@alda](https://discourse.nodered.org/u/alda)\
**Post date:** [8 March 2022 09:50 UTC](https://discourse.nodered.org/t/how-to-change-state-of-the-switch/59514/33 "2022-03-08T09:50:21Z")

</div>

yes looks good  
this is clever solution for Switch Timeout :

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/6/461ae144d25cb1127e4adea2894758f0b7a217ab.png)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [8 March 2022 09:50 UTC](https://discourse.nodered.org/t/how-to-change-state-of-the-switch/59514/34 "2022-03-08T09:50:25Z")

</div>

It doesn't turn the button off, it disables it.  
It doesn't disable the switch, it turns it off.  
Pick your own name for the node.

---

<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 March 2022 09:51 UTC](https://discourse.nodered.org/t/how-to-change-state-of-the-switch/59514/35 "2022-03-08T09:51:28Z")

</div>

Well to me `msg.payload` is very different to `msg.disable`.

![Screenshot from 2022-03-08 20-52-48](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/d/9def2ad20671fb326ee736d6b0180b58c5d55576.png)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [8 March 2022 09:56 UTC](https://discourse.nodered.org/t/how-to-change-state-of-the-switch/59514/36 "2022-03-08T09:56:10Z")

</div>

Fair enough.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [8 March 2022 10:15 UTC](https://discourse.nodered.org/t/how-to-change-state-of-the-switch/59514/37 "2022-03-08T10:15:48Z")

</div>

Node renamed, unnecessary wire removed.

 ![Untitled 2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/7/e71113652ef2fadb70513e54786cdb7c0eb0be42.jpeg)

```auto
[{"id":"0859c4d4a89178b4","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"95dec0d3de4bcbec","type":"group","z":"0859c4d4a89178b4","name":"These nodes use string ON/OFF","style":{"label":true},"nodes":["e1130c8f9458f535","5aaf17dda573ee51","ae99461167c10b19"],"x":114,"y":59,"w":212,"h":202},{"id":"1175c4771e01362c","type":"group","z":"0859c4d4a89178b4","name":"Convert to msg.enabled true/false","style":{"label":true},"nodes":["fb381298223f4873","b1ff96a3f055d01e"],"x":374,"y":119,"w":412,"h":82},{"id":"fb381298223f4873","type":"ui_button","z":"0859c4d4a89178b4","g":"1175c4771e01362c","name":"","group":"33a8383128087223","order":1,"width":0,"height":0,"passthru":false,"label":"button","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"Pressed","payloadType":"str","topic":"topic","topicType":"msg","x":710,"y":160,"wires":[["ae99461167c10b19","033e3fc568c6299b"]]},{"id":"e1130c8f9458f535","type":"ui_switch","z":"0859c4d4a89178b4","g":"95dec0d3de4bcbec","name":"","label":"switch","tooltip":"","group":"33a8383128087223","order":2,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"enable","topicType":"msg","style":"","onvalue":"ON","onvalueType":"str","onicon":"","oncolor":"","offvalue":"OFF","offvalueType":"str","officon":"","offcolor":"","animate":false,"className":"","x":230,"y":160,"wires":[["5aaf17dda573ee51","b1ff96a3f055d01e"]]},{"id":"5aaf17dda573ee51","type":"trigger","z":"0859c4d4a89178b4","g":"95dec0d3de4bcbec","name":"Switch TImeout","op1":"","op2":"OFF","op1type":"nul","op2type":"str","duration":"10","extend":false,"overrideDelay":false,"units":"s","reset":"OFF","bytopic":"all","topic":"topic","outputs":1,"x":220,"y":100,"wires":[["e1130c8f9458f535"]]},{"id":"ae99461167c10b19","type":"change","z":"0859c4d4a89178b4","g":"95dec0d3de4bcbec","name":"Switch OFF","rules":[{"t":"set","p":"payload","pt":"msg","to":"OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":230,"y":220,"wires":[["e1130c8f9458f535"]]},{"id":"b1ff96a3f055d01e","type":"change","z":"0859c4d4a89178b4","g":"1175c4771e01362c","name":"ON/OFF to true/false","rules":[{"t":"move","p":"payload","pt":"msg","to":"enabled","tot":"msg"},{"t":"change","p":"enabled","pt":"msg","from":"ON","fromt":"str","to":"true","tot":"bool"},{"t":"change","p":"enabled","pt":"msg","from":"OFF","fromt":"str","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":160,"wires":[["fb381298223f4873"]]},{"id":"033e3fc568c6299b","type":"debug","z":"0859c4d4a89178b4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":890,"y":160,"wires":[]},{"id":"33a8383128087223","type":"ui_group","name":"Group 1","tab":"1377960af0a72398","order":1,"disp":true,"width":6},{"id":"1377960af0a72398","type":"ui_tab","name":"OSTATNÍ ZAŘÍZENÍ","icon":"dashboard","order":3,"disabled":false,"hidden":false}]

```

---

<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:** [22 March 2022 10:16 UTC](https://discourse.nodered.org/t/how-to-change-state-of-the-switch/59514/38 "2022-03-22T10:16:12Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/how-to-change-state-of-the-switch/59514.md?page=1)
