How do you make a toggle

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!!

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.

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

Ah! Wow! I expected to be simpler.

Thank you so much!!

Have a look at the toggle node.

Here is same thing in a low code format.

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

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

Thank you!

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

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.

2 Likes

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