[{"id":"ce5534a3.3f8b98","type":"function","z":"be10358d.5f3058","name":"","func":"\nvar value = context.get('value') || 60000;\n\nif (msg.topic == \"value\")\n{\n value = msg.payload * 60000;\n context.set('value',value); \n}\n\nelse if (msg.topic == \"start\")\n{\n\nvar timer = setTimeout(function(){msg.payload = \"now\";node.send([msg,{ payload: \"Ende\" }]);var countdownstop = context.get('countdown');clearInterval(countdownstop)},value);\ncontext.set('timer',timer); \n\nvar count = value/60000;\n//count = count -1 ;\nmsg.payload=count;\nnode.send([null,msg]);\nvar countdown = setInterval(function() {count=count-1;msg.payload=count; node.send([null,msg]) }, 60000);\ncontext.set('countdown',countdown);\n\n}\n\nelse if (msg.topic == \"stop\")\n{\nvar timerstop = context.get('timer')\nclearTimeout(timerstop);\n\nvar countdownstop = context.get('countdown')\nclearInterval(countdownstop);\n\n}\n\n\n","outputs":"2","noerr":0,"x":410,"y":220,"wires":[["a70f86ce.c18f58"],["fa7c7eb.15f9d8"]]},{"id":"a70f86ce.c18f58","type":"debug","z":"be10358d.5f3058","name":"","active":true,"console":"false","complete":"payload","x":590,"y":180,"wires":[]},{"id":"6c4f84d8.656ebc","type":"ui_button","z":"be10358d.5f3058","name":"","group":"637d8cb3.d53d74","order":0,"width":0,"height":0,"label":"Start","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"start","x":190,"y":200,"wires":[["ce5534a3.3f8b98"]]},{"id":"1f4a748.bcd498c","type":"ui_numeric","z":"be10358d.5f3058","name":"","label":"Minuten","group":"637d8cb3.d53d74","order":0,"width":0,"height":0,"passthru":true,"topic":"value","format":"{{value}}","min":"1","max":"300","step":1,"x":180,"y":280,"wires":[["ce5534a3.3f8b98"]]},{"id":"8dba67c8.f2a418","type":"ui_button","z":"be10358d.5f3058","name":"","group":"637d8cb3.d53d74","order":0,"width":0,"height":0,"label":"Stop","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"stop","x":190,"y":240,"wires":[["ce5534a3.3f8b98"]]},{"id":"fa7c7eb.15f9d8","type":"ui_text","z":"be10358d.5f3058","group":"637d8cb3.d53d74","order":0,"width":0,"height":0,"name":"","label":"Restzeit","format":"{{msg.payload}} Minuten","layout":"row-spread","x":600,"y":240,"wires":[]},{"id":"637d8cb3.d53d74","type":"ui_group","z":"","name":"Default","tab":"7fe8c0a9.8eebd","disp":true,"width":"6"},{"id":"7fe8c0a9.8eebd","type":"ui_tab","z":"","name":"tests","icon":"dashboard"}]
Am Donnerstag, 29. Dezember 2016 18:52:48 UTC+1 schrieb boris runakov:
So far I have this :
[{"id":"8206d965.39384","type":"tab","label":"Flow 2"},{"id":"47ca922a.a7c244","type":"ui_switch","z":"8206d965.39384","name":"","label":"switch","group":"7d660aaa.41817c","order":0,"width":0,"height":0,"passthru":true,"topic":"on_off","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":218.5,"y":179,"wires":[["24024d81.92a282"]]},{"id":"827033bc.32ab1","type":"ui_numeric","z":"8206d965.39384","name":"","label":"numeric","group":"7d660aaa.41817c","order":0,"width":0,"height":0,"passthru":true,"topic":"numeric","format":"{{value}}","min":0,"max":10,"step":1,"x":207.5,"y":240,"wires":[["24024d81.92a282"]]},{"id":"24024d81.92a282","type":"function","z":"8206d965.39384","name":"","func":"var time = 0;\n\nif (msg.topic == \"numeric\"){\n var time = msg.payload;\n return null;\n}\nelse if (msg.topic == \"on_off\"){\n setTimeout({payload:\"off\"},time);\n return {payload:\"on\"};\n}","outputs":1,"noerr":0,"x":359.5,"y":207,"wires":[["ce7d307f.388958"]]},{"id":"ce7d307f.388958","type":"debug","z":"8206d965.39384","name":"","active":true,"console":"false","complete":"payload","x":580.5,"y":166,"wires":[]},{"id":"7d660aaa.41817c","type":"ui_group","z":"","name":"Default","tab":"9b0940f3.a925b8","disp":true,"width":"6"},{"id":"9b0940f3.a925b8","type":"ui_tab","z":"","name":"tests","icon":"dashboard"}]
Can you help me out with this please?
welcome to the world of functions :-)
I would use a function node to do this, fed by the two UI elements - one a button to trigger things and the numeric input to set the duration - set a topic on at least one of them... then in the function use the msg.topic to distinguish which message arrived. If the numeric one then set a context variable with the value (to save it) and return null. If the trigger message use the context variable to 1) create a setTimeout statement to node.send({payload:"off"} and 2) then return {payload:"on"}; then return will happen straight away and then the timeout will occur and fire later.