Variable delay time, set within function
I was looking for a way to add a delay within a "While" loop in a function node,this apparently is harder than adding the classic Thread.sleep(1000) line.
So thanks to this flow, I can now start creating a function that dims up or dims down a value.
The aim is to compare the required value against a known state. Is the value is > then dim down over a number of step
if the value is < then dim up over a number of steps
[{"id":"1d862422.f0204c","type":"function","z":"fe6c4c4c.494c7","name":"loop","func":"// Loop function\n// Top output provides triger for next actions\n// Botton output should be connected to the input through a dealy\n// The msg.payload can consis one of actions: start, stop, toggle\n// Other content is ignored\n// On the outoput the msg.payload contains current loop state\n\ncontext.loop = context.loop || \"stop\";\ncontext.loops = context.loops || 0;\n\n\n//console.log(\"topic :\" + msg.topic);\n//console.log(\"loop :\" + context.loop);\n//console.log(\"loops :\" + context.loops);\n//console.log(\"action:\" + msg.payload);\n\nswitch (msg.payload) {\n\tcase \"stop\":\n\t\tcontext.loops = context.loops + 1;\n\t\tmsg.payload = \"stopped\";\n\t\tcontext.loop = \"stop\";\n\t\treturn [msg,{payload:\"reset\",reset:\"reset\",delay:0}];\n\tcase \"toggle\":\n\t\tif (context.loop == \"start\") {\n\t\t\tmsg.payload = \"stopped\";\n\t\t\tcontext.loop = \"stop\";\n\t\t\treturn [msg,{payload:\"set delay\",delay:500}];\n\t\t} else {\n\t\t\tmsg.payload = \"started\";\n\t\t\tcontext.loop = \"loop\";\n\t\t\tcontext.loops = 1;\n\t\t\treturn [msg,{payload:\"set delay\",delay:500}];\n\t\t}\n\tcase \"start\":\n\t\tmsg.payload = \"started\";\n\t\tcontext.loop = \"loop\";\n\t\tcontext.loops = 1;\n\t\treturn [msg,{payload:\"set delay\",delay:500}];\n\tdefault:\n\t\tif (context.loop == \"loop\") {\n\t\t\tcontext.loops = context.loops + 1;\n\t\t\tvar loopNo = context.loops;\n\t\t\tmsg.payload = \"loop:\" + context.loops;\n\t\t\treturn [msg,{payload:\"set delay\",delay:loopNo * 300,loop:loopNo}];\n\t\t} else {\n\t\t\treturn [null,null]; \n\t\t}\n}","outputs":"2","noerr":0,"initialize":"","finalize":"","x":830,"y":1760,"wires":[["5b5c92ce.cd181c"],["21ce92a0.94700e","a4735c67.3d9cf"]]},{"id":"21ce92a0.94700e","type":"delay","z":"fe6c4c4c.494c7","name":"","pauseType":"delayv","timeout":"3","timeoutUnits":"seconds","rate":"1","nbRateUnits":"","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":836,"y":1886,"wires":[["1d862422.f0204c"]]},{"id":"5b5c92ce.cd181c","type":"debug","z":"fe6c4c4c.494c7","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"msg","x":1025,"y":1752,"wires":[]},{"id":"9fca89cb.119ef8","type":"inject","z":"fe6c4c4c.494c7","name":"toggle loop 1","props":[{"p":"payload","v":"toggle","vt":"str"},{"p":"topic","v":"loop1","vt":"string"}],"repeat":"","crontab":"","once":false,"topic":"loop1","payload":"toggle","payloadType":"str","x":622,"y":1759,"wires":[["1d862422.f0204c"]]},{"id":"49be7e54.d8879","type":"inject","z":"fe6c4c4c.494c7","name":"stop loop 1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"loop1","payload":"stop","payloadType":"str","x":620,"y":1840,"wires":[["1d862422.f0204c"]]},{"id":"a4735c67.3d9cf","type":"debug","z":"fe6c4c4c.494c7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"delay","statusType":"msg","x":1010,"y":1820,"wires":[]},{"id":"5d3cd480.cb1dbc","type":"inject","z":"fe6c4c4c.494c7","name":"start loop 1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"loop1","payload":"start","payloadType":"str","x":620,"y":1800,"wires":[["1d862422.f0204c"]]}]