Time control for lights automation

Hi so i added a lot of sonoff mini devices running tasmota on my house to control the lights, and i wanted to use a configurable timeout for each light with a enable switch and that you are able to control everything from the dashboard i have something working but is quite a lot of nodes, my question is anyone has a simpler solution or is there a way to make this into a couple of blocks also, like make my own block that has all the nodes inside, with my testings so far its working, also i am using mosquitto to save the last time used for when my pi restarts

This is the flow code:

[{"id":"98eff4a9.2e2b08","type":"tab","label":"Timeout","disabled":false,"info":""},{"id":"4a406144.cd8a2","type":"ui_switch","z":"98eff4a9.2e2b08","name":"","label":"Sonoff MiniTest","tooltip":"","group":"cf610d02.525a3","order":1,"width":0,"height":0,"passthru":false,"decouple":"true","topic":"","style":"","onvalue":"ON","onvalueType":"str","onicon":"","oncolor":"","offvalue":"OFF","offvalueType":"str","officon":"","offcolor":"","x":320,"y":40,"wires":[["dea50c3b.5b4a5"]]},{"id":"4c58694a.f5d3c8","type":"mqtt in","z":"98eff4a9.2e2b08","name":"","topic":"stat/sonoffT/POWER","qos":"1","datatype":"auto","broker":"4d34c726.f79428","x":110,"y":40,"wires":[["4a406144.cd8a2","9b6c4bfb.4fd0b8"]]},{"id":"2bece2c2.0897ce","type":"ui_switch","z":"98eff4a9.2e2b08","name":"","label":"SonoffT","tooltip":"","group":"988a8154.4bc07","order":1,"width":"4","height":"1","passthru":true,"decouple":"false","topic":"enable","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":240,"y":100,"wires":[["9b6c4bfb.4fd0b8"]]},{"id":"dea50c3b.5b4a5","type":"mqtt out","z":"98eff4a9.2e2b08","name":"","topic":"stat/sonoffT/POWER","qos":"2","retain":"true","broker":"4d34c726.f79428","x":540,"y":40,"wires":[]},{"id":"54228a72.62c784","type":"delay","z":"98eff4a9.2e2b08","name":"","pauseType":"delayv","timeout":"3","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":580,"y":100,"wires":[["dea50c3b.5b4a5","a9314a9e.136528"]]},{"id":"9330e0d4.e7c9d","type":"ui_text_input","z":"98eff4a9.2e2b08","name":"","label":"Tiempo Min","tooltip":"0-120 Min","group":"988a8154.4bc07","order":2,"width":"2","height":"1","passthru":true,"mode":"number","delay":"1","topic":"delay","x":470,"y":160,"wires":[["9b6c4bfb.4fd0b8","9d04ad3.610a95"]]},{"id":"9b6c4bfb.4fd0b8","type":"function","z":"98eff4a9.2e2b08","name":"IF","func":"if (msg.topic == \"delay\") {\n    if (msg.payload >=1 && msg.payload<120)\n        {context.delay=msg.payload*60000; \n    }\n    else{context.delay=5*60000;\n    }\n    return null;\n}\n\nif (msg.topic == \"enable\") {\n\tcontext.state = msg.payload;\n\treturn null;\n}\n\nif (msg.payload==\"ON\"){\n    if (context.state==1){\n        msg.payload=\"OFF\";\n        msg.delay=context.delay;\n        return msg;}\n    }\nelse\n  return null;","outputs":1,"noerr":0,"x":390,"y":100,"wires":[["54228a72.62c784","a9314a9e.136528"]]},{"id":"39b76e89.21f2c2","type":"inject","z":"98eff4a9.2e2b08","name":"ONS","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":true,"onceDelay":"0.1","x":90,"y":100,"wires":[["2bece2c2.0897ce","60fbcb4e.0be464"]]},{"id":"311d78.29111288","type":"mqtt in","z":"98eff4a9.2e2b08","name":"","topic":"delay1","qos":"1","datatype":"auto","broker":"4d34c726.f79428","x":70,"y":160,"wires":[["60fbcb4e.0be464"]]},{"id":"9d04ad3.610a95","type":"mqtt out","z":"98eff4a9.2e2b08","name":"","topic":"delay1","qos":"1","retain":"true","broker":"4d34c726.f79428","x":610,"y":160,"wires":[]},{"id":"3ff3ead4.3bdbb6","type":"function","z":"98eff4a9.2e2b08","name":"","func":"msg.payload = Number(msg.payload);\nmsg.topic = \"delay\";\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":160,"wires":[["9330e0d4.e7c9d"]]},{"id":"60fbcb4e.0be464","type":"trigger","z":"98eff4a9.2e2b08","op1":"","op2":"0","op1type":"pay","op2type":"str","duration":"0","extend":false,"units":"ms","reset":"true","bytopic":"all","name":"T&B","x":210,"y":160,"wires":[["3ff3ead4.3bdbb6"]]},{"id":"a9314a9e.136528","type":"debug","z":"98eff4a9.2e2b08","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":750,"y":220,"wires":[]},{"id":"cf610d02.525a3","type":"ui_group","z":"","name":"Luces","tab":"6f8b1d8b.49e0b4","order":1,"disp":true,"width":"6","collapse":false},{"id":"4d34c726.f79428","type":"mqtt-broker","z":"","name":"Mosquitto","broker":"JuanC","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"988a8154.4bc07","type":"ui_group","z":"","name":"Temporizadores","tab":"6f8b1d8b.49e0b4","order":2,"disp":true,"width":"6","collapse":false},{"id":"6f8b1d8b.49e0b4","type":"ui_tab","z":"","name":"Control Maestro","icon":"dashboard","order":1,"disabled":false,"hidden":false}]
```[Timeout Flow.json|attachment](upload://4aVGCPmLYuad0juCJyp144IgFZB.json) (4.4 KB)

Please wrap your flow code in back-ticks so that others can try it. You may need to re-paste as Discourse tends to mangle the text.

Like that?

2 Likes

So i just tested a different code i am using the persistent context per flow to do this so this is my code now, flows.json (4.0 KB)

[{"id":"d2f114c9.a81518","type":"mqtt in","z":"f5fcdab5.2836f8","name":"","topic":"stat/sonoffT2/POWER","qos":"2","datatype":"auto","broker":"4d34c726.f79428","x":140,"y":40,"wires":[["6c7e783a.0141a8","78d7b8bd.2c5b78"]]},{"id":"6c7e783a.0141a8","type":"ui_switch","z":"f5fcdab5.2836f8","name":"","label":"Sonoff MiniTest2","tooltip":"","group":"cf610d02.525a3","order":2,"width":0,"height":0,"passthru":false,"decouple":"true","topic":"","style":"","onvalue":"ON","onvalueType":"str","onicon":"","oncolor":"","offvalue":"OFF","offvalueType":"str","officon":"","offcolor":"","x":360,"y":40,"wires":[["fd153487.36f918"]]},{"id":"fd153487.36f918","type":"mqtt out","z":"f5fcdab5.2836f8","name":"","topic":"stat/sonoffT2/POWER","qos":"2","retain":"true","broker":"4d34c726.f79428","x":580,"y":40,"wires":[]},{"id":"f3263185.9904c","type":"delay","z":"f5fcdab5.2836f8","name":"","pauseType":"delayv","timeout":"3","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":480,"y":100,"wires":[["fd153487.36f918"]]},{"id":"d5a303d5.0aa77","type":"ui_text_input","z":"f5fcdab5.2836f8","name":"","label":"Tiempo Min2","tooltip":"0-120 Min","group":"988a8154.4bc07","order":4,"width":"2","height":"1","passthru":true,"mode":"number","delay":"0","topic":"delay","x":490,"y":220,"wires":[["49cc2bcf.b08ca4"]]},{"id":"78d7b8bd.2c5b78","type":"function","z":"f5fcdab5.2836f8","name":"","func":"if (msg.payload==\"ON\"){\n    if (flow.get(\"enable\")==1){\n        msg.payload=\"OFF\";\n        msg.delay=flow.get(\"delay\")*60000;\n        delete msg.qos;\n        delete msg.retain;\n        delete msg.topic;\n        return msg;}\n    }\nelse\n  return null;","outputs":1,"noerr":0,"x":330,"y":100,"wires":[["f3263185.9904c"]]},{"id":"12ee5580.22830b","type":"inject","z":"f5fcdab5.2836f8","name":"","topic":"ONS","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":true,"onceDelay":"0.1","x":160,"y":160,"wires":[["49cc2bcf.b08ca4","a6934ea6.59a0e"]]},{"id":"bc5f05ad.115bd8","type":"ui_switch","z":"f5fcdab5.2836f8","name":"","label":"SonoffT2","tooltip":"","group":"988a8154.4bc07","order":2,"width":"4","height":"1","passthru":true,"decouple":"false","topic":"enable","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":480,"y":160,"wires":[["a6934ea6.59a0e"]]},{"id":"49cc2bcf.b08ca4","type":"function","z":"f5fcdab5.2836f8","name":"","func":"if (msg.topic == \"ONS\") {\n    msg.topic = \"delay\";\n    msg.payload = flow.get(\"delay\")||5;\n    return msg;\n}\n\nif (msg.topic == \"delay\") {\n    if (msg.payload>=1 && msg.payload<=120){\n        flow.set(\"delay\", msg.payload);\n    }\n    else{\n        flow.set(\"delay\", 5);\n        msg.payload=5;\n        return msg;\n    }\n}\n","outputs":1,"noerr":0,"x":330,"y":220,"wires":[["d5a303d5.0aa77"]]},{"id":"a6934ea6.59a0e","type":"function","z":"f5fcdab5.2836f8","name":"","func":"if (msg.topic == \"ONS\") {\n    msg.topic = \"enable\";\n    msg.payload = flow.get(\"enable\")||0;\n    return msg;\n}\n\nif (msg.topic == \"enable\") {\n    flow.set(\"enable\", msg.payload);\n}\n","outputs":1,"noerr":0,"x":330,"y":160,"wires":[["bc5f05ad.115bd8"]]},{"id":"4d34c726.f79428","type":"mqtt-broker","z":"","name":"Mosquitto","broker":"JuanC","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"cf610d02.525a3","type":"ui_group","z":"","name":"Luces","tab":"6f8b1d8b.49e0b4","order":1,"disp":true,"width":"6","collapse":false},{"id":"988a8154.4bc07","type":"ui_group","z":"","name":"Temporizadores","tab":"6f8b1d8b.49e0b4","order":2,"disp":true,"width":"6","collapse":false},{"id":"6f8b1d8b.49e0b4","type":"ui_tab","z":"","name":"Control Maestro","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

So id still want a way to make it a single block or something like that but if anyone can help me to improve it, that would be great.
So its also to note that i am not using mosquitto anymore to store the values.