# One Button toggle to turn light on and off

**URL:** <https://discourse.nodered.org/t/one-button-toggle-to-turn-light-on-and-off/62050>\
**Category:** General\
**Tags:** home-assistant\
**Created:** [2 May 2022 08:15 UTC](https://discourse.nodered.org/t/one-button-toggle-to-turn-light-on-and-off/62050 "2022-05-02T08:15:34Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 May 2022 08:31 UTC](https://discourse.nodered.org/t/one-button-toggle-to-turn-light-on-and-off/62050/2 "2022-05-02T08:31:29Z")

</div>

> [@DiGRem](#):
>
> What I would like to do is press the same button to now turn off the light and I just can't see it

You will need to store the previous state so that you can send the opposite state.

Use context for this.

e.g.

![chrome_dR7B1kkz48](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/a/1a36901bbc0605b19ea8cc81c5e337f7cc6e6a97.gif)

```auto
[{"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","func":"var state = context.get(\"state\") || false;\nstate = !state;\ncontext.set(\"state\", state);\nif (state) {\n msg.payload = {payload: \"ON\"}\n} else {\n msg.payload = { payload: \"OFF\" }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":990,"y":432,"wires":[["52182669c4a415b7"]]},{"id":"52182669c4a415b7","type":"debug","z":"7eecf5f1d763605a","name":"payload","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1180,"y":432,"wires":[]}]

```

---

_[View the full topic](https://discourse.nodered.org/t/one-button-toggle-to-turn-light-on-and-off/62050)._
