Auto-off Lights based on time AND presence

Hi friends!

First, let me say that I’m loving Home Assistant. We bought a house earlier this year that was chock-a-block full of Control4 and ripping all that out and making Home Assistant run things has been a fun project and is way, way better than having to pay someone to make stupid easy changes. But I digress…

I’m trying to automate closet lights turning off after a specified amount of time IIF no one has been moving around in there. I’ve got a presence sensor and a smart switch, so it should be straightforward. I’ve come up with two possible solutions – code here.

Option 1 tracks the state of the switch for 5 minutes, then kicks off an action to wait for state of presence to be idle for 3 minutes.

Option 2 tracks the state of the switch for 5 minutes, then uses a 3 min idle on presence as a condition.

Which one is the “preferred” way to handle this situation? Option 2 seems more straightforward, but I’m not sure if the evaluation engine will constantly re-try the state eval on the light switch if the condition fails?

Any guidance or suggestions would be appreciated!

Thanks.

Hi Dizzy, I am not strictly answering your question but I would like to offer a possible suggestion based on my situation when I was looking to automate some closet lights.

Originally I was looking at presence detection and timers but it occurred to me that this was overkill for my application as the closets are quite small. I ended up using E27 LED lights with a built in PIR sensor for the silly price of about £2 ($3) each and they fitted into the existing light sockets. The ones I used were from CPC Farnell but are no longer in stock however the following is a simillar twin pack from Amazon:

Anyway I thought I would mention it in case it might help. The point is that you don’t always need a sophisicated automation solution depending on what your particular application is.

What fun is that? That’s like handing a carpenter a pipe wrench when all he needs is a hammer.

Yes, el-cheapo motion sensing lights would be the easy, and probably the cheaper way.

But, this is Home Assistant.
I have a small refrigerator in my office closet that we keep snacks and soft drinks in. I go in, grab a bottle of water then leave. Who wants to be bothered with turning a light on or off.

I modified a Sonoff Basic to add a PIR sensor, then I wrote my first ESPHome automation all by myself:

binary_sensor:
  - platform: gpio
    pin: 
      number: GPIO14    # PIR Sensor
      inverted: False
    name: ${devicename} PIR
    id: wemos_button
    filters:
      - delayed_off: 30s
    on_press:
      then:
      - switch.turn_on: relay
    on_release:
      then:
      - switch.turn_off: relay

That is the Home Assistant way of doing something simple.

Option 2 might not work the way you think it does. The State Condition doesn’t wait until the binary_sensor is off for 3 minutes. The moment the trigger occurs, the State Condition checks if the binary_sensor has been off for 3 minutes.

Option 2’s behavior is quite different from Option 1 which employs a wait_for_trigger to wait until the binary_sensor is off for 3 minutes.

Which one is preferable depends on which which behavior you prefer. Option 1 allows the light to be on for longer than Option 2.

Words to live by.

I did the same thing in a small but high-traffic area of our home for the reasons you mentioned. It was inexpensive and has worked flawlessly for over 15 years. I have no need to include that space in my home automation system so the simplest solution suits the application.