How can I perform the on-off operation with a single button?

Can I change the same boolean address to true-false with a single button?

Have you tried?

1 Like

Yes, I tried, but since I am new, I could not reach a result. Are there ready-made button patterns for this?
I'm looking for a switch that will have the same operation as the switch in the node-red dashboard plugin, but with the appearance of a button.

Search for Toggle Button in the forum.

1 Like

thank you .

Here's a simple flow with three examples of how to use a button to perform various actions.
e.g. On/Off, Toggle or Count.
Hope you find it useful.

[{"id":"60c6e074.6a27d","type":"inject","z":"c275af93.a0ced8","name":"Button_1 ON","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":150,"y":220,"wires":[["e470de66.2cd938"]]},{"id":"e470de66.2cd938","type":"function","z":"c275af93.a0ced8","name":"A","func":"if ( (msg.payload == \"off\") || (msg.payload == \"reset\") ) {\n    flow.set(\"button_1\",\"off\");\n    node.status({fill:\"red\",shape:\"dot\",text:\"Button_1 is OFF\"});\n    \n    msg.payload = 0; // Set this to what you want to pass on\n    return msg;\n}\nelse if (msg.payload == \"on\") {\n    flow.set(\"button_1\",\"on\");\n    node.status({fill:\"green\",shape:\"dot\",text:\"Button_1 is ON\"});\n    \n    msg.payload = 1; // Set this to what you want to pass on\n    return msg;\n}\n","outputs":1,"noerr":0,"x":370,"y":260,"wires":[["8eacc2e1.35574"]]},{"id":"5a1a61ad.3bf93","type":"inject","z":"c275af93.a0ced8","name":"Button_1 OFF","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":150,"y":300,"wires":[["e470de66.2cd938"]]},{"id":"6543e473.28ef04","type":"inject","z":"c275af93.a0ced8","name":"Reset at start-up","repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"reset","payloadType":"str","x":170,"y":100,"wires":[["a9b8db.58384f28"]]},{"id":"a9b8db.58384f28","type":"function","z":"c275af93.a0ced8","name":"Set all buttons to OFF state","func":"flow.set(\"button_1\",\"off\");\nflow.set(\"button_2\",\"off\");\n\nflow.set(\"counter\",0);\n\nmsg.payload = \"reset\";\nreturn msg;","outputs":1,"noerr":0,"x":440,"y":100,"wires":[["7ec3acf5.180e74"]]},{"id":"8eacc2e1.35574","type":"debug","z":"c275af93.a0ced8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":550,"y":260,"wires":[]},{"id":"de533a20.15dad8","type":"inject","z":"c275af93.a0ced8","name":"Toggle Button_2","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"change","payloadType":"str","x":160,"y":420,"wires":[["fc06e83a.20e808"]]},{"id":"fc06e83a.20e808","type":"function","z":"c275af93.a0ced8","name":"B","func":"if (msg.payload == \"change\") {\n    var state = flow.get(\"button_2\") || \"off\";\n    if (state == \"off\") {\n        state = \"on\";\n        node.status({fill:\"green\",shape:\"dot\",text:\"Button_2 is ON\"});\n        msg.payload = 1; // Set this to what you want to pass on\n    }\n    else if (state == \"on\") {\n        state = \"off\";\n        node.status({fill:\"red\",shape:\"dot\",text:\"Button_2 is OFF\"});\n        msg.payload = 0; // Set this to what you want to pass on\n    }\n    flow.set(\"button_2\",state);\n    return msg;\n}\n\nelse if (msg.payload == \"reset\") {\n    flow.set(\"button_2\",\"off\");\n    node.status({fill:\"red\",shape:\"dot\",text:\"Button_2 is OFF\"});\n    msg.payload = 0; // Set this to what you want to pass on\n    return msg;\n}\n","outputs":1,"noerr":0,"x":370,"y":420,"wires":[["a9e67527.c81b08"]]},{"id":"a9e67527.c81b08","type":"debug","z":"c275af93.a0ced8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":550,"y":420,"wires":[]},{"id":"7ec3acf5.180e74","type":"link out","z":"c275af93.a0ced8","name":"initial_reset","links":["f770bcb7.c96638","cf329dda.1ed2b8","ead38ede.857b","7464e7ef.fdb638"],"x":635,"y":100,"wires":[]},{"id":"f770bcb7.c96638","type":"link in","z":"c275af93.a0ced8","name":"","links":["7ec3acf5.180e74"],"x":195,"y":260,"wires":[["e470de66.2cd938"]]},{"id":"cf329dda.1ed2b8","type":"link in","z":"c275af93.a0ced8","name":"","links":["7ec3acf5.180e74"],"x":195,"y":460,"wires":[["fc06e83a.20e808"]]},{"id":"597b6280.3be11c","type":"inject","z":"c275af93.a0ced8","name":"Count on Button_3","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"increment","payloadType":"str","x":170,"y":560,"wires":[["374defc.d4d1f9"]]},{"id":"374defc.d4d1f9","type":"function","z":"c275af93.a0ced8","name":"C","func":"if (msg.payload == \"increment\") {\n    var counter = flow.get(\"counter\") || 0;\n    counter = counter + 1;\n    node.status({text:\"Counter = \"+counter});\n    flow.set(\"counter\",counter);\n    msg.payload = counter;\n    return msg;\n}\n\nelse if (msg.payload == \"reset\") {\n    flow.set(\"counter\",0);\n    node.status({text:\"Counter = 0\"});\n    msg.payload = 0;\n    return msg;\n}\n","outputs":1,"noerr":0,"x":370,"y":600,"wires":[["6b985784.d49828"]]},{"id":"6b985784.d49828","type":"debug","z":"c275af93.a0ced8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":550,"y":560,"wires":[]},{"id":"ead38ede.857b","type":"link in","z":"c275af93.a0ced8","name":"","links":["7ec3acf5.180e74"],"x":195,"y":600,"wires":[["374defc.d4d1f9"]]},{"id":"cbc967e4.f27be8","type":"inject","z":"c275af93.a0ced8","name":"Reset the counter","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"reset","payloadType":"str","x":170,"y":640,"wires":[["374defc.d4d1f9"]]},{"id":"acffc15e.44d5a8","type":"function","z":"c275af93.a0ced8","name":"E","func":"if ( (msg.payload == \"off\") || (msg.payload == \"reset\") ) {\n    flow.set(\"button_3\",\"off\");\n    node.status({fill:\"red\",shape:\"dot\",text:\"Button_3 is OFF\"});\n    \n    msg.payload = \"Button_3 is OFF\"; // Set this to what you want to pass on\n    return msg;\n}\nelse if (msg.payload == \"on\") {\n    flow.set(\"button_3\",\"on\");\n    node.status({fill:\"green\",shape:\"dot\",text:\"Button_3 is ON\"});\n    \n    msg.payload = \"Button_3 is ON\"; // Set this to what you want to pass on\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1050,"y":280,"wires":[["1864c779.31fa79"]]},{"id":"7464e7ef.fdb638","type":"link in","z":"c275af93.a0ced8","name":"","links":["7ec3acf5.180e74"],"x":915,"y":280,"wires":[["acffc15e.44d5a8"]]},{"id":"c0b2baa6.ba63d","type":"ui_button","z":"c275af93.a0ced8","name":"On","group":"4bfa11d8.376b98","order":1,"width":"3","height":"1","passthru":false,"label":"On","tooltip":"","color":"","bgcolor":"","icon":"","payload":"on","payloadType":"str","topic":"","x":890,"y":240,"wires":[["acffc15e.44d5a8"]]},{"id":"393ac977.aee796","type":"ui_button","z":"c275af93.a0ced8","name":"","group":"4bfa11d8.376b98","order":2,"width":"3","height":"1","passthru":false,"label":"Off","tooltip":"","color":"","bgcolor":"","icon":"","payload":"off","payloadType":"str","topic":"Off","x":890,"y":320,"wires":[["acffc15e.44d5a8"]]},{"id":"1864c779.31fa79","type":"ui_text","z":"c275af93.a0ced8","group":"4bfa11d8.376b98","order":3,"width":"6","height":"1","name":"Button status","label":"Status: ","format":"{{msg.payload}}","layout":"row-center","x":1240,"y":280,"wires":[]},{"id":"4bfa11d8.376b98","type":"ui_group","name":"on-off-demo","tab":"1f158e7d.631e42","order":1,"disp":true,"width":"6","collapse":false},{"id":"1f158e7d.631e42","type":"ui_tab","name":"Button demo","icon":"dashboard","order":4,"disabled":false,"hidden":false}]
2 Likes

I don't know how to use this code as I am new to node-red :slight_smile:

What Dave has posted is an export/demo flow that you can import and test out (CTRL+I)

Also, as a new user, I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

1 Like

Thank you


With these codes, I provide what I am looking for, but the button does not appear on the dashboard, how can I display this button on the dashboard?

There is an example of using a ui-button on the Dashboard in the flow I posted.

1 Like

In the example you gave, there is this ui example, but there is an on-off operation with two buttons.
What I want to do is the on-off operation with a single button.

Also, thank you very much for your answers.

Search the forum for Toggle Button, there are many examples of how to do that.

1 Like

Try this flow - toggle button action on the Dashboard.

[{"id":"c275af93.a0ced8","type":"tab","label":"Button examples","disabled":false,"info":""},{"id":"60c6e074.6a27d","type":"inject","z":"c275af93.a0ced8","name":"Button_1 ON","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":150,"y":220,"wires":[["e470de66.2cd938"]]},{"id":"e470de66.2cd938","type":"function","z":"c275af93.a0ced8","name":"A","func":"if ( (msg.payload == \"off\") || (msg.payload == \"reset\") ) {\n    flow.set(\"button_1\",\"off\");\n    node.status({fill:\"red\",shape:\"dot\",text:\"Button_1 is OFF\"});\n    \n    msg.payload = 0; // Set this to what you want to pass on\n    return msg;\n}\nelse if (msg.payload == \"on\") {\n    flow.set(\"button_1\",\"on\");\n    node.status({fill:\"green\",shape:\"dot\",text:\"Button_1 is ON\"});\n    \n    msg.payload = 1; // Set this to what you want to pass on\n    return msg;\n}\n","outputs":1,"noerr":0,"x":370,"y":260,"wires":[["8eacc2e1.35574"]]},{"id":"5a1a61ad.3bf93","type":"inject","z":"c275af93.a0ced8","name":"Button_1 OFF","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":150,"y":300,"wires":[["e470de66.2cd938"]]},{"id":"6543e473.28ef04","type":"inject","z":"c275af93.a0ced8","name":"Reset at start-up","repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"reset","payloadType":"str","x":170,"y":100,"wires":[["a9b8db.58384f28"]]},{"id":"a9b8db.58384f28","type":"function","z":"c275af93.a0ced8","name":"Set all buttons to OFF state","func":"flow.set(\"button_1\",\"off\");\nflow.set(\"button_2\",\"off\");\n\nflow.set(\"counter\",0);\n\nmsg.payload = \"reset\";\nreturn msg;","outputs":1,"noerr":0,"x":440,"y":100,"wires":[["7ec3acf5.180e74"]]},{"id":"8eacc2e1.35574","type":"debug","z":"c275af93.a0ced8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":550,"y":260,"wires":[]},{"id":"de533a20.15dad8","type":"inject","z":"c275af93.a0ced8","name":"Toggle Button_2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"change","payloadType":"str","x":880,"y":500,"wires":[["fc06e83a.20e808"]]},{"id":"fc06e83a.20e808","type":"function","z":"c275af93.a0ced8","name":"B","func":"if (msg.payload == \"change\") {\n    var state = flow.get(\"button_2\") || \"off\";\n    if (state == \"off\") {\n        state = \"on\";\n        node.status({fill:\"green\",shape:\"dot\",text:\"Button_2 is ON\"});\n        msg.payload = 1; // Set this to what you want to pass on\n    }\n    else if (state == \"on\") {\n        state = \"off\";\n        node.status({fill:\"red\",shape:\"dot\",text:\"Button_2 is OFF\"});\n        msg.payload = 0; // Set this to what you want to pass on\n    }\n    flow.set(\"button_2\",state);\n    return msg;\n}\n\nelse if (msg.payload == \"reset\") {\n    flow.set(\"button_2\",\"off\");\n    node.status({fill:\"red\",shape:\"dot\",text:\"Button_2 is OFF\"});\n    msg.payload = 0; // Set this to what you want to pass on\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1090,"y":500,"wires":[["a9e67527.c81b08","89939c22.bff49"]]},{"id":"a9e67527.c81b08","type":"debug","z":"c275af93.a0ced8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1270,"y":500,"wires":[]},{"id":"7ec3acf5.180e74","type":"link out","z":"c275af93.a0ced8","name":"initial_reset","links":["f770bcb7.c96638","cf329dda.1ed2b8","ead38ede.857b","7464e7ef.fdb638"],"x":635,"y":100,"wires":[]},{"id":"f770bcb7.c96638","type":"link in","z":"c275af93.a0ced8","name":"","links":["7ec3acf5.180e74"],"x":195,"y":260,"wires":[["e470de66.2cd938"]]},{"id":"cf329dda.1ed2b8","type":"link in","z":"c275af93.a0ced8","name":"","links":["7ec3acf5.180e74"],"x":915,"y":540,"wires":[["fc06e83a.20e808"]]},{"id":"597b6280.3be11c","type":"inject","z":"c275af93.a0ced8","name":"Count on Button_3","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"increment","payloadType":"str","x":170,"y":560,"wires":[["374defc.d4d1f9"]]},{"id":"374defc.d4d1f9","type":"function","z":"c275af93.a0ced8","name":"C","func":"if (msg.payload == \"increment\") {\n    var counter = flow.get(\"counter\") || 0;\n    counter = counter + 1;\n    node.status({text:\"Counter = \"+counter});\n    flow.set(\"counter\",counter);\n    msg.payload = counter;\n    return msg;\n}\n\nelse if (msg.payload == \"reset\") {\n    flow.set(\"counter\",0);\n    node.status({text:\"Counter = 0\"});\n    msg.payload = 0;\n    return msg;\n}\n","outputs":1,"noerr":0,"x":370,"y":600,"wires":[["6b985784.d49828"]]},{"id":"6b985784.d49828","type":"debug","z":"c275af93.a0ced8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":550,"y":560,"wires":[]},{"id":"ead38ede.857b","type":"link in","z":"c275af93.a0ced8","name":"","links":["7ec3acf5.180e74"],"x":195,"y":600,"wires":[["374defc.d4d1f9"]]},{"id":"cbc967e4.f27be8","type":"inject","z":"c275af93.a0ced8","name":"Reset the counter","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"reset","payloadType":"str","x":170,"y":640,"wires":[["374defc.d4d1f9"]]},{"id":"acffc15e.44d5a8","type":"function","z":"c275af93.a0ced8","name":"E","func":"if ( (msg.payload == \"off\") || (msg.payload == \"reset\") ) {\n    flow.set(\"button_3\",\"off\");\n    node.status({fill:\"red\",shape:\"dot\",text:\"Button_3 is OFF\"});\n    \n    msg.payload = \"Button_3 is OFF\"; // Set this to what you want to pass on\n    return msg;\n}\nelse if (msg.payload == \"on\") {\n    flow.set(\"button_3\",\"on\");\n    node.status({fill:\"green\",shape:\"dot\",text:\"Button_3 is ON\"});\n    \n    msg.payload = \"Button_3 is ON\"; // Set this to what you want to pass on\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1050,"y":280,"wires":[["1864c779.31fa79"]]},{"id":"7464e7ef.fdb638","type":"link in","z":"c275af93.a0ced8","name":"","links":["7ec3acf5.180e74"],"x":915,"y":280,"wires":[["acffc15e.44d5a8"]]},{"id":"c0b2baa6.ba63d","type":"ui_button","z":"c275af93.a0ced8","name":"On","group":"4bfa11d8.376b98","order":1,"width":"3","height":"1","passthru":false,"label":"On","tooltip":"","color":"","bgcolor":"","icon":"","payload":"on","payloadType":"str","topic":"","topicType":"str","x":890,"y":240,"wires":[["acffc15e.44d5a8"]]},{"id":"393ac977.aee796","type":"ui_button","z":"c275af93.a0ced8","name":"","group":"4bfa11d8.376b98","order":2,"width":"3","height":"1","passthru":false,"label":"Off","tooltip":"","color":"","bgcolor":"","icon":"","payload":"off","payloadType":"str","topic":"Off","x":890,"y":320,"wires":[["acffc15e.44d5a8"]]},{"id":"1864c779.31fa79","type":"ui_text","z":"c275af93.a0ced8","group":"4bfa11d8.376b98","order":3,"width":"6","height":"1","name":"Button status","label":"Status: ","format":"{{msg.payload}}","layout":"row-center","x":1240,"y":280,"wires":[]},{"id":"2e0818c.09bcee8","type":"ui_button","z":"c275af93.a0ced8","name":"On","group":"5f235325.3c90a4","order":1,"width":"3","height":"1","passthru":false,"label":"Toggle Me","tooltip":"","color":"","bgcolor":"","icon":"","payload":"change","payloadType":"str","topic":"","topicType":"str","x":890,"y":440,"wires":[["fc06e83a.20e808"]]},{"id":"89939c22.bff49","type":"ui_text","z":"c275af93.a0ced8","group":"5f235325.3c90a4","order":3,"width":"6","height":"1","name":"Button status","label":"Status: ","format":"{{msg.payload}}","layout":"row-center","x":1270,"y":440,"wires":[]},{"id":"d7f53c88.ef6b2","type":"comment","z":"c275af93.a0ced8","name":"Toggle button action on Dashboard","info":"","x":1080,"y":400,"wires":[]},{"id":"4bfa11d8.376b98","type":"ui_group","name":"on-off-demo","tab":"1f158e7d.631e42","order":1,"disp":true,"width":"6","collapse":false},{"id":"5f235325.3c90a4","type":"ui_group","name":"Toggle demo","tab":"1f158e7d.631e42","order":2,"disp":true,"width":"6","collapse":false},{"id":"1f158e7d.631e42","type":"ui_tab","name":"Button demo","icon":"dashboard","order":4,"disabled":false,"hidden":false}]
2 Likes

Thank you so much. This is what I was looking for. :+1: