I’ve been using Home Assistant for about a week now, so I am very new to all of this. While I have extensive programming experience, I am brand new to YAML and the methods available on this platform. In short, I don’t know what I don’t know.
I have a ring door sensor that I am using to detect if a door has been open for at least 10 minutes. If it is closed within that time, then everything is fine. If not, it sends me an alert. I have a delay in the initial trigger so it does not notify me if the door is closed within that time frame, however, I cannot figure out how to only send the door closed notification if the door had been open for more than desired duration, so currently it will always send me a close alert or I disable it and it will never send one.
I can get the last state change time, but I don’t know how I would compare that to the previous state, or if I am even on the right track in thinking this way. Help is greatly appreciated.
Post your current automation configuration.
After fiddling, I kinda started over with these:
alias: Door Open Alert
description: ""
triggers:
- type: opened
device_id: 2ead201b2aa84dea732071f8aca72aa0
entity_id: 93c71d614d0f8ad3c4054e11c82a4b45
domain: binary_sensor
trigger: device
for:
hours: 0
minutes: 10
seconds: 0
conditions: []
actions:
- action: <My Email Server>
metadata: {}
data:
message: >-
The door has been left open for 10 minutes as of {{
now().strftime('%c') }}
title: Home Assistant Alert
target: <Email ID>
mode: single
alias: Door Closed
description: ""
triggers:
- trigger: state
entity_id:
- automation.door_alert
attribute: last_triggered
alias: Door closed
conditions: []
actions:
- action: <My Email Server>
metadata: {}
data:
title: Home Assistant Alert
message:Door closed after being open for at least 10 minutes
target: <Email ID>
mode: single
Your “Door Closed” automation will fire immediately after the Alert automation… you have not included any connection to the actual state of the door.
Could you point me in a direction to do that? I figured that is what the last_triggered would do. I appreciate the response.
The trigger for your second automation literally says to fire on any change to last_triggered
on your first automation. Naturally that will make it fire immediately after your first automation does. Neither automation ever checks whether the door has been closed.
Triggering when the door closes is trivial. Triggering when the door closes, but only if it has been open for at least ten minutes, is (probably?) beyond the scope of what can be accomplished in the visual editor. Not impossible of course, but would require maybe something like this:
alias: Door Open Alert
triggers:
- trigger: state
entity_id: binary_sensor.my_door_sensor
to: "on"
for:
minutes: 10
id: door_open
- trigger: state
entity_id: binary_sensor.my_door_sensor
to: "off"
id: door_closed
conditions:
- or:
- condition: trigger
id: door_open
- and:
- condition: trigger
id: door_closed
- "{{ trigger.from_state.state == 'on' }}"
- "{{ trigger.to_state.state == 'off' }}"
- "{{ now() - trigger.from_state.last_changed >= timedelta(minutes=10) }}"
actions:
- choose:
- conditions:
- condition: trigger
id: door_open
sequence:
- action: <My Email Server>
data:
title: Home Assistant Alert
message: "The door has been left open for 10 minutes as of {{ now().strftime('%c') }}"
target: <Email ID>
- conditions:
- condition: trigger
id: door_closed
sequence:
- action: <My Email Server>
data:
title: Home Assistant Alert
message: "Door closed after being open for at least 10 minutes"
target: <Email ID>
Completely untested, probably won’t work on your first attempt as I always tend to manage a typo or miss a type conversion on my first attempts, but should get you started. Also do not use device triggers/actions/conditions as they are barely human readable, you will need to enter the to me unknown human readable entity id into the triggers as I’ve replaced them with state triggers.
That needs to use the from_state
:
{{ now() - trigger.from_state.last_changed >= timedelta(minutes=10)}}
And {{ trigger.to_state.state == 'off' }}
is redundant since you also used the trigger condition.
Always forgetting some little detail, corrected now!
I greatly appreciate the responses. My day job is mostly Powershell, so visual editors bring back memories of VisualBasic, fortunately or unfortunately.
The code is very much appreciated. It took me down the rabbit hole of testing and debugging, which is usually the best way to learn.