when the batroom light is switched on - switch it on
When the light is switched off, check for how long it was on for and if on for less than 5minutes, turn it off
If it was on for more than 5 minutes, delay the switching off by another 5minutes.
Seemingly easy but am just not getting it right.
I created a helper, set the duration to 5 minutes and when the light is turned on, start the timer.
Then I’m checking to see if it is still active (ie light was on for less than 5 minutes) or, if it is idle… the light was on more than 5 minutes.
For the life of me I can not make it work, the timer does but the light goes on and off as per the switch (using a sonoff mini2)
Hence this “sensor” will not switch on unless the switch has been on for 5 minutes and if it is switched on, it won’t switch off for 5 minutes after the primary switch goes off.
Then all you need is an automation that or’s both the switch and the delayed version together, i.e.:
mode: single
triggers:
- trigger: state
entity_id:
- switch.bath2
- binary_sensor.bath2_switch_delayed
conditions: []
actions:
- action: light.turn_{{ iif(is_state('switch.bath2', 'on') or is_state('binary_sensor.bath2_switch_delayed', 'on'), 'on', 'off') }}
target:
entity_id: light.bath2
So in the case the that primary switch is on for less than 5 minutes the delay never happens and as soon as you switch it off the light is turned off.
However if the switch is on for more than 5 minutes the delay “sensor” is triggered and won’t allow the light to turn off for an addition 5 minutes.
done, forgot to reboot, rebooted and tested…
light on works.
light off below 5 min works too
light off after 5 minutes, switches the light off and on again and repeats it in a loop.
here comes the smile… how do I stop this… heeeelp. Figured out how and now is off again.
I like the idea, I get the thought behind it but don’t quite understand how it all works yet. Will tinker a bit and see what I can come up with.
The issue here is that the switch and the light are coupled. The automation I gave you assumes that they are independent (switching the bulb doesn’t affect the switch).
If the bulb is really just a helper pointing to the same place, it will cause issues.
I only see two ways to solve this:
If the switch has a “decoupled” mode so the switch locally, does nothing without Home Assistant online (then the script above will work as is).
Otherwise we are going to need to rework it (but there will be some limitations).
mode: single
triggers:
- trigger: state
entity_id:
- binary_sensor.bath2_switch_delayed
id: Arm
from: "off"
to: "on"
- trigger: state
entity_id:
- switch.bath2
from: "on"
to: "off"
id: Light Off
conditions: []
actions:
- choose:
- conditions:
- condition: trigger
id:
- Arm
sequence:
- action: input_select.select_option
metadata: {}
data:
option: Arm
target:
entity_id: input_select.bath2_track_switch
- conditions:
- condition: trigger
id:
- Light Off
- condition: state
entity_id: input_select.bath2_track_switch
state: Arm
sequence:
- action: input_select.select_option
metadata: {}
data:
option: Hold
target:
entity_id: input_select.bath2_track_switch
- action: switch.turn_on
metadata: {}
data: {}
target:
entity_id: switch.bath2
- delay:
hours: 0
minutes: 5
seconds: 0
milliseconds: 0
- action: switch.turn_off
metadata: {}
data: {}
target:
entity_id: switch.bath2
- action: input_select.select_option
metadata: {}
data:
option: "Off"
target:
entity_id: input_select.bath2_track_switch
The first trigger condition “Arms” the hold if the binary sensor triggers (just changes the select to “Arm”).
The second condition is triggered if the light is turned Off AND the hold is Arm(ed), in which case it will run the final sequence to turn the light back on wait 5 minutes then turn it off again.
switch on, wait shorter that 2 minutes, switch off - works
switch on, wait longer that 2 minutes - as expected, it turns off for a second and right back on. BUT… it does not turn off after set 2 minutes.
You got me thinking …
seeing the switch has to turn off because it ,think you called it detached, does not have that feature, why not test for how long it was on for…
Your hold (on) time is 3 minutes 20 (200 seconds).
I think your hold time MUST always be less than your trigger time or it will cause the same issue that we started with (the hold will keep triggering a new hold each time it ends).
However with the setting you have here, I believe it should work fine.