Illuminated Toggle Button

Sometimes you just need a toggle button that lights to show status. Simple problem and this flow just works.

Uses a flow variable, so change to be unique for each toggle. (Also change to a Global variable if you need to access it from other flow pages)

Change the 1st variable in each function and obviously the mqtt and dashboard locations to suit your needs...

[{"id":"47406a1f9e128099","type":"ui_button","z":"a5b7afd8.59ad6","name":"","group":"a10fb9263d51981e","order":2,"width":"3","height":"2","passthru":false,"label":"YourLabel","tooltip":"","color":"{{msg.colour}}","bgcolor":"{{msg.backcolour}}","className":"","icon":"","payload":"toggle","payloadType":"str","topic":"","topicType":"str","x":1560,"y":160,"wires":[["a0da39f719c68c17"]]},{"id":"a0da39f719c68c17","type":"function","z":"a5b7afd8.59ad6","name":"Toggle","func":"var toggleswitch = \"your-toggle-name\"\nvar master = flow.get(toggleswitch)|| \"OFF\";\n\nswitch(master) {\n    case \"ON\": \n        master = \"OFF\";\n        node.status({ fill: \"red\", shape: \"dot\", text: toggleswitch + \": Off\"});\n        msg.colour = \"#c0c0c0\";\n        msg.backcolour = \"#404040\";\n        break;\n    case \"OFF\":\n        master = \"ON\";\n        node.status({ fill: \"green\", shape: \"dot\", text: toggleswitch + \": On\" });\n        msg.colour = \"#000000\";\n        msg.backcolour = \"#c0c020\";\n        break;\n    }\n\nmsg.payload = master;\nflow.set(toggleswitch, master);\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1750,"y":160,"wires":[["47406a1f9e128099","21845217ef943997"]]},{"id":"21845217ef943997","type":"mqtt out","z":"a5b7afd8.59ad6","name":"Your MQTT CMND Topic","topic":"yourMQTTcmndtopic","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"e4e72b98.749778","x":1970,"y":160,"wires":[]},{"id":"5bdd73094d1355c6","type":"mqtt in","z":"a5b7afd8.59ad6","name":"Your MQTT STAT Topic","topic":"YourMQTTstattopic","qos":"2","datatype":"auto-detect","broker":"e4e72b98.749778","nl":false,"rap":false,"inputs":0,"x":1120,"y":160,"wires":[["d228e5259b4d2383"]]},{"id":"d228e5259b4d2383","type":"function","z":"a5b7afd8.59ad6","name":"Set flow status","func":"var flowstatus = \"your-toggle-name\";\n\nflow.set(flowstatus,msg.payload);\n\nswitch(msg.payload) {\n    case \"ON\": \n        node.status({ fill: \"green\", shape: \"dot\", text: flowstatus + \": \" + msg.payload });\n        msg.colour = \"#000000\";\n        msg.backcolour = \"#c0c020\";\n        break;\n    case \"OFF\":\n        node.status({ fill: \"red\", shape: \"dot\", text: flowstatus + \": \" + msg.payload });\n        msg.colour = \"#c0c0c0\";\n        msg.backcolour = \"#404040\";\n        break;\n    default:\n        break;\n        \n    }\n    \nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1360,"y":160,"wires":[["47406a1f9e128099"]]},{"id":"a10fb9263d51981e","type":"ui_group","name":"Conservatory & Garden","tab":"e5bc8a1a.f5e098","order":3,"disp":false,"width":"6","collapse":false,"className":""},{"id":"e4e72b98.749778","type":"mqtt-broker","name":"Local","broker":"127.0.0.1","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""},{"id":"e5bc8a1a.f5e098","type":"ui_tab","name":"Home","icon":"dashboard","order":1}]

Flow Info

Created 1 year, 5 months ago
Rating: 5 1

Owner

Actions

Rate:

Node Types

Core
  • function (x2)
  • mqtt in (x1)
  • mqtt out (x1)
  • mqtt-broker (x1)
Other
  • ui_button (x1)
  • ui_group (x1)
  • ui_tab (x1)

Tags

  • buttons
  • illuminated
  • toggle
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option