Automation: Turn light off if sensor off and another sensor hasn't been on for the last 5 minutes

I have a door sensor which turns a light on when the door is opened to a room. There is a PIR sensor in that room.

If the door is opened but the PIR isn’t triggered shortly afterwards, I want the light to go off.

This is what I have so far. I feel like I’m close, but just not quite there.


- alias: Turn off Snug lamp when kitchen door opens and no pir
  trigger:
    platform: state
    entity_id: binary_sensor.kitchen_snug_door
    to: 'off'
    for:
      minutes: 5
  condition: 
  - condition: or  
    conditions:
      - condition: state
        entity_id: binary_sensor.snug_pir
        value_template: "((as_timestamp(now()) - as_timestamp(states.binary_sensor.snug_pir.last_changed)) / 60 )| int < 6)"
  action:
    - service: homeassistant.turn_off
      entity_id: group.snug

I’d be really thankful for any help! Cheers!

Well, first off, you can’t use value_template in a state condition. You’d have to use a template condition.

Maybe this would be easier:

  condition:
  - condition: state
    entity_id: binary_sensor.snug_pir
    state: 'off'
    for:
      minutes: 5

This would cause the action to run 5 minutes after the door closes, if at that time the PIR has been off for 5 minutes (i.e., hasn’t detected any motion since the door was closed.)

That’s good to know.

The thing about the door sensor - it doesn’t send an off command.

I’ll try your solution though - sounds logical! Thank you @pnbruckner !

Huh? We’re talking about a binary sensor (binary_sensor.kitchen_snug_door, right?), and binary sensors are either 'on' or 'off'. And if they don’t change between those states to reflect the state of something (like the door in this case), what good are they???

Just a signal for opening the door is good enough for me. They’re cheapo ones, like this.

I’m sorry, but you’ve completely lost me.

You have a physical sensor on the door, right? And you have a binary sensor in HA, specifically binary_sensor.kitchen_snug_door, whose state is either 'on' or 'off', depending on the physical state of the door sensor, right?

If the answer to both of those questions are yes, then I don’t know what you’re talking about. If either answer is no, then, well, I still don’t know what you’re talking about.

1 Like

Did you create your binary_sensor.kitchen_snug_door with an off_delay ? Something like this :

  - platform: mqtt
    name: garagedoor
    state_topic: 'sensor/garagedoor'
    off_delay: 30
    device_class: 'garage_door'   
    unique_id: garagedoor

yep, exactly that.

FWIW; this works with @pnbruckner’s suggestion. Thanks!

1 Like