# How do you make a toggle

**URL:** https://discourse.nodered.org/t/how-do-you-make-a-toggle/69431
**Category:** General
**Created:** [22 October 2022 19:59 UTC](https://discourse.nodered.org/t/how-do-you-make-a-toggle/69431 "2022-10-22T19:59:29Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![ygreq](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ygreq/32/28844_2.png) [@ygreq](https://discourse.nodered.org/u/ygreq)
#### Post date: [22 October 2022 19:59 UTC](https://discourse.nodered.org/t/how-do-you-make-a-toggle/69431/1 "2022-10-22T19:59:29Z")

</div>

Hi folks!

I have a momentary button seen as a binary sensor in Home Assistant that turns ON when I press it and OFF when I don't. I want to make a toggle out of it to toggle on and off only when I press the button. And thus to turn on and off a device.

I managed to filter out the ONs using a switch node but no idea how to make the ON inputs into a toggle.

Any suggestions?  
Thank you!!

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [22 October 2022 20:15 UTC](https://discourse.nodered.org/t/how-do-you-make-a-toggle/69431/2 "2022-10-22T20:15:01Z")

</div>

You would save the toggle position in a context variable, then in a change node with JSONata or a function node and javascript, alternate the saved value and save the new value.  
eg.

```auto
[{"id":"8a9d0c48438c6c7d","type":"inject","z":"366a43adb328cf95","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":250,"y":980,"wires":[["c0a46a18812e14e5"]]},{"id":"c0a46a18812e14e5","type":"change","z":"366a43adb328cf95","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"switch_toggle","tot":"flow"},{"t":"set","p":"payload","pt":"msg","to":"$$.payload = 1 ? 0 : 1","tot":"jsonata"},{"t":"set","p":"switch_toggle","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":980,"wires":[["afed0c6bfa9fbd91"]]},{"id":"afed0c6bfa9fbd91","type":"debug","z":"366a43adb328cf95","name":"debug 93","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":600,"y":980,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![ygreq](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ygreq/32/28844_2.png) [@ygreq](https://discourse.nodered.org/u/ygreq)
#### Post date: [22 October 2022 22:21 UTC](https://discourse.nodered.org/t/how-do-you-make-a-toggle/69431/3 "2022-10-22T22:21:41Z")

</div>

Ah! Wow! I expected to be simpler.

Thank you so much!!

---

<div class="post-metadata">

### Author: ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)
#### Post date: [23 October 2022 09:49 UTC](https://discourse.nodered.org/t/how-do-you-make-a-toggle/69431/4 "2022-10-23T09:49:49Z")

</div>

Have a look at the toggle node.

> **[node-red-contrib-toggle](https://flows.nodered.org/node/node-red-contrib-toggle)**
>
> Toggle switch node for Node-red

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [23 October 2022 10:15 UTC](https://discourse.nodered.org/t/how-do-you-make-a-toggle/69431/5 "2022-10-23T10:15:22Z")

</div>

Here is same thing in a low code format.

```auto
[{"id":"8a9d0c48438c6c7d","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":150,"y":900,"wires":[["f9b8159331935ddf"]]},{"id":"f9b8159331935ddf","type":"switch","z":"452103ea51141731","name":"","property":"toggle","propertyType":"flow","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":310,"y":900,"wires":[["2bccb9df7b263c05"],["23e38ca2c86b5088"]]},{"id":"2bccb9df7b263c05","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"off","tot":"str"},{"t":"set","p":"toggle","pt":"flow","to":"off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":880,"wires":[["afed0c6bfa9fbd91"]]},{"id":"23e38ca2c86b5088","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"on","tot":"str"},{"t":"set","p":"toggle","pt":"flow","to":"on","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":920,"wires":[["afed0c6bfa9fbd91"]]},{"id":"afed0c6bfa9fbd91","type":"debug","z":"452103ea51141731","name":"debug 93","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":700,"y":900,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![ygreq](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ygreq/32/28844_2.png) [@ygreq](https://discourse.nodered.org/u/ygreq)
#### Post date: [23 October 2022 12:13 UTC](https://discourse.nodered.org/t/how-do-you-make-a-toggle/69431/6 "2022-10-23T12:13:09Z")

</div>

I did. I did not quite understand how to make it work. I will have another look then.

Thank you!

---

<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: [23 October 2022 12:53 UTC](https://discourse.nodered.org/t/how-do-you-make-a-toggle/69431/7 "2022-10-23T12:53:26Z")

</div>

I wrote this tutorial some years ago for my IoT students.  
It explains how to create 'on and off' buttons, 'toggle' buttons, 'buttons that count', etc..

[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)

There is a link on the last page of the tutorial that will take you to the Node-RED flow.  
I hope you find the tutorial useful.

---

<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 November 2022 12:54 UTC](https://discourse.nodered.org/t/how-do-you-make-a-toggle/69431/8 "2022-11-06T12:54:23Z")

</div>

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