# Toggle switch with MQTT

**URL:** https://discourse.nodered.org/t/toggle-switch-with-mqtt/24330
**Category:** General
**Created:** [6 April 2020 15:49 UTC](https://discourse.nodered.org/t/toggle-switch-with-mqtt/24330 "2020-04-06T15:49:22Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![billard\_fm](https://avatars.discourse-cdn.com/v4/letter/b/e79b87/32.png) [@billard\_fm](https://discourse.nodered.org/u/billard_fm)
#### Post date: [6 April 2020 15:49 UTC](https://discourse.nodered.org/t/toggle-switch-with-mqtt/24330/1 "2020-04-06T15:49:23Z")

</div>

hello,  
i am new with node-red. I use it with a MQTT bidge to a Mesh Network.  
I would have a switch that make this sequence:

At the beginning i have a button with RON name  
First click on the button it send the payload msg RON.  
the name button and the value send must change to ROFF  
a the second click ROFF is send.

I have search on the forum, but for example i don"t understand this one:

> [@Create toggle switch](https://discourse.nodered.org/t/create-toggle-switch/9000/7):
>
> @aufruf Here is my solution. Instead of the butten I am using a 433 mhz remote control (Sonoff RF Bridge) to turn on/off a mixture of Sonoff S26, Sonoff Basic and KNX devices. [{"id":"29e7d45f.dea00c","type":"change","z":"adb29586.1f2388","name":"","rules":[{"t":"set","p":"toggle-me","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":880,"y":280,"wires":[["38a5d91c.0be77e"]]},{"id":"1b6fe3d1.5580a4","type":"switch","z":"adb29586.1f2388","name":"…

because the button don't change.

Tank's for your help.

---

<div class="post-metadata">

### Author: ![egeu5](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/egeu5/32/16876_2.png) [@egeu5](https://discourse.nodered.org/u/egeu5)
#### Post date: [20 April 2020 20:15 UTC](https://discourse.nodered.org/t/toggle-switch-with-mqtt/24330/3 "2020-04-20T20:15:54Z")

</div>

Everything alright now? The original code posted by you was my one and it is working. 😉

---

<div class="post-metadata">

### Author: ![billard\_fm](https://avatars.discourse-cdn.com/v4/letter/b/e79b87/32.png) [@billard\_fm](https://discourse.nodered.org/u/billard_fm)
#### Post date: [21 April 2020 07:06 UTC](https://discourse.nodered.org/t/toggle-switch-with-mqtt/24330/4 "2020-04-21T07:06:02Z")

</div>

Yes, your code work fine, but my problem was a toggle button that changed after a click. I have tried some par of code. Now i have this one (in the last message i have posted only the reset part) :

But i think today, i would e better having two outputs at the end of the function. The fisrt for the msg.payload message using by another node in the flow, and the second for the button change.

Thanks a lot for your help.

```auto
[{"id":"9f4223e3.4a1cd8","type":"ui_button","z":"ff884fe7.9427b8","name":"Toggle Button","group":"eb6a632f.e9248","order":0,"width":0,"height":0,"passthru":false,"label":"{{msg.state}}","tooltip":"","color":"","bgcolor":"{{msg.background}}","icon":"","payload":"true","payloadType":"bool","topic":"","x":680,"y":440,"wires":[["27e101fd.2d8fae"]]},{"id":"d82852bc.c0dd3","type":"inject","z":"ff884fe7.9427b8","name":"","topic":"","payload":"reset","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":640,"y":380,"wires":[["27e101fd.2d8fae"]]},{"id":"27e101fd.2d8fae","type":"function","z":"ff884fe7.9427b8","name":"Function","func":"\nvar state=context.get(\"state\") || \"OFF\";\n\nvar color = '';\nif (msg.payload===\"reset\") {\n state=\"OFF\"\n color = 'red';\n txt = \"OFF\"\n msg.state=\"ON\";\n}\nelse{\n\nif ((msg.payload===true) && (state==\"OFF\")) {\n state=!state;\n context.set(\"state\",\"ON\") ;\n msg.payload=\"MSG_ON\";\n color = \"green\";\n txt = \"ON\"\n msg.state=\"OFF\";\n\n} \nelse {\n msg.payload=\"MSG_OFF\";\n context.set(\"state\",\"OFF\") ;\n color = 'red';\n txt = \"OFF\"\n msg.state=\"ON\";\n}\n}\n\n node.status({\n \tfill : color,\n \tshape : 'dot',\n \ttext : txt\n });\nnode.send(msg);\nmsg.background=color;\n\n\n\nreturn msg;\n","outputs":1,"noerr":0,"x":920,"y":440,"wires":[["9f4223e3.4a1cd8","9f28e900.e94d78"]]},{"id":"eb6a632f.e9248","type":"ui_group","z":"","name":"Carte 1 V2","tab":"62c54554.67f834","disp":true,"width":"6","collapse":true},{"id":"62c54554.67f834","type":"ui_tab","z":"","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

```

---

<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: [5 May 2020 07:06 UTC](https://discourse.nodered.org/t/toggle-switch-with-mqtt/24330/5 "2020-05-05T07:06:08Z")

</div>

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