Let the switch be the controller of the gate
[{"id":"6f6a991e.4a28d8","type":"function","z":"81c5c63b.6b8288","name":"gate","func":"// read (or initialize) gate state\nvar gateIsOpen = context.get(\"gateIsOpen\") || false;\n\n//message is from switch\nif(msg.topic === \"gate\"){\n gateIsOpen = msg.payload;\n // store the gate state \n context.set(\"gateIsOpen\",gateIsOpen);\n}\n\n// message is from other sources\nelse{\n if(gateIsOpen === true){\n node.status({fill:\"green\",shape:\"ring\",text:\"gate is open\"});\n // send through the original message\n return msg;\n }\n else{\n //just display the status of gate\n node.status({fill:\"red\",shape:\"ring\",text:\"gate is closed\"});\n }\n}\n\n","outputs":1,"noerr":0,"x":650,"y":580,"wires":[["d28937dd.8d5a98"]]},{"id":"58196f37.a5047","type":"ui_switch","z":"81c5c63b.6b8288","name":"","label":"switch","group":"df7325b1.2ce318","order":0,"width":0,"height":0,"passthru":false,"decouple":"false","topic":"gate","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":430,"y":500,"wires":[["6f6a991e.4a28d8"]]},{"id":"e67fead5.6a33c8","type":"inject","z":"81c5c63b.6b8288","name":"","topic":"init","payload":"This is data","payloadType":"str","repeat":"2","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":580,"wires":[["6f6a991e.4a28d8"]]},{"id":"d28937dd.8d5a98","type":"debug","z":"81c5c63b.6b8288","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":850,"y":580,"wires":[]},{"id":"2f34865f.5350aa","type":"comment","z":"81c5c63b.6b8288","name":"Switch controls gate","info":"Switch sends boolean value to gate.\nmsg has topic \"gate\". This way the gate can understand \nthat this message is meant for gate to change it's state ","x":390,"y":440,"wires":[]},{"id":"a1928b73.28be98","type":"comment","z":"81c5c63b.6b8288","name":"Gate ","info":"Gate can be even OPEN or CLOSED (boolean value)\nAccording to the current state the incoming data is sent through or not","x":650,"y":520,"wires":[]},{"id":"df7325b1.2ce318","type":"ui_group","z":"","name":"Default","tab":"245a6127.50b27e","disp":true,"width":"6","collapse":false},{"id":"245a6127.50b27e","type":"ui_tab","z":"","name":"Home","icon":"dashboard"}]