How to convert to a function

This looks interesting I am trying to do it and also keep the people in

Will see if poss

not complete but death star so far:-s20181230_120614

1 Like

Can anyone tell why this clears both timers thought the functions were independent

[{"id":"22af34a8.2c6844","type":"rpi-gpio out","z":"fb0d00b6.ddad3","name":"","pin":"7","set":true,"level":"0","freq":"","out":"out","x":1020,"y":380,"wires":[]},{"id":"c6db990c.1dfe9","type":"ui_text","z":"fb0d00b6.ddad3","group":"2f45f695.6435ba","order":2,"width":"1","height":"1","name":"D 1st sw","label":"{{msg.payload}}","format":"","layout":"row-left","x":640,"y":380,"wires":[]},{"id":"1da4ec7e.2caebc","type":"change","z":"fb0d00b6.ddad3","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"true","fromt":"bool","to":"on","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"false","fromt":"bool","to":"off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":380,"wires":[["c6db990c.1dfe9"]]},{"id":"fa086dfd.c74ca","type":"inject","z":"fb0d00b6.ddad3","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":true,"onceDelay":"0.0","x":130,"y":280,"wires":[["c7631f18.3486e"]]},{"id":"696f35da.80e84c","type":"comment","z":"fb0d00b6.ddad3","name":"Room Lights  {row1}","info":"","x":110,"y":200,"wires":[]},{"id":"7c769adb.955ccc","type":"change","z":"fb0d00b6.ddad3","name":"set timings","rules":[{"t":"set","p":"lighton","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"lightoff","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"numberofblinks","pt":"msg","to":"1","tot":"num"},{"t":"set","p":"delaystart","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":830,"y":300,"wires":[["65ba55c3.5546c4","fd2c8c22.e2cb18"]]},{"id":"65ba55c3.5546c4","type":"function","z":"fb0d00b6.ddad3","name":"The Light Show","func":"//get the values of msg.* to var's\nvar numberofblinks = msg.numberofblinks;\nvar light_on_for_ms = msg.lighton;\nvar light_off_for_ms = msg.lightoff;\n\n//how long to wait before starting the light show\nvar time_in_ms_to_wait_before_first_execution = msg.delaystart || 0;\n\n//total run time of light on and light off\nvar time_in_ms_to_wait_between_recursive_setTimeout = (light_on_for_ms + light_off_for_ms);\n\n//set the count to 1\nvar count = 1;\n\nvar tostopit;\n\n//function to count the number of blinks and stop timmer\nfunction blinks() {\n    if (numberofblinks <= count) {\n        //stop the recursive setTimeout\n        clearTimeout(tostopit);\n    } else {\n        count++;\n    }\n}\n\n//function to set payload to true\nfunction payload_true() {\n    //set payload to true\n    msg.payload = true;\n    node.send(msg);\n\n    //display green dot when light is on\n    node.status({fill: 'green', shape: 'dot', text: ' '});\n}\n\n//function to set payload to false\nfunction payload_false() {\n    //set payload to false\n    msg.payload = false;\n    node.send(msg);\n\n    //clear status when payload false\n    node.status({});\n}\n\n//run the light show!!!!\nsetTimeout(function run() {\n    //turn the light on by setting msg.payload to true\n    payload_true();\n\n    //turn the light off by waiting to set the payload to false by the ammount of time light is on\n    setTimeout(payload_false, light_on_for_ms);\n\n    //total ammount of time light can be on or off before it runs again\n    tostopit = setTimeout(run, time_in_ms_to_wait_between_recursive_setTimeout);\n\n    blinks();\n\n}, time_in_ms_to_wait_before_first_execution);\n\nreturn null;","outputs":1,"noerr":0,"x":840,"y":340,"wires":[["22af34a8.2c6844"]]},{"id":"fd2c8c22.e2cb18","type":"debug","z":"fb0d00b6.ddad3","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"lighton","x":1010,"y":300,"wires":[]},{"id":"99c4d986.26bcc","type":"change","z":"fb0d00b6.ddad3","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"(payload*60)*1000","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":840,"y":260,"wires":[["7c769adb.955ccc"]]},{"id":"800a1073.06b13","type":"ui_slider","z":"fb0d00b6.ddad3","name":"D Slider","label":"Time on","group":"2f45f695.6435ba","order":3,"width":"10","height":"1","passthru":true,"topic":"","min":"1","max":"20","step":1,"x":260,"y":320,"wires":[["8bfc1f70.42d9b8","18e4ec.21fce315"]]},{"id":"8bfc1f70.42d9b8","type":"change","z":"fb0d00b6.ddad3","name":"","rules":[{"t":"set","p":"slider","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":340,"wires":[["2bfbabcb.f61294"]]},{"id":"c7631f18.3486e","type":"ui_switch","z":"fb0d00b6.ddad3","name":"D Room Lights","label":"Rooms","group":"2f45f695.6435ba","order":1,"width":"2","height":"1","passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":280,"y":260,"wires":[["15c2736d.b9788d","1da4ec7e.2caebc"]]},{"id":"15c2736d.b9788d","type":"switch","z":"fb0d00b6.ddad3","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":450,"y":260,"wires":[["d8129041.41de68"],["22af34a8.2c6844"]]},{"id":"d8129041.41de68","type":"change","z":"fb0d00b6.ddad3","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"slider","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":260,"wires":[["bc4b71cc.22852","99c4d986.26bcc"]]},{"id":"bc4b71cc.22852","type":"debug","z":"fb0d00b6.ddad3","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":830,"y":220,"wires":[]},{"id":"2bfbabcb.f61294","type":"ui_text","z":"fb0d00b6.ddad3","group":"2f45f695.6435ba","order":4,"width":"2","height":"1","name":"D slider text","label":"slider ","format":"{{msg.payload}}","layout":"col-center","x":650,"y":340,"wires":[]},{"id":"18e4ec.21fce315","type":"change","z":"fb0d00b6.ddad3","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":300,"wires":[["1223b912.08ffdf"]]},{"id":"f84de64f.9331e8","type":"link in","z":"fb0d00b6.ddad3","name":"","links":["c2686abd.ee3e4","1223b912.08ffdf"],"x":155,"y":240,"wires":[["c7631f18.3486e"]]},{"id":"1223b912.08ffdf","type":"link out","z":"fb0d00b6.ddad3","name":"","links":["f84de64f.9331e8"],"x":595,"y":300,"wires":[]},{"id":"79392cdf.4a5714","type":"rpi-gpio out","z":"fb0d00b6.ddad3","name":"","pin":"11","set":true,"level":"0","freq":"","out":"out","x":1020,"y":640,"wires":[]},{"id":"57bf75ca.df90a4","type":"ui_text","z":"fb0d00b6.ddad3","group":"7e5c089.52701f8","order":2,"width":"1","height":"1","name":"D 2nd sw","label":"{{msg.payload}}","format":"","layout":"row-left","x":640,"y":640,"wires":[]},{"id":"c9dfa7b.215f758","type":"change","z":"fb0d00b6.ddad3","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"true","fromt":"bool","to":"on","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"false","fromt":"bool","to":"off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":640,"wires":[["57bf75ca.df90a4"]]},{"id":"eb1da669.29751","type":"inject","z":"fb0d00b6.ddad3","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":true,"onceDelay":"0.0","x":130,"y":540,"wires":[["727a863f.b773e"]]},{"id":"a15e4d73.d12848","type":"comment","z":"fb0d00b6.ddad3","name":"Lift Lights {row2}","info":"","x":100,"y":460,"wires":[]},{"id":"47e2d2.cebcd53","type":"change","z":"fb0d00b6.ddad3","name":"set timings","rules":[{"t":"set","p":"lighton","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"lightoff","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"numberofblinks","pt":"msg","to":"1","tot":"num"},{"t":"set","p":"delaystart","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":830,"y":560,"wires":[["a5c270f6.5fa7c","eeaa6956.0733c8"]]},{"id":"a5c270f6.5fa7c","type":"function","z":"fb0d00b6.ddad3","name":"The Light Show","func":"//get the values of msg.* to var's\nvar numberofblinks = msg.numberofblinks;\nvar light_on_for_ms = msg.lighton;\nvar light_off_for_ms = msg.lightoff;\n\n//how long to wait before starting the light show\nvar time_in_ms_to_wait_before_first_execution = msg.delaystart || 0;\n\n//total run time of light on and light off\nvar time_in_ms_to_wait_between_recursive_setTimeout = (light_on_for_ms + light_off_for_ms);\n\n//set the count to 1\nvar count = 1;\n\nvar tostopit;\n\n//function to count the number of blinks and stop timmer\nfunction blinks() {\n    if (numberofblinks <= count) {\n        //stop the recursive setTimeout\n        clearTimeout(tostopit);\n    } else {\n        count++;\n    }\n}\n\n//function to set payload to true\nfunction payload_true() {\n    //set payload to true\n    msg.payload = true;\n    node.send(msg);\n\n    //display green dot when light is on\n    node.status({fill: 'green', shape: 'dot', text: ' '});\n}\n\n//function to set payload to false\nfunction payload_false() {\n    //set payload to false\n    msg.payload = false;\n    node.send(msg);\n\n    //clear status when payload false\n    node.status({});\n}\n\n//run the light show!!!!\nsetTimeout(function run() {\n    //turn the light on by setting msg.payload to true\n    payload_true();\n\n    //turn the light off by waiting to set the payload to false by the ammount of time light is on\n    setTimeout(payload_false, light_on_for_ms);\n\n    //total ammount of time light can be on or off before it runs again\n    tostopit = setTimeout(run, time_in_ms_to_wait_between_recursive_setTimeout);\n\n    blinks();\n\n}, time_in_ms_to_wait_before_first_execution);\n\nreturn null;","outputs":1,"noerr":0,"x":840,"y":600,"wires":[["79392cdf.4a5714"]]},{"id":"eeaa6956.0733c8","type":"debug","z":"fb0d00b6.ddad3","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"lighton","x":1010,"y":560,"wires":[]},{"id":"cb954bfa.9e89c","type":"change","z":"fb0d00b6.ddad3","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"(payload*60)*1000","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":840,"y":520,"wires":[["47e2d2.cebcd53"]]},{"id":"15cd7967.cd3477","type":"ui_slider","z":"fb0d00b6.ddad3","name":"D Slider","label":"Time on","group":"7e5c089.52701f8","order":3,"width":"10","height":"1","passthru":true,"topic":"","min":"1","max":"20","step":1,"x":260,"y":580,"wires":[["3c4dafc6.292d18","eeb2e3b3.d4315"]]},{"id":"3c4dafc6.292d18","type":"change","z":"fb0d00b6.ddad3","name":"","rules":[{"t":"set","p":"slider","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":600,"wires":[["bf69abef.ae96f"]]},{"id":"727a863f.b773e","type":"ui_switch","z":"fb0d00b6.ddad3","name":"D Room Lights","label":"Lift","group":"7e5c089.52701f8","order":1,"width":"2","height":"1","passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":280,"y":520,"wires":[["57c85455.a5fabc","c9dfa7b.215f758"]]},{"id":"57c85455.a5fabc","type":"switch","z":"fb0d00b6.ddad3","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":450,"y":520,"wires":[["2058388.849c248"],["79392cdf.4a5714"]]},{"id":"2058388.849c248","type":"change","z":"fb0d00b6.ddad3","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"slider","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":520,"wires":[["75072ab8.2985f4","cb954bfa.9e89c"]]},{"id":"75072ab8.2985f4","type":"debug","z":"fb0d00b6.ddad3","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":830,"y":480,"wires":[]},{"id":"bf69abef.ae96f","type":"ui_text","z":"fb0d00b6.ddad3","group":"7e5c089.52701f8","order":4,"width":"2","height":"1","name":"D slider text","label":"slider ","format":"{{msg.payload}}","layout":"col-center","x":650,"y":600,"wires":[]},{"id":"eeb2e3b3.d4315","type":"change","z":"fb0d00b6.ddad3","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":560,"wires":[["a0750cd0.620608"]]},{"id":"2514ee39.35f5da","type":"link in","z":"fb0d00b6.ddad3","name":"","links":["c2686abd.ee3e4","a0750cd0.620608"],"x":155,"y":500,"wires":[["727a863f.b773e"]]},{"id":"a0750cd0.620608","type":"link out","z":"fb0d00b6.ddad3","name":"","links":["2514ee39.35f5da"],"x":595,"y":560,"wires":[]},{"id":"2f45f695.6435ba","type":"ui_group","z":"","name":"row 1","tab":"568a2c54.1e76f4","order":2,"disp":false,"width":"15","collapse":false},{"id":"7e5c089.52701f8","type":"ui_group","z":"","name":"row 2","tab":"568a2c54.1e76f4","order":3,"disp":false,"width":"15","collapse":false},{"id":"568a2c54.1e76f4","type":"ui_tab","z":"","name":"Deathstar","icon":"dashboard","order":1}]

Think this is now correct:-
(both times seem to work as expected)

Capture

[{"id":"22af34a8.2c6844","type":"rpi-gpio out","z":"fb0d00b6.ddad3","name":"","pin":"7","set":true,"level":"0","freq":"","out":"out","x":1020,"y":380,"wires":[]},{"id":"c6db990c.1dfe9","type":"ui_text","z":"fb0d00b6.ddad3","group":"2f45f695.6435ba","order":2,"width":"1","height":"1","name":"D 1st sw","label":"{{msg.payload}}","format":"","layout":"row-left","x":640,"y":380,"wires":[]},{"id":"1da4ec7e.2caebc","type":"change","z":"fb0d00b6.ddad3","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"true","fromt":"bool","to":"on","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"false","fromt":"bool","to":"off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":380,"wires":[["c6db990c.1dfe9"]]},{"id":"fa086dfd.c74ca","type":"inject","z":"fb0d00b6.ddad3","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":true,"onceDelay":"0.0","x":110,"y":280,"wires":[["c7631f18.3486e"]]},{"id":"696f35da.80e84c","type":"comment","z":"fb0d00b6.ddad3","name":"Room Lights  {row1}","info":"","x":110,"y":200,"wires":[]},{"id":"7c769adb.955ccc","type":"change","z":"fb0d00b6.ddad3","name":"set timings","rules":[{"t":"set","p":"lighton","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"lightoff","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"numberofblinks","pt":"msg","to":"1","tot":"num"},{"t":"set","p":"delaystart","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":830,"y":300,"wires":[["fd2c8c22.e2cb18","a2dad40a.d0978"]]},{"id":"fd2c8c22.e2cb18","type":"debug","z":"fb0d00b6.ddad3","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"lighton","x":1010,"y":300,"wires":[]},{"id":"99c4d986.26bcc","type":"change","z":"fb0d00b6.ddad3","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"(payload*60)*1000","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":840,"y":260,"wires":[["7c769adb.955ccc","1b8ff599.23bd1a"]]},{"id":"800a1073.06b13","type":"ui_slider","z":"fb0d00b6.ddad3","name":"D Slider","label":"Time on","group":"2f45f695.6435ba","order":3,"width":"10","height":"1","passthru":true,"topic":"","min":"1","max":"20","step":1,"x":260,"y":320,"wires":[["8bfc1f70.42d9b8","18e4ec.21fce315"]]},{"id":"8bfc1f70.42d9b8","type":"change","z":"fb0d00b6.ddad3","name":"","rules":[{"t":"set","p":"slider","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":340,"wires":[["2bfbabcb.f61294"]]},{"id":"c7631f18.3486e","type":"ui_switch","z":"fb0d00b6.ddad3","name":"D Room Lights","label":"Rooms","group":"2f45f695.6435ba","order":1,"width":"2","height":"1","passthru":false,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":280,"y":260,"wires":[["15c2736d.b9788d","1da4ec7e.2caebc"]]},{"id":"15c2736d.b9788d","type":"switch","z":"fb0d00b6.ddad3","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":450,"y":260,"wires":[["d8129041.41de68"],["22af34a8.2c6844"]]},{"id":"d8129041.41de68","type":"change","z":"fb0d00b6.ddad3","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"slider","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":260,"wires":[["bc4b71cc.22852","99c4d986.26bcc"]]},{"id":"bc4b71cc.22852","type":"debug","z":"fb0d00b6.ddad3","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":830,"y":220,"wires":[]},{"id":"2bfbabcb.f61294","type":"ui_text","z":"fb0d00b6.ddad3","group":"2f45f695.6435ba","order":4,"width":"2","height":"1","name":"D slider text","label":"slider ","format":"{{msg.payload}}","layout":"col-center","x":650,"y":340,"wires":[]},{"id":"18e4ec.21fce315","type":"change","z":"fb0d00b6.ddad3","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":300,"wires":[["1223b912.08ffdf"]]},{"id":"f84de64f.9331e8","type":"link in","z":"fb0d00b6.ddad3","name":"","links":["c2686abd.ee3e4","1223b912.08ffdf","57b6e0f4.d52de","a3f57b69.725e98","96496b44.d07d68","f71020b1.d73c3"],"x":155,"y":240,"wires":[["c7631f18.3486e"]]},{"id":"1223b912.08ffdf","type":"link out","z":"fb0d00b6.ddad3","name":"","links":["f84de64f.9331e8"],"x":595,"y":300,"wires":[]},{"id":"79392cdf.4a5714","type":"rpi-gpio out","z":"fb0d00b6.ddad3","name":"","pin":"11","set":true,"level":"0","freq":"","out":"out","x":1020,"y":640,"wires":[]},{"id":"57bf75ca.df90a4","type":"ui_text","z":"fb0d00b6.ddad3","group":"7e5c089.52701f8","order":2,"width":"1","height":"1","name":"D 2nd sw","label":"{{msg.payload}}","format":"","layout":"row-left","x":640,"y":640,"wires":[]},{"id":"c9dfa7b.215f758","type":"change","z":"fb0d00b6.ddad3","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"true","fromt":"bool","to":"on","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"false","fromt":"bool","to":"off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":640,"wires":[["57bf75ca.df90a4"]]},{"id":"eb1da669.29751","type":"inject","z":"fb0d00b6.ddad3","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":true,"onceDelay":"0.0","x":110,"y":540,"wires":[["727a863f.b773e"]]},{"id":"a15e4d73.d12848","type":"comment","z":"fb0d00b6.ddad3","name":"Lift Lights {row2}","info":"","x":100,"y":460,"wires":[]},{"id":"47e2d2.cebcd53","type":"change","z":"fb0d00b6.ddad3","name":"set timings","rules":[{"t":"set","p":"lighton","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"lightoff","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"numberofblinks","pt":"msg","to":"1","tot":"num"},{"t":"set","p":"delaystart","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":830,"y":560,"wires":[["a5c270f6.5fa7c","eeaa6956.0733c8"]]},{"id":"a5c270f6.5fa7c","type":"function","z":"fb0d00b6.ddad3","name":"The Light Show","func":"//get the values of msg.* to var's\nvar numberofblinks = msg.numberofblinks;\nvar light_on_for_ms = msg.lighton;\nvar light_off_for_ms = msg.lightoff;\n\n//how long to wait before starting the light show\nvar time_in_ms_to_wait_before_first_execution = msg.delaystart || 0;\n\n//total run time of light on and light off\nvar time_in_ms_to_wait_between_recursive_setTimeout = (light_on_for_ms + light_off_for_ms);\n\n//set the count to 1\nvar count = 1;\n\nvar tostopit;\n\n//function to count the number of blinks and stop timmer\nfunction blinks() {\n    if (numberofblinks <= count) {\n        //stop the recursive setTimeout\n        clearTimeout(tostopit);\n    } else {\n        count++;\n    }\n}\n\n//function to set payload to true\nfunction payload_true() {\n    //set payload to true\n    msg.payload = true;\n    node.send(msg);\n\n    //display green dot when light is on\n    node.status({fill: 'green', shape: 'dot', text: ' '});\n}\n\n//function to set payload to false\nfunction payload_false() {\n    //set payload to false\n    msg.payload = false;\n    node.send(msg);\n\n    //clear status when payload false\n    node.status({});\n}\n\n//run the light show!!!!\nsetTimeout(function run() {\n    //turn the light on by setting msg.payload to true\n    payload_true();\n\n    //turn the light off by waiting to set the payload to false by the ammount of time light is on\n    setTimeout(payload_false, light_on_for_ms);\n\n    //total ammount of time light can be on or off before it runs again\n    tostopit = setTimeout(run, time_in_ms_to_wait_between_recursive_setTimeout);\n\n    blinks();\n\n}, time_in_ms_to_wait_before_first_execution);\n\nreturn null;","outputs":1,"noerr":0,"x":840,"y":600,"wires":[["79392cdf.4a5714","d57c2977.6229d","d7645fcc.a0439"]]},{"id":"eeaa6956.0733c8","type":"debug","z":"fb0d00b6.ddad3","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"lighton","x":1010,"y":560,"wires":[]},{"id":"cb954bfa.9e89c","type":"change","z":"fb0d00b6.ddad3","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"(payload*60)*1000","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":840,"y":520,"wires":[["47e2d2.cebcd53","70845dec.02a6dc"]]},{"id":"15cd7967.cd3477","type":"ui_slider","z":"fb0d00b6.ddad3","name":"D Slider","label":"Time on","group":"7e5c089.52701f8","order":3,"width":"10","height":"1","passthru":true,"topic":"","min":"1","max":"20","step":1,"x":260,"y":580,"wires":[["3c4dafc6.292d18","eeb2e3b3.d4315"]]},{"id":"3c4dafc6.292d18","type":"change","z":"fb0d00b6.ddad3","name":"","rules":[{"t":"set","p":"slider1","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":600,"wires":[["bf69abef.ae96f"]]},{"id":"727a863f.b773e","type":"ui_switch","z":"fb0d00b6.ddad3","name":"D Room Lights","label":"Lift","group":"7e5c089.52701f8","order":1,"width":"2","height":"1","passthru":false,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":280,"y":520,"wires":[["57c85455.a5fabc","c9dfa7b.215f758"]]},{"id":"57c85455.a5fabc","type":"switch","z":"fb0d00b6.ddad3","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":450,"y":520,"wires":[["2058388.849c248"],["79392cdf.4a5714"]]},{"id":"2058388.849c248","type":"change","z":"fb0d00b6.ddad3","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"slider1","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":520,"wires":[["75072ab8.2985f4","cb954bfa.9e89c"]]},{"id":"75072ab8.2985f4","type":"debug","z":"fb0d00b6.ddad3","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":830,"y":480,"wires":[]},{"id":"bf69abef.ae96f","type":"ui_text","z":"fb0d00b6.ddad3","group":"7e5c089.52701f8","order":4,"width":"2","height":"1","name":"D slider text","label":"slider ","format":"{{msg.payload}}","layout":"col-center","x":650,"y":600,"wires":[]},{"id":"eeb2e3b3.d4315","type":"change","z":"fb0d00b6.ddad3","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":560,"wires":[["a0750cd0.620608"]]},{"id":"2514ee39.35f5da","type":"link in","z":"fb0d00b6.ddad3","name":"","links":["c2686abd.ee3e4","a0750cd0.620608","d57c2977.6229d"],"x":155,"y":500,"wires":[["727a863f.b773e"]]},{"id":"a0750cd0.620608","type":"link out","z":"fb0d00b6.ddad3","name":"","links":["2514ee39.35f5da"],"x":595,"y":560,"wires":[]},{"id":"1b8ff599.23bd1a","type":"ui_text","z":"fb0d00b6.ddad3","group":"6dfb2c4f.499a04","order":2,"width":"1","height":"1","name":"D 1st sw","label":"{{msg.payload}}","format":"","layout":"row-left","x":940,"y":180,"wires":[]},{"id":"70845dec.02a6dc","type":"ui_text","z":"fb0d00b6.ddad3","group":"6dfb2c4f.499a04","order":2,"width":"1","height":"1","name":"D 1st sw","label":"{{msg.payload}}","format":"","layout":"row-left","x":940,"y":440,"wires":[]},{"id":"4b3cda87.b3282c","type":"ui_text","z":"fb0d00b6.ddad3","group":"6dfb2c4f.499a04","order":2,"width":"1","height":"1","name":"D 1st sw","label":"{{msg.payload}}","format":"","layout":"row-left","x":1020,"y":340,"wires":[]},{"id":"8d5e5258.a670d8","type":"inject","z":"fb0d00b6.ddad3","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":110,"y":320,"wires":[["800a1073.06b13"]]},{"id":"a2dad40a.d0978","type":"function","z":"fb0d00b6.ddad3","name":"The Light Show","func":"//get the values of msg.* to var's\nvar numberofblinks = msg.numberofblinks;\nvar light_on_for_ms = msg.lighton;\nvar light_off_for_ms = msg.lightoff;\n\n//how long to wait before starting the light show\nvar time_in_ms_to_wait_before_first_execution = msg.delaystart || 0;\n\n//total run time of light on and light off\nvar time_in_ms_to_wait_between_recursive_setTimeout = (light_on_for_ms + light_off_for_ms);\n\n//set the count to 1\nvar count = 1;\n\nvar tostopit;\n\n//function to count the number of blinks and stop timmer\nfunction blinks() {\n    if (numberofblinks <= count) {\n        //stop the recursive setTimeout\n        clearTimeout(tostopit);\n    } else {\n        count++;\n    }\n}\n\n//function to set payload to true\nfunction payload_true() {\n    //set payload to true\n    msg.payload = true;\n    node.send(msg);\n\n    //display green dot when light is on\n    node.status({fill: 'green', shape: 'dot', text: ' '});\n}\n\n//function to set payload to false\nfunction payload_false() {\n    //set payload to false\n    msg.payload = false;\n    node.send(msg);\n\n    //clear status when payload false\n    node.status({});\n}\n\n//run the light show!!!!\nsetTimeout(function run() {\n    //turn the light on by setting msg.payload to true\n    payload_true();\n\n    //turn the light off by waiting to set the payload to false by the ammount of time light is on\n    setTimeout(payload_false, light_on_for_ms);\n\n    //total ammount of time light can be on or off before it runs again\n    tostopit = setTimeout(run, time_in_ms_to_wait_between_recursive_setTimeout);\n\n    blinks();\n\n}, time_in_ms_to_wait_before_first_execution);\n\nreturn null;","outputs":1,"noerr":0,"x":840,"y":340,"wires":[["4b3cda87.b3282c","22af34a8.2c6844","f71020b1.d73c3"]]},{"id":"f71020b1.d73c3","type":"link out","z":"fb0d00b6.ddad3","name":"","links":["f84de64f.9331e8"],"x":895,"y":380,"wires":[]},{"id":"d57c2977.6229d","type":"link out","z":"fb0d00b6.ddad3","name":"","links":["2514ee39.35f5da"],"x":895,"y":640,"wires":[]},{"id":"d7645fcc.a0439","type":"ui_text","z":"fb0d00b6.ddad3","group":"6dfb2c4f.499a04","order":2,"width":"1","height":"1","name":"D 2nd sw","label":"{{msg.payload}}","format":"","layout":"row-left","x":1020,"y":600,"wires":[]},{"id":"fd8becbe.feab08","type":"inject","z":"fb0d00b6.ddad3","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":110,"y":580,"wires":[["15cd7967.cd3477"]]},{"id":"2f45f695.6435ba","type":"ui_group","z":"","name":"row 1","tab":"568a2c54.1e76f4","order":2,"disp":false,"width":"15","collapse":false},{"id":"7e5c089.52701f8","type":"ui_group","z":"","name":"row 2","tab":"568a2c54.1e76f4","order":3,"disp":false,"width":"15","collapse":false},{"id":"6dfb2c4f.499a04","type":"ui_group","z":"","name":"info","tab":"568a2c54.1e76f4","order":6,"disp":true,"width":"6","collapse":false},{"id":"568a2c54.1e76f4","type":"ui_tab","z":"","name":"Deathstar","icon":"dashboard","order":1}]

Hi @cflurin
can you advise how I go about stopping these timers out side of the "Function node"

is there a way to do this? (like if I click a stop button)

Send the function node a specific message (possibly using a particular topic, such as "stop") which code in the function watches for and then stops the timers.

@gbond

Just now the The Light Show function terminates blinking after msg.numberofblinks.
Do you want to replace the msg.numberofblinks by a start and stop input?

Not really when you turn the switch off the timers are still runing and some times when you turn back on they dont run the full time

Well, I've only fixed the issue in the function but I wouldn't do it this way.

Basically this is an usual practise, as soon the flow works as desired you should handle the edge cases and add error handling.

Have a look here: javascript

Here is a blink flow (without function) I used in the past:

blink

Flow:

[{"id":"3f747fe7.ca2ea","type":"trigger","z":"66646ca8.e97654","op1":"on","op2":"trigger","op1type":"str","op2type":"str","duration":"1","extend":false,"units":"s","reset":"stop","bytopic":"all","name":"on ","x":270,"y":1820,"wires":[["9a40f39c.04a16"]]},{"id":"fba8416a.45b85","type":"inject","z":"66646ca8.e97654","name":"","topic":"","payload":"start","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":1820,"wires":[["3f747fe7.ca2ea"]]},{"id":"a9110164.340b","type":"debug","z":"66646ca8.e97654","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":1860,"wires":[]},{"id":"719884a5.33b28c","type":"trigger","z":"66646ca8.e97654","op1":"off","op2":"trigger","op1type":"str","op2type":"str","duration":"1","extend":false,"units":"s","reset":"stop","bytopic":"all","name":"off","x":270,"y":1900,"wires":[["8da43e31.c64e2"]]},{"id":"8da43e31.c64e2","type":"switch","z":"66646ca8.e97654","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"trigger","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":390,"y":1900,"wires":[["3f747fe7.ca2ea"],["a9110164.340b"]]},{"id":"9a40f39c.04a16","type":"switch","z":"66646ca8.e97654","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"trigger","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":390,"y":1820,"wires":[["719884a5.33b28c"],["a9110164.340b"]]},{"id":"27067596.dbb3ea","type":"inject","z":"66646ca8.e97654","name":"stop","topic":"","payload":"stop","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":1900,"wires":[["3f747fe7.ca2ea","719884a5.33b28c"]]}]

This is only one idea how it can be done.

sorry don't understand what you are trying to say

You've posted above that The Light Show function works.

You noticed however that sometimes it doesn't work as expected.

Not really when you turn the switch off the timers are still runing and some times when you turn back on they dont run the full time

This probably depends on how you use the function (edge case). So to handle these cases you should understand javascript and try to optimise your code.

Or what exactly don't you understand?

Everthing i have tried so far to clear the times in the function seems to produce a loop that tie's up the pi and i have to pull the plug on it so any help on how you cancel the timers that are start in the function "from say a stop button" would be helpfull