How do you trigger an automation on a binary sensor’s state?

I have a binary sensor (based on a motion sensor) whose value in the developer STATES tab shows as off or on. I want it to trigger if the sensor is off for 7 minutes. The sensor could be off or on when HA starts. So using platform
state isn’t what I’m looking for because the state might not change after restarting and I want the automation to fire once in this regard. Then if the state changes to on and off again I want the “for” period to take effect.

The following doesn’t work because the state isn’t changing. What platform should I be using? I’d prefer not to use a timer.

- alias: do something after off for 7 minutes 
  trigger:
    platform: state
    entity_id: binary_sensor.my_motion_sensor
    to: 'off'
    for:
      minutes: 7
  action:
    ...

One option would be another automation triggered upon startup, that requires the motion sensor to be off, waits 7 minutes, and then checks to see if the motion sensor is still off:

automation:
  trigger:
    - platform: homeassistant
      event: start
  condition:
    - condition: state
      state: 'off'
      entity_id: binary_sensor.my_motion_sensor
  action:
    - delay: '00:07:00'
    - condition: state
      state: 'off'
      entity_id: binary_sensor.my_motion_sensor
      for: '00:07:00'
    - service: ...
1 Like

This doesn’t really tick all the boxes, because the motion sensor could have been on and off several times within that 7 minutes, but as long as it’s off at the end of the 7 minutes, the automation will proceed.

You’d have to have a condition that the state of the sensor hadn’t changed in the last 7 minutes.

Never mind…misread :slight_smile:

Aren’t all sensors ‘unknown’ until they get set again during the startup process anyway?

The motion sensor turns off in 30 secs, resetting the 7 minute countdown again. What I’m looking for is if the associated light has been on for 7 minutes or greater, regardless of the state changed or not.

Then use that entity in the automation :man_shrugging:

Using which platform? state only fires if the state changes . If the state is already on during a restart, the automation won’t fire until the state changes from on to something else then on again. Catch 22

You said:

But at the start you said:

Either way, change the use of binary_sensor.my_motion_sensor and off to your light entity and on if that’s what you want - in the automation example I posted.