# How to make a button do the action ONLY if the the switch is switched on?

**URL:** <https://discourse.nodered.org/t/how-to-make-a-button-do-the-action-only-if-the-the-switch-is-switched-on/63290>\
**Category:** General\
**Created:** [31 May 2022 09:27 UTC](https://discourse.nodered.org/t/how-to-make-a-button-do-the-action-only-if-the-the-switch-is-switched-on/63290 "2022-05-31T09:27:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lumanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lumanga/32/58334_2.png) [@lumanga](https://discourse.nodered.org/u/lumanga)\
**Post date:** [31 May 2022 09:27 UTC](https://discourse.nodered.org/t/how-to-make-a-button-do-the-action-only-if-the-the-switch-is-switched-on/63290/1 "2022-05-31T09:27:57Z")

</div>

Hi All,  
I'm in trouble with this project.  
I just wanto to have a Switch, plus a Button.  
I want that pressing the button, the button do the action ONLY if the the switch is switched on! (Is a kind of safety)  
I try in different way but I'm not able to do this.  
Could someone help me giving the easyste and simplest solution?

---

<div class="post-metadata">

**Author:** ![Napest](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/napest/32/55656_2.png) [@Napest](https://discourse.nodered.org/u/Napest)\
**Post date:** [31 May 2022 09:41 UTC](https://discourse.nodered.org/t/how-to-make-a-button-do-the-action-only-if-the-the-switch-is-switched-on/63290/2 "2022-05-31T09:41:42Z")

</div>

Hi Lumanga,

it seems to me, that you have to use context variable. Please import the attached flow and inspect nodes.

```auto
[{"id":"174aad89f01f41e5","type":"ui_text","z":"ef1fc105685aed46","group":"e924fd1fd06cf83a","order":2,"width":0,"height":0,"name":"","label":"Button msg:","format":"{{msg.payload}}","layout":"row-spread","className":"","x":710,"y":360,"wires":[]},{"id":"e924fd1fd06cf83a","type":"ui_group","name":"Default","tab":"854bd9cd49df7ac1","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"854bd9cd49df7ac1","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

```

I hope it helps.

Stepan

---

<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:** [31 May 2022 11:01 UTC](https://discourse.nodered.org/t/how-to-make-a-button-do-the-action-only-if-the-the-switch-is-switched-on/63290/3 "2022-05-31T11:01:59Z")

</div>

An alternative is to use a node-red-contrib-simple-gate node (which you have to install via Manage Palette).  
It can be set up so that an ON message from the switch opens the gate, allowing messages from the button to pass and OFF closes it, blocking the button messages.

 ![Untitled 2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/6/1619ac083270308ea20fa89430e820b865c2a875.jpeg)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [31 May 2022 15:39 UTC](https://discourse.nodered.org/t/how-to-make-a-button-do-the-action-only-if-the-the-switch-is-switched-on/63290/4 "2022-05-31T15:39:28Z")

</div>

Or use a join node and a switch node.  
e.g.

```auto
[{"id":"725512a1.b50eec","type":"ui_switch","z":"bf9e1e33.030598","name":"","label":"switch","tooltip":"","group":"2d4fe667.28f8ba","order":8,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"switch","topicType":"str","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"className":"","x":190,"y":2080,"wires":[["6dc8aa87.cc473c"]]},{"id":"6dc8aa87.cc473c","type":"join","z":"bf9e1e33.030598","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":320,"y":2120,"wires":[["9056566f.da35d8"]]},{"id":"8ce1886b.667c78","type":"ui_button","z":"bf9e1e33.030598","name":"","group":"2d4fe667.28f8ba","order":9,"width":0,"height":0,"passthru":false,"label":"button","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"the payload","payloadType":"str","topic":"button","topicType":"str","x":190,"y":2180,"wires":[["6dc8aa87.cc473c"]]},{"id":"9056566f.da35d8","type":"switch","z":"bf9e1e33.030598","name":"","property":"payload.switch","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":460,"y":2120,"wires":[["c4eb9de8.b47e3"]]},{"id":"c4eb9de8.b47e3","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"move","p":"payload.button","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":2120,"wires":[["93e51fcb.ead128"]]},{"id":"93e51fcb.ead128","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":580,"y":1920,"wires":[]},{"id":"2d4fe667.28f8ba","type":"ui_group","name":"demo","tab":"1caa8458.b17814","order":1,"disp":true,"width":"12","collapse":false},{"id":"1caa8458.b17814","type":"ui_tab","name":"Demo","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

```

or use the switch to enable / diable the button  
e.g.

```auto
[{"id":"8ce1886b.667c78","type":"ui_button","z":"bf9e1e33.030598","name":"","group":"2d4fe667.28f8ba","order":9,"width":0,"height":0,"passthru":false,"label":"button","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"the payload","payloadType":"str","topic":"button","topicType":"str","x":160,"y":1980,"wires":[["93e51fcb.ead128"]]},{"id":"c4eb9de8.b47e3","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"enabled","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":2020,"wires":[["8ce1886b.667c78"]]},{"id":"93e51fcb.ead128","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":580,"y":1920,"wires":[]},{"id":"725512a1.b50eec","type":"ui_switch","z":"bf9e1e33.030598","name":"","label":"switch","tooltip":"","group":"2d4fe667.28f8ba","order":8,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"switch","topicType":"str","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"className":"","x":160,"y":2020,"wires":[["c4eb9de8.b47e3"]]},{"id":"2d4fe667.28f8ba","type":"ui_group","name":"demo","tab":"1caa8458.b17814","order":1,"disp":true,"width":"12","collapse":false},{"id":"1caa8458.b17814","type":"ui_tab","name":"Demo","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

```

---

<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:** [31 May 2022 16:04 UTC](https://discourse.nodered.org/t/how-to-make-a-button-do-the-action-only-if-the-the-switch-is-switched-on/63290/5 "2022-05-31T16:04:33Z")

</div>

> use the switch to enable / disable the button  
> Genius 🤩

---

<div class="post-metadata">

**Author:** ![lumanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lumanga/32/58334_2.png) [@lumanga](https://discourse.nodered.org/u/lumanga)\
**Post date:** [31 May 2022 16:40 UTC](https://discourse.nodered.org/t/how-to-make-a-button-do-the-action-only-if-the-the-switch-is-switched-on/63290/6 "2022-05-31T16:40:38Z")

</div>

AMAZING solution! Incredible... thanks a lot!

---

<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:** [14 June 2022 16:40 UTC](https://discourse.nodered.org/t/how-to-make-a-button-do-the-action-only-if-the-the-switch-is-switched-on/63290/7 "2022-06-14T16:40:59Z")

</div>

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