Motion light subflow
This is a node-red subflow with four properties:
- switch_id
- sensor_id
- timeout — timeout in minutes
- condition_template — optional property. If set, will be checked before turning the switch on.
[{"id":"480915c9.c5516c","type":"subflow","name":"Motion light","info":"condition_template — дополнительное условие, по которому должен включаться свет (например, только ночью)\nшаблон должен возвращать on, если свет нужно включать и off, если не нужно","category":"","in":[{"x":60,"y":140,"wires":[{"id":"c47d304b.2a92b"}]}],"out":[],"env":[{"name":"switch_id","type":"str","value":""},{"name":"sensor_id","type":"str","value":""},{"name":"timeout","type":"num","value":"2"},{"name":"condition_template","type":"str","value":""}],"color":"#87A980","icon":"font-awesome/fa-lightbulb-o","status":{"x":540,"y":600,"wires":[{"id":"9a5eea25.ac3258","port":0}]}},{"id":"4e8aff68.b98b7","type":"server-state-changed","z":"480915c9.c5516c","name":"Occupancy","server":"2f85ac72.940ba4","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"${sensor_id}","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":110,"y":220,"wires":[["51e611f5.f0b3b","c47d304b.2a92b","59509bc2.e20e94"],["c6ae5fcb.d51e2","50d55b14.65a7e4","59509bc2.e20e94"]]},{"id":"6de95750.826028","type":"api-call-service","z":"480915c9.c5516c","name":"Turn on","server":"2f85ac72.940ba4","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"${switch_id}","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":820,"y":140,"wires":[[]]},{"id":"c6ae5fcb.d51e2","type":"trigger","z":"480915c9.c5516c","name":"Timer","op1":"","op2":"","op1type":"nul","op2type":"pay","duration":"${timeout}","extend":false,"units":"min","reset":"on","bytopic":"all","topic":"topic","outputs":1,"x":610,"y":340,"wires":[["c9fb4ebd.9c9b1"]]},{"id":"51e611f5.f0b3b","type":"change","z":"480915c9.c5516c","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":300,"wires":[["c6ae5fcb.d51e2","50d55b14.65a7e4"]]},{"id":"c9fb4ebd.9c9b1","type":"api-call-service","z":"480915c9.c5516c","name":"Turn off","server":"2f85ac72.940ba4","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_off","entityId":"${switch_id}","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":780,"y":340,"wires":[[]]},{"id":"bbba6047.932c","type":"server-state-changed","z":"480915c9.c5516c","name":"Switch","server":"2f85ac72.940ba4","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"${switch_id}","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":90,"y":320,"wires":[["59d9ef0.b37491","adbe08d6.bedad8"],["51e611f5.f0b3b","adbe08d6.bedad8"]]},{"id":"59d9ef0.b37491","type":"api-current-state","z":"480915c9.c5516c","name":"Occupancy off","server":"2f85ac72.940ba4","version":1,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"${sensor_id}","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":360,"y":380,"wires":[["c6ae5fcb.d51e2","50d55b14.65a7e4"],[]]},{"id":"c47d304b.2a92b","type":"switch","z":"480915c9.c5516c","name":"Has template","property":"condition_template","propertyType":"env","rules":[{"t":"empty"},{"t":"nempty"}],"checkall":"true","repair":false,"outputs":2,"x":310,"y":160,"wires":[["6de95750.826028"],["5029bff8.9deea"]]},{"id":"5029bff8.9deea","type":"api-render-template","z":"480915c9.c5516c","name":"Condition","server":"2f85ac72.940ba4","template":"${condition_template}","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"template","templateLocationType":"msg","x":480,"y":200,"wires":[["254dd963.0cebe6"]]},{"id":"254dd963.0cebe6","type":"switch","z":"480915c9.c5516c","name":"Condition met","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":640,"y":200,"wires":[["6de95750.826028"]]},{"id":"50d55b14.65a7e4","type":"trigger","z":"480915c9.c5516c","name":"Counter","op1":"","op2":"","op1type":"date","op2type":"pay","duration":"-1","extend":false,"units":"s","reset":"on","bytopic":"all","topic":"topic","outputs":1,"x":620,"y":420,"wires":[["1b13946a.6b674c"]]},{"id":"1b13946a.6b674c","type":"function","z":"480915c9.c5516c","name":"","func":"const occupancy = flow.get('occupancy');\nconst status = flow.get('current_status');\n\nif (status === 'off') {\n return {\n payload: `S: off; M: ${occupancy}`\n }\n}\n\nif (occupancy === 'on') {\n return {\n payload: `S: on; M: on`\n }\n}\n\nconst timerStarted = Math.floor(msg.payload / 1000);\nconst timer = env.get('timeout') * 60;\nconst now = Math.floor(+(new Date()) / 1000);\nconst timeElapsed = now - timerStarted;\nconst timeLeft = timer - timeElapsed;\n\nreturn {\n payload: `S: on; M: off; T: ${timeLeft}`\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":520,"y":500,"wires":[["9a5eea25.ac3258"]]},{"id":"adbe08d6.bedad8","type":"change","z":"480915c9.c5516c","name":"","rules":[{"t":"set","p":"current_status","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":500,"wires":[["1b13946a.6b674c"]]},{"id":"59509bc2.e20e94","type":"change","z":"480915c9.c5516c","name":"","rules":[{"t":"set","p":"occupancy","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":250,"y":460,"wires":[["1b13946a.6b674c"]]},{"id":"9a5eea25.ac3258","type":"template","z":"480915c9.c5516c","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload}}","output":"str","x":380,"y":600,"wires":[[]]},{"id":"2f85ac72.940ba4","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]