# Switch node to toggle light switch on/off

**URL:** <https://discourse.nodered.org/t/switch-node-to-toggle-light-switch-on-off/83657>\
**Category:** General\
**Created:** [13 December 2023 06:08 UTC](https://discourse.nodered.org/t/switch-node-to-toggle-light-switch-on-off/83657 "2023-12-13T06:08:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sirhc](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sirhc](https://discourse.nodered.org/u/Sirhc)\
**Post date:** [13 December 2023 06:08 UTC](https://discourse.nodered.org/t/switch-node-to-toggle-light-switch-on-off/83657/1 "2023-12-13T06:08:01Z")

</div>

Hello

I have a zigbee controlled light switch. A msg.payload {"state": "OFF"} turns the switch off and a payload with {"state": "ON turns it on"}

I then want to a dashboard with a button. Pressing the button once turns the light on, pressing the same button again turns the light off.

I have the button and dashboard working, but not sure how to make the same button toggle the state on/off.

Here is my flow currently:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/8/78d55a7495eb28f0d4277b9915c50dc752436a9f.png)

The first node is status from the switch everytime it is initiated.

The 2nd node is a function node to change the colour of the light switch on the dashboard (i.e. press it once, it's green (on), press it again, it's red (off)).

The 3rd node is the UI dashboard button

The 4th node (this is where I am stuck), needs to turn the light on/off when pressing the same UI dashboard.

The 5th node is passing the payload from the 4th node to the light switch.

Any ideas on what to do here on the 4th node?

---

<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:** [13 December 2023 06:32 UTC](https://discourse.nodered.org/t/switch-node-to-toggle-light-switch-on-off/83657/2 "2023-12-13T06:32:15Z")

</div>

Does the light not accept a command "TOGGLE"?

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [13 December 2023 06:33 UTC](https://discourse.nodered.org/t/switch-node-to-toggle-light-switch-on-off/83657/3 "2023-12-13T06:33:23Z")

</div>

Here's a link to a thread about buttons you might find useful...

> [@One Button toggle to turn light on and off](https://discourse.nodered.org/t/one-button-toggle-to-turn-light-on-and-off/62050/2):
>
> You will need to store the previous state so that you can send the opposite state. Use context for this. e.g. [chrome\_dR7B1kkz48] [{"id":"83746a428adcbc17","type":"inject","z":"7eecf5f1d763605a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":810,"y":432,"wires":[["056008d289d9ada1"]]},{"id":"056008d289d9ada1","type":"function","z":"7eecf5f1d763605a","name":"toggle on/off","fu…

Here's the link to the tutorial I wrote for my IoT students about implementing different types of buttons...  
[http://www.resources-area.co.uk/node-red-flows/having\_fun\_with\_buttons.pdf](http://www.resources-area.co.uk/node-red-flows/having_fun_with_buttons.pdf)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 December 2023 11:33 UTC](https://discourse.nodered.org/t/switch-node-to-toggle-light-switch-on-off/83657/4 "2023-12-13T11:33:45Z")

</div>

> [@Sirhc](#):
>
> I then want to a dashboard with a button. Pressing the button once turns the light on, pressing the same button again turns the light off.

It sounds as if what you actually want is a switch not a button. Then you will not have to mess about making it toggle, and will have feedback as to whether it is on or off.

---

<div class="post-metadata">

**Author:** ![Sirhc](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sirhc](https://discourse.nodered.org/u/Sirhc)\
**Post date:** [15 December 2023 08:42 UTC](https://discourse.nodered.org/t/switch-node-to-toggle-light-switch-on-off/83657/5 "2023-12-15T08:42:35Z")

</div>

Thanks - managed to do it with global variables!

---

<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:** [13 February 2024 08:42 UTC](https://discourse.nodered.org/t/switch-node-to-toggle-light-switch-on-off/83657/6 "2024-02-13T08:42:57Z")

</div>

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