Lights automation: switch on and off only in case of stable status from the presence detector

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?

Is this really a presence sensor (mmWave or similar) or a much simpler/cheaper motion sensor (PIR)? The latter normally react much more slowly and often have a built-in cooldown period as well before they toggle off. Before tinkering with the automation you need to actually look at the state of the sensor and figure out how it behaves, latency, cooldown etc.

It’s a real presence sensor I think (Aqara FP1E), although I don’t know its latency…I think I could make so ping-pong tests checking the state changes, indeed…

I use the very same sensor in my bathroom. While I find it to be very responsive, I think you are asking for a bit too much for the sensor to be able to toggle on/off within five seconds. If it were to respond that quickly, you’d surely risk many more false negatives.

And that is really the very best thing about mmWave sensors – nowhere near as many false negatives as a PIR sensor. With a PIR sensor I had to set my bathroom lights to turn off after 10 minutes of inactivity, and sometimes even that was not enough. With mmWave I have it set to 1 minute.

So I don’t really understand why you wouldn’t want to turn on the lights immediately when the sensor detects motion?

(The only issues I’ve had with the sensor is it reacting to the washer and/or dryer. Supposedly this can be tuned but I felt it made the sensor less responsive.)

1 Like

It often happens that I go to the bathroom for few seconds, for example just to pick up the dishwashing detergent.

It’s not that it’s so annoying, in facts. I could live with that.

But I was just wondering if it could be stressful for the devices…