Action if light was on for x minutes and turned off

Hi,

Can anyone point me in the right direction?

I want to take an action (e.g. open a ventilation window or start a fan) if the light in the toilet was turned off after a period of > x minutes.

So: start a timer if the light is turned on.
Switched off and timer < x minutes: do nothing, reset timer
Switched off and timer > x minutes: turn device on, reset timer

If tried to measure the elapsed time but can’t find a way to evaluate this at the moment that the light was switched off.

Thanks!

Can you not just use an “events: state” node where the state is off for ‘x’ minutes? You would also need to check all of the options under “Ignore state change event when” to ensure it doesn’t trigger if HA restarts.

Note that if the light has been off for under ‘x’ minutes and you redeploy your flows for whatever reason, it will then not trigger. This is because when the flows start it will already be off, so the state will not change. And you can’t select “Output on connect” in this case.

Hi @michaelblight I’m not sure this is the solution.
I only want to start the fan if the toilet was occupied for e.g. > 5 minutes. If it was occupied for < 5 minutes nothing should happen. I think I need to check the time after the light was switched off.
If the occupation is 7 minutes than the fan schould start at 7 min, not already at 5 min.

Yes you’re right, I misunderstood your question. When the state goes off, you can calculate how long it was on for using the “last_changed” of the “old_state” on an “events: state” node. So using a NR change node with JSONata:

$moment(data.old_state.last_changed).diff($now())

This gives you the duration that it was in the previous on state in milliseconds - so “-45000” would be 45 seconds. Therefore you could just check that it’s more negative than 1000 * 60 * x (or more positive if you adjust the sign).

Finally I figured it out.

The toilet light is triggerd by a LD2410
When it’s turned on a timer will start
After turning off the light it will check if the occupation was > 5 min.
In that case the window will be openend and closed after 30 min.

[{"id":"d1107b5fa75886af","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"b5aea008e81fba23","type":"server-state-changed","z":"d1107b5fa75886af","name":"WC occupied","server":"d04bf2be.f81e1","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.wc_sturing_presence","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"700","forType":"num","forUnits":"milliseconds","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":130,"y":140,"wires":[["bed38c70aff3e7cd","396b72639fdceedf"],["77c9074b2e83b46e","8ee031275521021b"]]},{"id":"396b72639fdceedf","type":"api-call-service","z":"d1107b5fa75886af","name":"Light on","server":"d04bf2be.f81e1","version":5,"debugenabled":false,"domain":"switch","service":"turn_on","areaId":[],"deviceId":[],"entityId":["switch.wc_fibaro_lamp"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":400,"y":100,"wires":[[]]},{"id":"8ee031275521021b","type":"api-call-service","z":"d1107b5fa75886af","name":"Light off","server":"d04bf2be.f81e1","version":5,"debugenabled":false,"domain":"switch","service":"turn_off","areaId":[],"deviceId":[],"entityId":["switch.wc_fibaro_lamp"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":400,"y":180,"wires":[[]]},{"id":"866a4b48e4811907","type":"hourglass","z":"d1107b5fa75886af","name":"","persistId":"","humanizeLocale":"nl","x":560,"y":320,"wires":[["f5438ee87d6a5499"]]},{"id":"bed38c70aff3e7cd","type":"change","z":"d1107b5fa75886af","name":"Start","rules":[{"t":"set","p":"command","pt":"msg","to":"start","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":260,"wires":[["866a4b48e4811907"]]},{"id":"77c9074b2e83b46e","type":"change","z":"d1107b5fa75886af","name":"Stop","rules":[{"t":"set","p":"command","pt":"msg","to":"stop","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":300,"wires":[["866a4b48e4811907","4f8b1dd3a19b91a9"]]},{"id":"bdd9b9ae203122b7","type":"change","z":"d1107b5fa75886af","name":"Reset","rules":[{"t":"set","p":"command","pt":"msg","to":"reset","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":380,"wires":[["866a4b48e4811907"]]},{"id":"4f8b1dd3a19b91a9","type":"change","z":"d1107b5fa75886af","name":"Status","rules":[{"t":"set","p":"command","pt":"msg","to":"status","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":340,"wires":[["866a4b48e4811907","bdd9b9ae203122b7"]]},{"id":"f5438ee87d6a5499","type":"function","z":"d1107b5fa75886af","name":"function 6","func":"const p=msg.elapsed.time;\nnode.log(typeof p);\nmsg.payload= p.minutes;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":320,"wires":[["e21e58ed413a1b83"]]},{"id":"e21e58ed413a1b83","type":"switch","z":"d1107b5fa75886af","name":"> 5 minuten?","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"5","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":890,"y":320,"wires":[["5c6c2ec8475db45d","e10774f7c9d95317"]]},{"id":"5c6c2ec8475db45d","type":"stoptimer","z":"d1107b5fa75886af","duration":"30","units":"Minute","payloadtype":"num","payloadval":"0","name":"","x":1100,"y":320,"wires":[["6b65a9a669058110"],[]]},{"id":"e10774f7c9d95317","type":"api-call-service","z":"d1107b5fa75886af","name":"Open window","server":"d04bf2be.f81e1","version":5,"debugenabled":false,"domain":"switch","service":"turn_on","areaId":[],"deviceId":[],"entityId":["switch.wc_sturing_sturing_wc_raam"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1140,"y":260,"wires":[[]]},{"id":"6b65a9a669058110","type":"api-call-service","z":"d1107b5fa75886af","name":"Close window","server":"d04bf2be.f81e1","version":5,"debugenabled":false,"domain":"switch","service":"turn_off","areaId":[],"deviceId":[],"entityId":["switch.wc_sturing_sturing_wc_raam"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1360,"y":260,"wires":[[]]},{"id":"d04bf2be.f81e1","type":"server","name":"Home Assistant","addon":true}]