Hello,
I changed your flow a little bit and it should work like you want it now (detailed explanation below):
[{"id":"3e7c05a9.acc372","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"1fe52d55.d9dacb","type":"switch","z":"3e7c05a9.acc372","name":"pass only 1s block 0s","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":320,"y":100,"wires":[["a24d5f97.39bbd8"]]},{"id":"a24d5f97.39bbd8","type":"function","z":"3e7c05a9.acc372","name":"toggle 1/0","func":"let toggle = flow.get(\"toggle\") || 0;\nif(toggle === 0){\n msg.payload = 1;\n return [msg, null];\n} else {\n msg.payload = 0;\n msg.reset = true;\n return [null, msg];\n}","outputs":2,"noerr":0,"x":520,"y":100,"wires":[["c1f04b0f.4e215"],["7e38ae2c.2b012","c1f04b0f.4e215"]]},{"id":"c1f04b0f.4e215","type":"trigger","z":"3e7c05a9.acc372","op1":"","op2":"0","op1type":"pay","op2type":"num","duration":"5","extend":true,"units":"s","reset":"0","bytopic":"all","name":"","x":700,"y":80,"wires":[["7e38ae2c.2b012"]]},{"id":"7e38ae2c.2b012","type":"change","z":"3e7c05a9.acc372","name":"","rules":[{"t":"set","p":"toggle","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":880,"y":120,"wires":[["c855bbdd.2380c8"]]},{"id":"1e1c813e.537d5f","type":"inject","z":"3e7c05a9.acc372","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":100,"wires":[["1fe52d55.d9dacb"]]},{"id":"c855bbdd.2380c8","type":"debug","z":"3e7c05a9.acc372","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1070,"y":100,"wires":[]}]
(i exchanged the gpios for an inject and a debug, as i haven’t got anything connected to my pi but that shouldn’t matter)
What i did is i used a generic trigger node instead of the timeout and substituted your subflow with a single function node that checks a flow variable “toggle” and returns 1 for 0 and vice versa. The function node has two outputs. 1’s get returned on the first and 0’s on the second. Messages on the second output also include a msg.reset property.
Now comes the trigger node. It gets triggered by everything coming from the first output of the function node (1’s) and just outputs the msg that came in as is immediately.
after five seconds it returns a 0 (your timeout).
When somebody presses the button in the meantime before the five seconds are over it gets stopped and never sends anything as you can stop a trigger node with a msg.reset property in the msg object and this property gets attached to every 0 from the function node and the second output of the function node (0’s) is connected both to the trigger node and the change node at the end.
Right at the end we set the flow.toggle variable to whatever payload gets send to the gpio with this change node. So flow.toggle is always synchronized and it doesn’t matter if the payload came from a timeout or a button push.
I hope this is what you wanted.
best regards Johannes