Why not use Node-RED nodes to make things simple ?
Flow:
[{"id":"3d109f83.9e4bd","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"23fb1e3b.2faaa2","type":"inject","z":"3d109f83.9e4bd","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":300,"wires":[["b7c54366.523e8"]]},{"id":"4c0fee22.1eff1","type":"debug","z":"3d109f83.9e4bd","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":710,"y":300,"wires":[]},{"id":"368eb1aa.e7518e","type":"inject","z":"3d109f83.9e4bd","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":360,"wires":[["b7c54366.523e8"]]},{"id":"b7c54366.523e8","type":"switch","z":"3d109f83.9e4bd","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":370,"y":320,"wires":[["b37afd32.e166c"],["13be90fe.f9c38f"]]},{"id":"b37afd32.e166c","type":"delay","z":"3d109f83.9e4bd","name":"","pauseType":"delay","timeout":"3","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":520,"y":300,"wires":[["4c0fee22.1eff1"]]},{"id":"13be90fe.f9c38f","type":"debug","z":"3d109f83.9e4bd","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":570,"y":380,"wires":[]}]