An example flow for control of devices via MQTT - tasmota in this case

Scenario:

You want to turn a device ON/OFF via MQTT.
In this case it is a power point.
There is a button node to indicate the status.
There is also an inject node to test it from the edit screen.

What happens:

RED is OFF and LIME/GREEN is ON.
ORANGE is I'm changing state to make it a bit more realistic in turning ON/OFF.
Most of the work is done in the subflow. Read the instructions for the subflow.
Sorry, I am not a good person with writing docs, but it is fairly simple. I hope what I have put there is good enough.

The LWT (and birth certificate) are used to control the icon/switch node status.
If it device is unplugged, the button/switch/icon is disabled.
If it is plugged in and working, the button/switch/icon is enabled and usable.

YOU MUST
set the device to be controlled as an env variable of the subflow.

And also set attributes on the button node to match the device's name also.
As is, it won't work for you. I've included the flow to give you an example of HOW to use the subflow.

I'd be interested if you like it.

[{"id":"94cf2ea396e5b6f6","type":"subflow","name":"MQTT Button Toggle","info":"# 2023 02 08\nAdded status text to icon to \nshow status of device.\n\n# 2023 01 25\nControl MQTT devices (power points)\nwith this node.\n\n`Button` node MUST have `topic` set to include\nthe device's name.  eg: `A1/button`\nand the payload MUST be `X`\n\nSet the `Device Name` to (eg) `A1` to\ncontrol `A1` device.\n","category":"","in":[{"x":70,"y":160,"wires":[{"id":"7207fbe19cc07923"}]}],"out":[{"x":990,"y":200,"wires":[{"id":"66fb54e88f4acb8d","port":0}]},{"x":1190,"y":160,"wires":[{"id":"27d2dd9a875ab1a5","port":0}]}],"env":[{"name":"DN","type":"str","value":"","ui":{"label":{"en-US":"Device Name"},"type":"input","opts":{"types":["str"]}}}],"meta":{},"color":"#DDAA99","inputLabels":["MQTT (tele and LWT) and button input"],"outputLabels":["Back to `button` input","MQTT output"],"status":{"x":1070,"y":270,"wires":[{"id":"45a85f7621f5c28f","port":0}]}},{"id":"541720ad73579447","type":"switch","z":"94cf2ea396e5b6f6","name":"what message?","property":"topic","propertyType":"msg","rules":[{"t":"cont","v":"/stat/POWER","vt":"str"},{"t":"cont","v":"/tele/LWT","vt":"str"},{"t":"cont","v":"button","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":195,"y":160,"wires":[["61843ca1684f9f9d"],["4788b5265af1d45a"],["5b8cd9fe254ea8d1"]],"l":false},{"id":"61843ca1684f9f9d","type":"delay","z":"94cf2ea396e5b6f6","name":"Delay","pauseType":"delay","timeout":"200","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":340,"y":80,"wires":[["bdb2c46daf62b36f"]]},{"id":"6d4704f6fc9b98be","type":"change","z":"94cf2ea396e5b6f6","name":"RESET","rules":[{"t":"set","p":"payload","pt":"msg","to":"RESET","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":120,"wires":[["bdb2c46daf62b36f"]]},{"id":"4788b5265af1d45a","type":"switch","z":"94cf2ea396e5b6f6","name":"LWT","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Online","vt":"str"},{"t":"eq","v":"Offline","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":340,"y":160,"wires":[["6d4704f6fc9b98be","bc2aabd04561d534"],["6a82c12953657965"]]},{"id":"6a82c12953657965","type":"function","z":"94cf2ea396e5b6f6","name":"BAN","func":"msg = {icon: '<font color = \"red\"><i class=\"fa fa-ban fa-3x\"></i></font>'};\nreturn msg;","outputs":1,"noerr":0,"x":340,"y":200,"wires":[["8dc8a0dd2b669ae3","704ae850657c9724"]]},{"id":"bc2aabd04561d534","type":"change","z":"94cf2ea396e5b6f6","name":"Enable","rules":[{"t":"set","p":"enabled","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"ARLEC-1","pt":"flow","to":"ONLINE","tot":"str"},{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":160,"wires":[["66fb54e88f4acb8d"]]},{"id":"bdb2c46daf62b36f","type":"function","z":"94cf2ea396e5b6f6","name":"Set state","func":"let device = msg.topic.split(\"/\");\n//node.warn(\"Device \" + device[0]);\n//node.warn(\"Payload \" + msg.payload);\n\nif (msg.payload == \"RESET\")\n{\n    let state = context.get(device[0]);\n    if (state === 0)\n    {\n        //\n        msg= {\"icon\": '<font color = \"red\"><i class=\"fa fa-toggle-on fa-rotate-270 fa-3x\"></i></font>',\"background\":\"black\"};\n        return msg;\n    }\n    //\n}\nif (msg.payload == \"ON\")\n{\n    msg = { \"icon\": '<font color = \"lime\"><i class=\"fa fa-toggle-on fa-rotate-90 fa-3x\"></i></font>', \"background\": \"black\" };\n    context.set(\"STATE\",1);\n    context.set(device[0],1);\n} else\n{\n    msg = { \"icon\": '<font color = \"red\"><i class=\"fa fa-toggle-on fa-rotate-270 fa-3x\"></i></font>', \"background\": \"black\" };\n    context.set(\"STATE\",0);\n    context.set(device[0], 1);\n}\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":120,"wires":[["66fb54e88f4acb8d"]]},{"id":"8dc8a0dd2b669ae3","type":"change","z":"94cf2ea396e5b6f6","name":"Disable","rules":[{"t":"set","p":"enabled","pt":"msg","to":"false","tot":"bool"},{"t":"set","p":"ARLEC-1","pt":"flow","to":"OFFLINE","tot":"str"},{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":200,"wires":[["66fb54e88f4acb8d"]]},{"id":"5b8cd9fe254ea8d1","type":"function","z":"94cf2ea396e5b6f6","name":"Button control","func":"let msg1 = {};\n\nlet x = msg.payload;\nlet counter = context.get(\"counter\") || 0;\nif (x == \"X\")\n{\n    counter = (counter + 1) % 2;\n    context.set(\"counter\",counter);\n    if (counter === 0)\n    {\n        //\n        msg.payload = \"OFF\";\n        msg1 = {\"icon\": '<font color = \"orangered\"><i class=\"fa fa-toggle-on fa-rotate-270 fa-3x\"></i></font>',\"background\":\"black\"};\n\n    } else\n    {\n        //\n        msg.payload = \"ON\";\n        msg1 = {\"icon\": '<font color = \"orangered\"><i class=\"fa fa-toggle-on fa-rotate-90 fa-3x\"></i></font>',\"backgound\":\"black\"};\n    }\n}\nif (x == \"ON\")\n{\n    //  Force power ON\n    msg.payload = \"ON\";\n    msg1 = { \"icon\": '<font color = \"orangered\"><i class=\"fa fa-toggle-on fa-rotate-90 fa-3x\"></i></font>', \"backgound\": \"black\" };\n    context.set(\"counter\",1);\n}\nif (x == \"OFF\")\n{\n    //  Force power OFF\n    msg.payload = \"OFF\";\n    msg1 = { \"icon\": '<font color = \"orangered\"><i class=\"fa fa-toggle-on fa-rotate-90 fa-3x\"></i></font>', \"backgound\": \"black\" };\n    context.set(\"counter\",0);\n}\n\nreturn [msg,msg1];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":750,"y":120,"wires":[["4f29a481b6f0d0e3","0ae8f43ccf9a1016"],["66fb54e88f4acb8d"]]},{"id":"4f29a481b6f0d0e3","type":"delay","z":"94cf2ea396e5b6f6","name":"Delay","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"outputs":1,"x":1040,"y":120,"wires":[["27d2dd9a875ab1a5"]]},{"id":"27d2dd9a875ab1a5","type":"change","z":"94cf2ea396e5b6f6","name":"#1","rules":[{"t":"set","p":"topic","pt":"msg","to":"DN","tot":"env"},{"t":"set","p":"topic","pt":"msg","to":"topic&'/cmnd/power1'","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1040,"y":160,"wires":[[]]},{"id":"7207fbe19cc07923","type":"switch","z":"94cf2ea396e5b6f6","name":"Only for this device","property":"topic","propertyType":"msg","rules":[{"t":"cont","v":"DN","vt":"env"}],"checkall":"true","repair":false,"outputs":1,"x":135,"y":160,"wires":[["541720ad73579447"]],"l":false},{"id":"0ae8f43ccf9a1016","type":"function","z":"94cf2ea396e5b6f6","name":"Identify","func":"let dev = env.get(\"DN\");\nlet status = msg.payload;\n\nnode.status(dev + \" \" + status);","outputs":1,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\nnode.status(env.get(\"DN\"));\n","finalize":"","libs":[],"x":540,"y":270,"wires":[[]]},{"id":"45a85f7621f5c28f","type":"status","z":"94cf2ea396e5b6f6","name":"","scope":["0ae8f43ccf9a1016","f9f07adecaa02241"],"x":770,"y":270,"wires":[[]]},{"id":"704ae850657c9724","type":"change","z":"94cf2ea396e5b6f6","name":"Wipe status when unplugged","rules":[{"t":"set","p":"payload","pt":"msg","to":" ","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":305,"y":270,"wires":[["0ae8f43ccf9a1016"]],"l":false},{"id":"66fb54e88f4acb8d","type":"junction","z":"94cf2ea396e5b6f6","x":680,"y":200,"wires":[[]]},{"id":"86852c080f02187b","type":"mqtt in","z":"6dd5ca4d.d1958c","g":"08979a5b752f5e66","name":"","topic":"A1/stat/POWER","qos":"2","datatype":"auto-detect","broker":"bbf26a6c.b7922","nl":false,"rap":true,"rh":0,"inputs":0,"x":3990,"y":3120,"wires":[["f29f3ae3ad90c065"]]},{"id":"1e39d8c837d7b2fb","type":"mqtt in","z":"6dd5ca4d.d1958c","g":"08979a5b752f5e66","name":"","topic":"A1/tele/LWT","qos":"2","datatype":"auto-detect","broker":"bbf26a6c.b7922","nl":false,"rap":true,"rh":0,"inputs":0,"x":4000,"y":3170,"wires":[["f29f3ae3ad90c065"]]},{"id":"61ee633777cbbe4d","type":"link in","z":"6dd5ca4d.d1958c","g":"08979a5b752f5e66","name":"Button","links":["c2f5a62f6628cf5f","9b45b54a54fd7017"],"x":4055,"y":3210,"wires":[["f29f3ae3ad90c065"]]},{"id":"f29f3ae3ad90c065","type":"subflow:94cf2ea396e5b6f6","z":"6dd5ca4d.d1958c","g":"08979a5b752f5e66","name":"","env":[{"name":"DN","value":"A1","type":"str"},{"name":"Device name","value":"A1","type":"str"}],"x":4200,"y":3170,"wires":[["9ba2764071e69ef2"],["a468b0cde187f892","4acfea6a575732bb"]]},{"id":"c42bfcfaec6f1e40","type":"inject","z":"6dd5ca4d.d1958c","g":"08979a5b752f5e66","name":"Button","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"A1/button","payload":"X","payloadType":"str","x":4410,"y":3100,"wires":[["9b45b54a54fd7017"]]},{"id":"9ba2764071e69ef2","type":"ui_button","z":"6dd5ca4d.d1958c","g":"08979a5b752f5e66","name":"A1","group":"5dde4bf66eb38aa1","order":1,"width":"1","height":"1","passthru":false,"label":"{{msg.icon}}","tooltip":"","color":"","bgcolor":"{{msg.background}}","className":"","icon":"","payload":"X","payloadType":"str","topic":"A1/button","topicType":"str","x":4410,"y":3140,"wires":[["9b45b54a54fd7017"]]},{"id":"a468b0cde187f892","type":"mqtt out","z":"6dd5ca4d.d1958c","g":"08979a5b752f5e66","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"bbf26a6c.b7922","x":4410,"y":3180,"wires":[]},{"id":"9b45b54a54fd7017","type":"link out","z":"6dd5ca4d.d1958c","g":"08979a5b752f5e66","name":"link out 3","mode":"link","links":["61ee633777cbbe4d"],"x":4505,"y":3140,"wires":[]},{"id":"bbf26a6c.b7922","type":"mqtt-broker","name":"TIMEPI MQTT","broker":"192.168.17.39","port":"1883","clientid":"","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"SOM","birthQos":"0","birthRetain":"false","birthPayload":"BedPi Comms UP","birthMsg":{},"closeTopic":"EOM","closeQos":"0","closeRetain":"false","closePayload":"BedPi Shutting DOWN","closeMsg":{},"willTopic":"EOM","willQos":"0","willRetain":"false","willPayload":"BedPi COMMS FAILURE","willMsg":{},"userProps":"","sessionExpiry":""},{"id":"5dde4bf66eb38aa1","type":"ui_group","name":"A(1-4) Sockets","tab":"e92ac76d.c24e7","order":12,"disp":true,"width":"4","collapse":false,"className":""},{"id":"e92ac76d.c24e7","type":"ui_tab","name":"Real_World_Control","icon":"dashboard","order":7,"disabled":false,"hidden":false}]
1 Like