Sensor last_updated on dashboard

Pretty basic but it works so it’s a good starting point if someone else wants it. Basically there’s a simple python script that checks the last_updated time for an entity and returns how long it’s been in hours. You then create some custom templates for each item you want to check and you can see if that sensor needs to be checked or not.

I put this in my .homeasisstant dir but it can live anywhere you just have to update the template accordingly

get_last_updated.py

#!/usr/bin/env python

import time
import sys
import homeassistant.remote as remote
from datetime import timezone, datetime

entity_name = sys.argv[1]

api = remote.API('192.168.1.98', '')
entity = remote.get_state(api, entity_name)

if entity is None:
  print('100000')
  quit()
last_updated = entity.last_updated
now = datetime.now(timezone.utc)

diff = now - last_updated

seconds = diff.seconds
minutes = int(seconds / 60)
hours = int(minutes/60)
days = int(hours/24)
weeks = int(days/7)
# You can output what ever you like here, I'm using hours
print(hours)

Then you need a template for each sensor. Here’s a sample of mine. In mine if it goes longer than 168 hours (a week) without an update the text changes to CHECK ME instead of OK. I only change the name and the end of the command for each new entry.

  - platform: command_line
    name: Mudroom Door Updated
    command: "/home/homeassistant/.homeassistant/get_last_updated.py sensor.mudroom_door_alarm_level"
    scan_interval: 1800
    value_template: >-
        {%- if value | int > 168 %} CHECK ME {% else %} OK {%- endif %}
  - platform: command_line
    name: Mudroom Motion Updated
    command: "/home/homeassistant/.homeassistant/get_last_updated.py binary_sensor.mudroom_motion_sensor"
    scan_interval: 1800
    value_template: >-
        {%- if value | int > 168 %} CHECK ME {% else %} OK {%- endif %}

You could alert off this but I just stuck it in a group

Last Updated:
  entities:
    - sensor.slider_door_updated
    - sensor.entry_door_updated
    - sensor.mudroom_door_updated
    - sensor.mudroom_multi_motion_updated
    - sensor.mudroom_motion_updated
    - sensor.entry_motion_updated
5 Likes

Thank you for sharing this project. I was just starting my research on doing something similar.