An entity’s State object has a field called last_changed.
state.last_changed
Time the state changed in the state machine in UTC time. This is not updated when there are only updated attributes. Example: 2017-10-28 08:13:36.715874+00:00 .
I suppose you could make an automation that is triggered every hour (using, for example, a Time Pattern Trigger) and its condition calculates the time difference between now and last_changed. If the difference exceeds your desired threshold value, the automation continues to execute its action.
The limitation of using last_changed is that it is updated when Home Assistant restarts. In other words, if Home Assistant is restarted at 07:30, all entities will have their last_changed set to 07:30. That behavior can screw up calculations that employ last_changed.
@TinyDoT, unfortunately, I believe I need this to be a condition rather than a trigger, but I may be wrong. At least this is how I’m thinking about it in my head.
Action
I’m trying to turn a light on or have alexa announce something
Condition
When the garage has been closed longer than six hours
It’s Monday (trash day)
Trigger
and it’s 8pm
Why - So I don’t forget to take out the trash.
If the garage door hasn’t been opened in the last six hours on Monday by 8pm, then the trash hasn’t been taken out yet.
alias: turn on light for trash reminder
trigger:
- platform: time
at: '20:00:00'
- platform: state
entity_id: cover.single_car_garage_4
to: closed
for: '06:00:00'
condition:
- condition: state
entity_id: cover.single_car_garage_4
state: closed
- condition: template
# someone else will need to help with the template to check for Monday
action:
- service: light.turn_on
entity_id: light.some_light
I believe the requirement is a bit more challenging than what can be achieved with a State Trigger and ‘for 6 hours’.
For example, the garage door has to change state to ‘closed’ first (and then remain unchanged for 6 hours). If no one opens/closes the garage door that day, there won’t be any countdown at all. In other words, ‘for 6 hours’ requires an initial state-change before it can start the 6-hour countdown. That’s not quite the same as reporting if a door hasn’t changed state in the past 6 hours.
Finity had the right idea of using it in a condition.
I have a feeling that the use of for is subject to the same drawback as timers and last_changed. If Home Assistant is restarted at any time during the 6-hour period prior to 20:00 the for is reset. That’s a problem with Home Assistant; countdown periods don’t survive restarts. It’s a real challenge to make these duration-based automations immune to restarts.
Yeah, me too; will test it tomorrow. Can’t see how it could know about an entity’s state 6 hours ago if it restarted an hour ago. The entity’s last_changed would be set to an hour ago.
How is your building skills?
I understand that this is a way to solve the problem with software.
But I believe you need hardware this time.
As others mentioned, a restart can break it, if you need to fetch something large from the garage, someone drops by and you want to show that you can open and close the garage with voice or an app and the time is 14:05 on a Monday.
Or perhaps you took out the trash on Sunday/Monday morning and you still get notified.
I suggest a hardware solution because there can be so many reasons this can fail (I assume).
A simple distance sensor that sees if the trash can is in the garage will work great.
If you want to use mechanical ways then a load cell or force sensitive resistor below the trash can will feel if it’s there.
Or a magnet on the trash can and a reed switch.
There is lots of ways to do this and knowing for a fact that it has been done or not.
I believe the software solution will let you down a few times.
Thanks for that. I thought there had to be a simple solution but I only looked under the weekday integration and didn’t find it… because it’s under ‘time’ instead. I’ll keep it in mind for future
Automations don’t lose their history across restarts so you could use the state change of the garage door to trigger an automation and then use the automation’s state.last_triggered to compare to current time.