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) }}"
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…
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
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.
Was looking for something like this as well.
Example of need:
If the front door has been opened and the living room has been unoccupied in the last minutes, then set TTS “Welcome Home”.
I can’t use me or my phone being home as a condition because this is also true when I leave the house.
Open to other ideas.
Make a trigger based template binary sensor.
E.g.
template:
- trigger:
- id: 'on'
platform: state
entity_id: binary_sensor.living_room_motion
from: 'off'
to: 'on'
- id: 'off'
platform: state
entity_id: binary_sensor.living_room_motion
from: 'on'
to: 'off'
for:
minutes: 5
binary_sensor:
- name: Living Room Motion Last 5 Minutes
state: "{{ trigger.id }}"
Then use that as a condition.
For some reason that didn’t work for me. The sensor stayes on “Unknown”. In the process of debugging it, I created a “Toggle” / “Input Boolean” helper plus automation instead:
alias: EG.Küche - Sonos 5min
description: ""
triggers:
- trigger: state
entity_id:
- media_player.eg_kueche_sonos_one
id: "on"
to: playing
- trigger: state
entity_id:
- media_player.eg_kueche_sonos_one
id: "off"
from: playing
for:
hours: 0
minutes: 5
seconds: 0
conditions: []
actions:
- if:
- condition: trigger
id:
- "on"
then:
- action: input_boolean.turn_on
metadata: {}
data: {}
target:
entity_id: input_boolean.eg_kuche_sonos_one_playing_in_last_5_minutes
else:
- action: input_boolean.turn_off
metadata: {}
data: {}
target:
entity_id: input_boolean.eg_kuche_sonos_one_playing_in_last_5_minutes
mode: single
It’s a bit more verbose but works for me.