Automation condition - if a state of an entity hasn't changed

i need to add a condition to an existing automation which will have the following logic:
“execute only if a state of entity XXX hasn’t changed for the last 5 mins”
what is the best way to implement such a condition?

If you want to know if it’s been in a specific state you can use the state trigger for that.

In general though, you can use a template trigger to check that.

automation:
  trigger:
    platform: template
    value_template: "{{ (as_timestamp(now()) - as_timestamp(states.YOUR.ENTITY.last_changed)) > 300 }}"
3 Likes

now() won’t update a trigger template.

1 Like

thanks - works great

You’re right, should have used a sensor.time_date entity there

sensor:
  - platform: time_date
    display_options:
      - 'time'
automation:
  trigger:
    platform: template
    value_template: "{{ (as_timestamp(states.sensor.time.last_changed) - as_timestamp(states.YOUR.ENTITY.last_changed)) > 300 }}"
1 Like

Once, at startup, then it won’t update ever again … until the next restart. Because the value_template uses now() which is a function, not an entity (see tom_I’s comment).

Be sure to update the automation using Tinkerer’s revised version that employs sensor.time.

I’ve got to say (and maybe I’ve been a bit slow on the uptake) that is genius.
I’ve obviously known for a long time and regularly seen people say how you can’t use now() as a trigger but no one has ever offered this as an alternative.

Should the Trigger docs be updated to include this with it’s warning not to use now()?

It would be a worthwhile addition.

That only works if you’ve added the sensor as I did, but you could submit an edit to that end.

I’d do it but I clicked on:
image
and I’m afraid I didn’t know what to do after that!

Are there any instructions anywhere?

Fortunately there’s a tutorial :slight_smile:

2 Likes

Hit the pencil icon. The developer docs cover things to know.

The basic minimal process is:

  1. Edit the page, following the standards (linked above)
  2. Check your changes with the preview
  3. Submit the PR (there’s a few stages to that)
  4. Use the Show all checks -> Netlify -> Details option to check that you didn’t mess it up
  5. Wait for a review and then fix anything needed
1 Like

thanks for the update guys - fixed

Old thread, but great advice!

I was wondering how frequently the sensor updates (every second? Every minute?) and the anser seems to be for time it is once a minute:

Sensors including the time update every minute, the date sensor updates each day at midnight, and the beat sensor updates with each beat (86.4 seconds).

So this works great for minutes resolution.

For lags that are shorter, I suspect one would need to trigger a delay on each state change, and in restart mode it should only succeed if there was no interim state change.