# 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:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DiGRem](https://avatars.discourse-cdn.com/v4/letter/d/5f9b8f/32.png) [@DiGRem](https://discourse.nodered.org/u/DiGRem)\
**Post date:** [2 May 2022 08:15 UTC](https://discourse.nodered.org/t/one-button-toggle-to-turn-light-on-and-off/62050/1 "2022-05-02T08:15:34Z")

</div>

Home Assistant and Node Red novice

I have a Ikea Styrbar controller and Ikea light bulb that I have configured in mqtt and this is working great.

Using Events:Status and Service Node (Toggle) I can toggle the light on and off but I see people using the MQTT nodes and can also see a lot more control over devices. (

So - I have setup a MTQQ in Node Red -

MTQQ IN - Message - Function (msg.payload = {"state": "ON"}) - MTQQ OUT (using SET after the topic name) and this is working great.

What I would like to do is press the same button to now turn off the light and I just can't see it

Searching for this seams to bring up all manor of stuff that I can't quite work out yet.

Can anyone point me in a general direction please.

(On a different matter, coming from Smartthings, I can't get over how quick this is)

Thanks

---

<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":[]}]

```

---

<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:** [2 May 2022 08:32 UTC](https://discourse.nodered.org/t/one-button-toggle-to-turn-light-on-and-off/62050/3 "2022-05-02T08:32:02Z")

</div>

Hello and welcome to the forum.

Here's a [link](http://www.resources-area.co.uk/node-red-flows/having_fun_with_buttons.pdf) to a worksheet I created for my IoT students. It shows how to create various buttons.

[Having fun with buttons](http://www.resources-area.co.uk/node-red-flows/having_fun_with_buttons.pdf)

---

<div class="post-metadata">

**Author:** ![DiGRem](https://avatars.discourse-cdn.com/v4/letter/d/5f9b8f/32.png) [@DiGRem](https://discourse.nodered.org/u/DiGRem)\
**Post date:** [2 May 2022 09:50 UTC](https://discourse.nodered.org/t/one-button-toggle-to-turn-light-on-and-off/62050/4 "2022-05-02T09:50:40Z")

</div>

Many thanks to both of you. I managed to work it out from the worksheet and its working.

I had to change the payload to payload.action and update the code as that what the Ikea button sends over.

---

<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:** [2 May 2022 10:22 UTC](https://discourse.nodered.org/t/one-button-toggle-to-turn-light-on-and-off/62050/5 "2022-05-02T10:22:18Z")

</div>

Glad to hear you got it working so quickly.

---

<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:** [16 May 2022 10:22 UTC](https://discourse.nodered.org/t/one-button-toggle-to-turn-light-on-and-off/62050/6 "2022-05-16T10:22:24Z")

</div>

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