# How to use the same button to turn the lights on/off in node red 

**URL:** <https://discourse.nodered.org/t/how-to-use-the-same-button-to-turn-the-lights-on-off-in-node-red/96968>\
**Category:** General\
**Created:** [8 May 2025 09:31 UTC](https://discourse.nodered.org/t/how-to-use-the-same-button-to-turn-the-lights-on-off-in-node-red/96968 "2025-05-08T09:31:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![toShiba0101](https://avatars.discourse-cdn.com/v4/letter/t/a4c791/32.png) [@toShiba0101](https://discourse.nodered.org/u/toShiba0101)\
**Post date:** [8 May 2025 09:31 UTC](https://discourse.nodered.org/t/how-to-use-the-same-button-to-turn-the-lights-on-off-in-node-red/96968/1 "2025-05-08T09:31:32Z")

</div>

Hi, I came across a one small problem. I want to turn on/off the the light system with the same button with node red, but I don’t know how. My idea is to use toggle button but don’t know how to read/keep last used state. So when the lights are off I want to turn them on, and turn them off if they were on. Feel free to help me LoL

---

<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 May 2025 09:39 UTC](https://discourse.nodered.org/t/how-to-use-the-same-button-to-turn-the-lights-on-off-in-node-red/96968/2 "2025-05-08T09:39:26Z")

</div>

Hi and welcome to Node-Red.

Storing states is called CONTEXT.

It will require a bit of coding in a `function` node.

The button always sends the same thing and the `function` node does all the work.

EXAMPLE code:

```auto
// read previous state and set to `0` if not set.
let x = context.get("state") || 0

if (x == 0)
{
    msg.payload = "OFF"
}
if (x == 1)
{
    msg.payload = "ON"
}
// toggle state from `0` to `1`
x = (x + 1) % 2
// save to context
context.set("state",x)

return msg

```

But there are a lot of ways to do it.

But just to get you started.

All the best.

---

<div class="post-metadata">

**Author:** ![wb666greene](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wb666greene/32/6534_2.png) [@wb666greene](https://discourse.nodered.org/u/wb666greene)\
**Post date:** [8 May 2025 09:49 UTC](https://discourse.nodered.org/t/how-to-use-the-same-button-to-turn-the-lights-on-off-in-node-red/96968/3 "2025-05-08T09:49:47Z")

</div>

I tend to use function nodes since I come from a C/C++/Python background, but you might find this documentation useful in using the flow option in the various dropdown properties: [https://nodered.org/docs/user-guide/context](https://nodered.org/docs/user-guide/context)

If you want to dive into function nodes, start here: [https://nodered.org/docs/user-guide/writing-functions](https://nodered.org/docs/user-guide/writing-functions)

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [8 May 2025 10:52 UTC](https://discourse.nodered.org/t/how-to-use-the-same-button-to-turn-the-lights-on-off-in-node-red/96968/4 "2025-05-08T10:52:32Z")

</div>

A super compact version for a function node 😉

```auto
const x = context.get("state") || 0;
msg.payload = ["OFF", "ON"][x];
context.set("state", 1-x);
return msg;

```

Any incoming message, will result in alternating ON OFF messages being output.

As the current state is stored in the function nodes "local" context, you can make multiple copies if you need them, and each will remember it's own state.

Example flow

```auto
[{"id":"77db1fa576cb54a2","type":"inject","z":"90084585.8983b8","name":"Trigger","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":580,"y":3420,"wires":[["9d562eb57f4f1ff0"]]},{"id":"82e85d0fcb517759","type":"debug","z":"90084585.8983b8","name":"debug 40","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1000,"y":3420,"wires":[]},{"id":"9d562eb57f4f1ff0","type":"function","z":"90084585.8983b8","name":"Toggle State","func":"const x = context.get(\"state\") || 0;\nmsg.payload = [\"OFF\", \"ON\"][x];\ncontext.set(\"state\", 1-x);\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":780,"y":3420,"wires":[["82e85d0fcb517759"]]}]

```

---

<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:** [8 May 2025 11:15 UTC](https://discourse.nodered.org/t/how-to-use-the-same-button-to-turn-the-lights-on-off-in-node-red/96968/5 "2025-05-08T11:15:55Z")

</div>

Personally I prefer to use ui-switch node rather than a button for things like this. The switch can output On or Off as required for the two states and also then the position of the switch tells you whether the light is on or off.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [8 May 2025 11:55 UTC](https://discourse.nodered.org/t/how-to-use-the-same-button-to-turn-the-lights-on-off-in-node-red/96968/6 "2025-05-08T11:55:35Z")

</div>

> [@Colin](#):
>
> prefer to use ui-switch

That assumes that you are using Dashboard 1 or 2 of course.  
 ![WinkEyeWinkGIF](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/8/e8dc5fbc3e34eb1ed046050d66bda0d0edc038f0.gif)

---

<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:** [8 May 2025 12:34 UTC](https://discourse.nodered.org/t/how-to-use-the-same-button-to-turn-the-lights-on-off-in-node-red/96968/7 "2025-05-08T12:34:14Z")

</div>

You might find this tutorial useful - I wrote it for my IoT students a couple of years ago.

> **[having\_fun\_with\_buttons.pdf](https://www.resources-area.co.uk/node-red-flows/having_fun_with_buttons.pdf)**
>
> 257.00 KB

Note: There is a link to the Node-RED flows on the very last page of the tutorial.

---

<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 May 2025 12:55 UTC](https://discourse.nodered.org/t/how-to-use-the-same-button-to-turn-the-lights-on-off-in-node-red/96968/8 "2025-05-08T12:55:22Z")

</div>

> [@toShiba0101](#):
>
> I want to turn on/off the the light system with the same button

Are these physical lights?  
Presumably you send a message "on" or "off" somewhere.  
What happens if you send "toggle"?

My light is controlled by an esp32 running tasmota. It does respond to "toggle".

---

<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:** [6 August 2025 12:56 UTC](https://discourse.nodered.org/t/how-to-use-the-same-button-to-turn-the-lights-on-off-in-node-red/96968/9 "2025-08-06T12:56:03Z")

</div>

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