Automation When State Hasn't Changed?

I’m trying to create an automation that executes only when the entity status has not changed in the last ‘x’ hours.

From looking around, it seems this is possible. But the instructions, advice, or suggestions are as clear as mud.

Do I need to start with a Platform template in my configuration.yaml file, or can I simply use a value template in the automation trigger?

Any/all examples anyone can provide would be much appreciated. Don’t be afraid to explain things like you would to a small child. I won’t be offended. :slight_smile:

1 Like

What kind of entity are we talking about?
And what is the possible states of this entity?

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.

If you are talking about sensors you could use something like this:

alias: Do something after not state change for x Hours
trigger:
  - platform: state
    entity_id: binary_sensor.whatever
    to: 'off'
    for:
      hours: x

That give you an idea and then you can change it to you needs.

It’s a cover. ‘cover.single_car_garage_4’ to be specific.

State attributes

friendly_name: 'Single Car Garage ’
supported_features: 3
device_class: garage

@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

  1. When the garage has been closed longer than six hours
  2. It’s Monday (trash day)

Trigger
and it’s 8pm

Why - So I don’t forget to take out the trash. :slight_smile:
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.

Hopefully this helps:

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

the weekday can be selected like this: https://www.home-assistant.io/docs/scripts/conditions/#time-condition. No template necessary.

I would trigger at 8 pm. then use a condition to check that the door hasn’t been opened for 6 hours and a condition for monday:

trigger:
  - platform: time
    at: '20:00:00'
condition:
  - condition: state
    entity_id: cover.your_gd
    state: 'closed'
    for:
      hours: 6
  - condition: time
    weekday:
      - mon
action:
  - your action
2 Likes

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.

That was actually an oversight because I was kinda rushing it. Agreed it needs to be in the condition as well.

And I misread 8pm rather than the ‘am’ that I coded for… I’m used to 24 hour time

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.

1 Like

Good point but I’m not sure honestly.

I can’t really test it right now, tho.

If that fails then I guess I’ve been lucky since I don’t think I’ve run into it before…that I can remember at least.

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 :+1:

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.

1 Like

In that case it’s better to set an input datetime with the automation.
That way it’s easily accessible

Certainly could do, just adds an extra step.

Well… not really.
It creates another entity but the amount of steps is the same.

So you don’t have to create the ‘input_datetime’ in the first place ?