One Button toggle to turn light on and off

You will need to store the previous state so that you can send the opposite state.

Use context for this.

e.g.

chrome_dR7B1kkz48

[{"id":"83746a428adcbc17","type":"inject","z":"7eecf5f1d763605a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":810,"y":432,"wires":[["056008d289d9ada1"]]},{"id":"056008d289d9ada1","type":"function","z":"7eecf5f1d763605a","name":"toggle on/off","func":"var state = context.get(\"state\") || false;\nstate = !state;\ncontext.set(\"state\", state);\nif (state) {\n    msg.payload = {payload: \"ON\"}\n} else {\n    msg.payload = { payload: \"OFF\" }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":990,"y":432,"wires":[["52182669c4a415b7"]]},{"id":"52182669c4a415b7","type":"debug","z":"7eecf5f1d763605a","name":"payload","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1180,"y":432,"wires":[]}]
1 Like