Trigger automation if no state update for x minutes

@ReneTode @timstanley1985

I have replicated this situation and it appears to be working for me. Not sure if it will be useful or not but here it goes…

I am using the ping function of the speed test component as my sensor.

sensor:
  - platform: speedtest
    server_id: 1234
    minute:
     - 0
     - 30
    monitored_conditions:
     - ping

I then created a template sensor that counts the time it has been since it was last updated.

  - platform: template
    sensors:
      ping_last_update:
        value_template: "{{ relative_time(states.sensor.speedtest_ping.last_updated) }}"

My automation uses a template trigger to read the state of the template sensor

automation:
- alias: notify if sensor state unchanged
  trigger:
    platform: template
    value_template: "{% if is_state('sensor.ping_last_update', '2 minutes') %}true{% endif %}"
  action:
    service: persistent_notification.create
    data:
      notification_id: "1234"
      message: "Sensor is Down"
      title: "Test"

I get a notification when the template sensor hits 2 minutes.

Regards

2 Likes

but does the template sensor change value in the frontend?

It changes value in real time and you can watch it update the time since the last update of the sensor (sensor you are monitoring). Since it is an entity you can add it to the front end but it is not necessary. I was just watched it from the Dev tools-> states page. If I forced the speedtest sensor to update then the template sensor went back to 0 and started counting up again. It appeared to update every minute.

2 Likes

i finally found a general way to see how long ago a sensor is updated. in a format i like and it updates in the frontend.
@PtP got me thinking again.

i made a date_time sensor and use that as entity from the template.

so it is:

sensors:
  - platform: time_date
    display_options:
      - 'date_time'
  - platform: template
    sensors:
      controle_sensor:
        friendly_name: any sensor
        value_template: '{% if states.sensor.your_sensor.last_updated is undefined %}{{"00:00:00"}}{% else %}{{ ((as_timestamp(states.sensor.date__time.last_updated)-as_timestamp(states.your_sensor.last_updated))|timestamp_utc)[11:19] }}{% endif %}'
        entity_id:
          - sensor.date__time

it gives the time gone by as 00:00:00 (but it is a string)
to lose the seconds change [11:19] to [11:16]
to see the amount of seconds gone by as int replace |timestamp_utc with .seconds and lose the [11:16] part

8 Likes

The thread is very interesting. Monitoring for stale data from a sensor looks to be very useful. I am trying another approach, however. I am attempting, with mixed sucess to use device_tracker to determine if a sensor is transmitting. NMAP performs the needed function for showing on the UI status (Home or Away) for the sensor. However, NMAP causes my rpi3 wifi to lockup after a short run time. Still trying to sort out the cause of that. NOTE: edited original post to correct error citing nodemcu wifi issue instead of rpi3 wifi issue.

Trying to get the seconds version to work and have below based on you comment “replace |timestamp_utc with .seconds and lose the [11:16] part”.

Could you point me in the right direction (again)?

  - platform: template
    sensors:
      last_motionv2:
        friendly_name: Last motionv2
        value_template: '{% if states.group.motion_indoors.last_updated is undefined %}{{"00:00:00"}}{% else %}{{ ((as_timestamp(states.sensor.date__time.last_updated)-as_timestamp(states.group.motion_indoors.last_updated)).seconds) }}{% endif %}'
        entity_id:
          - sensor.date__time

the part with .seconds doesnt seem to work anymore.

please try:

{% if states.group.motion_indoors.last_updated is undefined %}{{"00:00:00"}}{% else %}{{ ((as_timestamp(states.sensor.date__time.last_updated)-as_timestamp(states.group.motion_indoors.last_updated))|int) }}{% endif %}

i tried it and it works for me.

1 Like

Thanks. That did it.

An Appdaemon solution: https://gist.github.com/adrianlzt/f0c073dd163e4512510ca3d8fa91a4c2

Can i check, is this working for anyone?

I have added an option “expire_after” to mqtt sensor that does exactly what you describe here. See https://github.com/home-assistant/home-assistant/issues/6705 for details. Pull request is coming as soon as my test runs.

1 Like

As of 0.53, there’s a new counter component that might be used to check if something isn’t working. For example, setting a counter at 24, and decreasing it by one every hour (resetting it at 24 whenever an event arrives), you could set an automation triggered by the counter reaching 0 that would let you know it’s been 24h since last update.

Does it work with 0.54? HA show 0 seconds on this sensor and does not sens alerts.

I am trying something like this:

- alias: Notify if THB1 sensor state unchanged
  trigger:
    platform: template
    value_template: "{% if as_timestamp(states.sensor.date__time.last_updated) - as_timestamp(states.sensor.thb_1_0.last_updated) > 5*60 %}{% endif %}"
  action:
  - service: notify.olivia
    data:
      message: 'THB1 sensor state unchanged in the last 24hours'

But it is not working. To be specific…Trigger is not working.

1 Like

Sensor.date_time looks like it has a double underscore, is that a typo?

No. It is correct. I have finally got it working. I’ve forgotten “true”

Correct line :

value_template: "{% if as_timestamp(states.sensor.date__time.last_updated) - as_timestamp(states.sensor.thb_1_0.last_updated) > 5*60 %}true{% endif %}"
5 Likes

What is the 5*60 part? 5 hours?

No, 5 minutes

I have the same problem with my tellstick sensors, however their value does not say “undefined” it just stays the same temperature as the last reading. I assume your solution wont work for this case?

Just want to make sure that you actually have the sensor say “undefined” as a value when its battery died

i never had any battery operated sensors in HA.
and my solution was something i figured out for someone else, 4 years ago!
i doubt if it still would work with the amount of changes that HA made in those 4 years.