WTH Automation condition: was entity in 'x' state in the last 'x' minutes even if it has changed state

Basically, I would like to be able to check if my door has been open any time in the last ‘x’ minutes/hours despite if it is now closed via the UI.

What? Do you mean “Was my door open at any time during the last hour”?
You can check that with the states.my.entity.last_changed attribute, although a little cumbersome I admit.

The answer to that will always be false

Did you perhaps mean this?

Check if my door has been open any time in the last hour despite if it is now closed.

If so:

condition:
  condition: or
  conditions:
    - condition: state
      entity_id: binary_sensor.door
      state: 'on'
    - condition: template
      value_template: "{{ states.binary_sensor.door.last_changed > now() - timedelta(minutes=60) }}"

Or in shorthand notation:

condition:
  or:
    - condition: state
      entity_id: binary_sensor.door
      state: 'on'
    - "{{ states.binary_sensor.door.last_changed > now() - timedelta(minutes=60) }}"
1 Like

but this will tell me if the door sensor changed in the past hour… It can go into several states… “unavailable” for example…
I gave the door as an example… but I would use it in other circumstances as well… for example. to check if a person was at work (zone) this morning etc…

Probably it can be achieved via templating… but I guess the goal of the WTH is to make it more user friendly…

1 Like

This is example of something you could use History Stats sensors for…

sensor:
  - platform: history_stats
    name: Door open last hour
    entity_id: binary_sensor.my_door
    state: "on"
    type: count
    duration: "01:00:00"
    end: "{{ now() }}"

This would give you a sensor with a value of 0 if the door had not been open in the last hour.

Consider voting for History stats in the UI

4 Likes

Is there a smarter solution than this one meanwhile?

If history_stats would be configurable from the UI this would maybe be a more attractive approach.

(via Check an entity's previous state)

2 Likes