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 }}"
1 Like

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