I’ve successfully created an automation for switching the lights on and off based on various conditions. I decided to build an automation by myself becase the (many) available blueprints look to overkilled to me for the task (and difficult to debug).
What I’m struggling with is probably a simple action, but I cannot figure out how to do.
Basically, what I want is to avoid triggering the automation until the presence status is stable for say 5 seconds. This to avoid the lights to be powered on if I just enter and exit the bathroom quicly.
This is the code of the automation.
It triggers at any state change of the presence detector. Then, based on the new value of the state, it choose between powering on or off the lights.
In the poweroff branch, it also check the sunlights at home and, if below a given threshold, it alternatively poweron the ceiling (daily) or the diffuse lights (night) depending on the time of the day. If some lights were on already, for example triggered by a scene activation or the wall switch, it does nothing.
alias: "Automatic Lights: Bathroom"
triggers:
- trigger: state
entity_id:
- binary_sensor.sensore_di_presenza_bagno_occupazione
to: null
from: null
for:
hours: 0
minutes: 0
seconds: 5
conditions: []
actions:
- choose:
- conditions:
- condition: and
conditions:
- condition: state
entity_id: binary_sensor.sensore_di_presenza_bagno_occupazione
state: "on"
- condition: state
entity_id: light.hue_enrave_ceiling_1
state: "off"
- condition: state
entity_id: light.led_vasca_luce
state: "off"
- condition: numeric_state
entity_id: sensor.sunlight_at_home
below: 50
sequence:
- if:
- condition: time
after: "06:00:00"
before: "23:00:00"
then:
- action: light.turn_on
metadata: {}
data: {}
target:
entity_id: light.hue_enrave_ceiling_1
else:
- action: scene.turn_on
metadata: {}
data: {}
target:
entity_id: scene.bagno_rilassante
alias: Select between daily or night lights
alias: Turns on the lights
- conditions:
- condition: state
entity_id: binary_sensor.sensore_di_presenza_bagno_occupazione
state: "off"
sequence:
- action: light.turn_off
metadata: {}
data: {}
target:
entity_id:
- light.hue_enrave_ceiling_1
- light.led_vasca_luce
alias: Turn off the lights
mode: restart
I thought that the for
clause in the trigger was enought to achieve this, but I’m probably missing something: even if I stay in the bathroom for less than 5 seconds, the lights switch on.
I think it could be related to some kind of hardware limitation, for ex. a minimum latency of the sensor, but I cannot exclude an error in the script itself instead.
Nonetheless, is there a way I can overcome this in your opinion?