That is already in the system.
But the problem was because of multiple devices computers being able to control the one external device and all be in sync.
As said, the code sort of worked but then if another computer's button was pressed, it wasn't in sync.
This is an example:
Incoming messages of "ON" and "OFF" set the correct state of the switch.
Otherwise pressing the button (switch) an X
message is local and toggles the button's condition.
[{"id":"9fb7e7ec.4e6b78","type":"switch","z":"26262ba1.62dcbc","name":"TOPICS *","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"BULB-1/cmnd/power1","vt":"str"},{"t":"eq","v":"BULB-1/cmnd/dimmer","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":2660,"y":300,"wires":[["c0671188.49e67","b31baea6.87a2f"],["2eac36ef.582eca"],[]],"outputLabels":["State","","LWT"],"info":"This needs editing for different BULBS"},{"id":"c0671188.49e67","type":"function","z":"26262ba1.62dcbc","name":"Set state","func":"if (msg.payload == \"RESET\")\n{\n let state = context.get(\"STATE\");\n if (state === 0)\n {\n //\n msg= {icon: '<font color = \"red\"><i class=\"fa fa-lightbulb-o fa-3x\"></i></font>'};\n msg.background = \"black\";\n return msg;\n } else\n {\n //\n msg= {icon: '<font color = \"lime\"><i class=\"fa fa-lightbulb-o fa-3x\"></i></font>'};\n msg.background = \"black\";\n return msg;\n }\n}\nif (msg.payload == \"ON\")\n{\n msg= {icon: '<font color = \"lime\"><i class=\"fa fa-lightbulb-o fa-3x\"></i></font>'};\n msg.background = \"black\";\n context.set(\"STATE\",1);\n} else\n{\n msg= {icon: '<font color = \"red\"><i class=\"fa fa-lightbulb-o fa-3x\"></i></font>'};\n msg.background = \"black\";\n context.set(\"STATE\",0);\n}\nreturn msg;\n","outputs":1,"noerr":0,"x":2860,"y":340,"wires":[["22cee9b7.50c1ae","c8ea1741.fbff8"]]},{"id":"c8ea1741.fbff8","type":"ui_button","z":"26262ba1.62dcbc","name":"BULB#1","group":"7893c6c4.30ede","order":2,"width":"1","height":"3","passthru":false,"label":"{{msg.icon}}","tooltip":"","color":"","bgcolor":"{{msg.background}}","icon":"","payload":"X","payloadType":"str","topic":"","x":3070,"y":380,"wires":[["b31baea6.87a2f"]]},{"id":"b31baea6.87a2f","type":"function","z":"26262ba1.62dcbc","name":"Button control BULB1","func":"var msg1 = {};\nvar counter = context.get(\"counter\") || 0;\ncounter = (counter + 1) % 2;\ncontext.set(\"counter\",counter);\n\nvar x = msg.payload;\n\nif (x == \"ON\")\n{\n counter = 1;\n context.set(\"counter\",1);\n return;\n} else\nif (x == \"OFF\")\n{\n counter = 0;\n context.set(\"counter\",0);\n return;\n}\n\nif (x == \"X\")\n{\n //\n if (counter == 0)\n {\n msg = {\n payload: \"OFF\",\n };\n } else\n {\n msg = {\n payload: \"ON\",\n };\n }\n msg.icon = '<font color = \"orangered\"><i class=\"fa fa-lightbulb-o fa-3x\"></i></font>';\n msg.background = \"black\";\n}\nreturn msg;\n","outputs":1,"noerr":0,"x":3110,"y":420,"wires":[["d71b2e38.587d3","43c6ebaa.d58c44","c8ea1741.fbff8"]]},{"id":"7893c6c4.30ede","type":"ui_group","z":"","name":"BULB-1","tab":"aa487daa.33c1c","order":4,"disp":true,"width":"4","collapse":false},{"id":"aa487daa.33c1c","type":"ui_tab","z":"","name":"Real_World_Control","icon":"dashboard","order":3,"disabled":false,"hidden":false}]
If you want I could include more of the flow, but basically it is looking at a MQTT
in node and it transmits to a MQTT OUT
node.