Create sensor for showing sensors last update?

Hi,

I discovered earlier today that one of my Aqara sensors hadn’t updated for six hours - so had to refresh it manually. To keep track of any lost connections, I’d like binary sensors showing my the timestamp for last sensor update.

I’ve followed a couple of guides, but none seem to work. My sensor only displays “off”.

My current code:

    sensor_humidity_lastupdate:
      friendly_name: "humidity room"
      value_template: >-
        {{ (as_timestamp(now())-as_timestamp(states.sensor.humidity.last_updated)) }}
      entity_id: sensor.humidity

Do I need to add code somewhere or remove some instead?

Hey @steadi,
The binary sensor can only output two states: on / off.
You’d need a sensor for that. Let me know if you need help!

you’re right - that was a newb mistake on my part.

I’ve put the code in my sensor-part of configuration.yaml, but the log now gives an error that “last_updated” can’t be used in a sensor?

No worries mate. I checked your code in the meantime and can confirm that something needs to be adjusted… You could try this one:

    sensor_humidity_lastupdate:
      friendly_name: "humidity room"
      value_template: >-
        {{ relative_time(states.sensor.humidity.last_updated) }}
      entity_id: sensor.humidity

returns something like: 15 minutes

EDIT: This doesn’t work - as it doesn’t update automatically and you’d need to run homeassistant.update_entity :sweat_smile:

This gives me the time in seconds. Tested under developertools -> templates.
{{ (as_timestamp(now()) - as_timestamp(states.binary_sensor.motion_sensor_hallway.last_changed)) }}

Finally got it working! This shows in minutes (you can change this in the code below)

{{ ((as_timestamp(strptime(states('sensor.date_time'), "%Y-%m-%d, %H:%M")) - as_timestamp(states.sensor.humidity.last_changed)) / 60) |round() }}

It’s future-proof (0.115 will bring some changes) and updates every minute (as_timestamp(now()) also doesn’t update the sensor regularly btw). Additionally you need to add the following to your sensor part:

  - platform: time_date
    display_options:
      - 'date_time'

PS: I suggest to exclude both sensors from the recorder. Again, if you need help with this - just ask :smiley:

1 Like

Will try this as soon as “family duties” are done!

I’m running my recorder on include, not exclude. Found it easiest to choose what to record and leave out the rest. My database is nice and small :slight_smile:

If you’re putting this into a normal entities card you can do multiple things that are easier.

template sensor

    sensor_humidity_lastupdate:
      friendly_name: "humidity room"
      value_template: "{{ states.sensor.humidity.last_updated }}"
      device_class: timestamp

or in lovelace entities card (not last updated, this is last change)…

- type: entities
  entities:
  - entity: sensor.humidity
    secondary_info: last-changed
6 Likes

Had to add “platform: template” and “sensors” to your template sensor, but it works. Thank you!
This only shows time of last update, right and not doing any refreshing of the sensor itself? Just wondering with regards to sensor battery.

It only works inside an entities card. It will show '5 minutes ago` if the timestamp was 5 minutes ago. So the sensor barely updates but the UI will constantly look like it’s updating.

ahh, so I can’t use it for automations, for example?

Just so I understand your UI/update comment; the template sensor will just update with whatever timestamps it gets when the sensor updates, but won’t do any active refreshing of the humidity sensor?

Boy, just when I think I’m starting to understand Home Assistant :slight_smile:

when trying your code, my sensor returns “-1”?

However, if I enter it in the template editor it seems to work…

Hmm that’s strange. It works fine on my side:

last_updated
(top is Petro’s second suggestion, bottom is my second)

Did you wait a few minutes to check whether the value changes?


Addition:

If you don’t want to create a separate sensor for every single device you want to observe, you could also create such a binary sensor as a base for notifications (5 hours inactive --> on):

      inactivity:
        friendly_name: Inactivity?
        device_class: problem
        value_template: >-
          {{ ((as_timestamp(strptime(states('sensor.date_time'), "%Y-%m-%d, %H:%M")) - as_timestamp(states.sensor.humidity.last_changed)) / 3600) |float > 5
              or ((as_timestamp(strptime(states('sensor.date_time'), "%Y-%m-%d, %H:%M")) - as_timestamp(states.sensor.whatever.last_changed)) / 3600) |float > 5
              or ((as_timestamp(strptime(states('sensor.date_time'), "%Y-%m-%d, %H:%M")) - as_timestamp(states.sensor.whatever2.last_changed)) / 3600) |float > 5
              or ((as_timestamp(strptime(states('sensor.date_time'), "%Y-%m-%d, %H:%M")) - as_timestamp(states.sensor.whatever3.last_changed)) / 3600) |float > 5
              or ((as_timestamp(strptime(states('sensor.date_time'), "%Y-%m-%d, %H:%M")) - as_timestamp(states.sensor.whatever4.last_changed)) / 3600) |float > 5 }}

then put all above mentioned sensors in an entities card with secondary-info: last-changed, so you can check which one is not reporting. I’m pretty sure there are simpler ways to achieve this - but it works :man_shrugging:

when you restart, last_changed get’s wiped and you need to wait for a new state change.

oh, that’s a clever way of doing it as a binary sensor!

My current result in lovelace with “your” sensor is: -0.06628673473993937 or a varying minus number.

  - platform: template
    sensors:
      sensor_humidity_lastupdate:
        friendly_name: "Office - humidity"
        value_template: >-
          {{ ((as_timestamp(strptime(states('sensor.date_time'), "%Y-%m-%d, %H:%M")) - as_timestamp(states.sensor.humidity.last_changed)) / 60  |round()) }}
        entity_id: sensor.humidity
        device_class: timestamp

Same value_template code seems to return correct time in developer tools.

It’s not a timestamp, so device_class: timestamp should be removed. Also, an incoming change in 0.115 makes the entity_id field no longer needed. It’s best if you remove it now.

If you use an external DB - it’s much easier to create an sql server. Hidden benefit is that you can check last update even after restart of HA. This is very important for me for some kind of events like motion detection and etc.

I know that this post is pretty old by now, however, I wanted to share a slightly cleaner template. This allows you to pass a threshold in hours for any of the sensors.

{%- macro stale(reading, threshold) %}
{%- set current = as_timestamp(now()) | float %}
{{- (current - as_timestamp(reading)) | float / 3600 > threshold -}}
{%- endmacro %}
{% set anystale = 'True' in [
    stale(states.sensor.dining_room_temperature_temperature.last_changed, 6),
    stale(states.sensor.garens_bedroom_temperature_temperature.last_changed, 6),
    stale(states.sensor.recroom_temperature_temperature.last_changed, 6),
    stale(states.sensor.server_room_temperature_temperature.last_changed, 6),
    stale(states.sensor.master_bedroom_temperature_temperature.last_changed, 6),
    stale(states.sensor.tawnis_office_temperature_temperature.last_changed, 6),
    stale(states.sensor.tylers_office_temperature_temperature.last_changed, 6),
  ]
%}
{{ anystale }}
2 Likes