How to check if doors were opened or closed last 15 minutes?

Hello! Small question here. I would like to turn all lights off if there is no motion for 15 minutes and no door was opened or closed at last 15 minutes

- id: "008_2"
  alias: All lights off on motion
  initial_state: true
  trigger:
    - entity_id: group.all_motions
      platform: state
      to: "off"
      for:
        minutes: 13
  action:
    - entity_id: group.all_real_lights
      service: switch.turn_off

I have motion part covered, but cant really find how to check that doors were not opened or closed at last 15 minutes

A group won’t work well for this because it won’t necessarily react to state changes for individual entities. If I understand correctly, you have a bunch of door sensors and you just want to determine if they’ve remained in the same state, open or closed, for at least 15 mins? I would do it with the following:

  • An input_boolean that indicates when a door has changed state
  • Automations like these:
alias: Door state changed
trigger:
  platform: state
  entity_id: binary_sensor.door1, binary_sensor.door2, etc...
action:
  - entity_id: input_boolean.door_state_changed
    service: homeassistant.toggle

alias: Door state idle
trigger:
  platform: state
  entity_id: input_boolean.door_state_changed
  for: '00:15:00'
action:
  - do stuff...
1 Like

I use groups for motion sensors, not door sensor. But I need to track doors too
Yes you got right my case with doors
And yes, your snippet with input boolean should work as I want

The following Template Trigger will evaluate to true when each group member’s last_changed is older than 13 minutes ago. In other words, each motion sensor has maintained its state (whether on or off) for at least 13 minutes.

- id: "008_2"
  alias: All lights off on motion
  initial_state: true
  trigger:
    - entity_id: group.all_motions
      platform: template
      value_template: >
        {{ expand('group.all_motions')
           | selectattr('last_changed', 'lt', now()-timedelta(minutes=13))
           | list | count == 0 }}
  action:
    - entity_id: group.all_real_lights
      service: switch.turn_off
2 Likes

I created a timer for now and did this:

- id: "008_2"
  alias: All lights off on motion
  initial_state: true
  trigger:
    - entity_id: group.all_motions
      platform: state
      to: "off"
      for:
        minutes: 13
  condition:
    condition: state
    entity_id: timer.door_state_changed
    state: "idle"
  action:
    - entity_id: group.all_real_lights
      service: switch.turn_off
- id: "038"
  alias: Door state changed
  trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_158d00036b3d9f, binary_sensor.door_window_sensor_158d000315c017, binary_sensor.0x158d00036c84c9_contact
  action:
    - entity_id: timer.door_state_changed
      service: timer.start

But I like your solution with getting last changed time

Will try out these solutions
Thanks

Any progress to report?

Yes, sorry, I finally tried your solution and it works as I needed!

1 Like

Thanks so much for this solution. I wanted to adapt this to create a sensor basically to show if anyone is home. How could I expand this to show if group.all_motions has been off for 13 minutes (I want to be able to specify the specific state ) and also check if person.randperson is away as well for 13 minutes?

I am not sure how to add additional conditions to the template.

Thanks in advance for your help.

This code didn’t work for me, it never returned true. After a lot of trial and error, the following code seems to be giving me the result I want.

{{ 
( expand('binary_sensor.outside_doors')
| sort(reverse=true, attribute='last_changed')
| map(attribute='last_changed')
| list )[0] > utcnow() - timedelta(minutes=15)
}}

It isn’t clear to me why last_changed is coming back UTC for me, but I solved it by using utcnow() instead of now().

I also noticed that for my double doors, I had to use the individual sensors front_door_left and front_door_right instead of the front_door sensor group. In other words, the logic didn’t work if I tried to place a group sensor (front door) within a group sensor (outside doors), which I had initially done to save time.

It’s always UTC. That’s the default for all of Home Assistant’s datetime values.

When performing datetime arithmetic, as long as all values have a timezone, the calculation is performed correctly even if their timezone offsets aren’t identical.

The following example demonstrates that the difference between two datetime values is the same, regardless of which one is utc or local timezone.