Paste this template into the Template Editor. It will list all sensors whose states have not changed in the past hour (3600 seconds, change it to whatever duration you prefer).
{{ states.sensor
| selectattr('last_changed', 'lt', now()-timedelta(seconds=3600))
| map(attribute='entity_id') | join(', ') }}
You can use that as a starting point to create a template that checks for ‘stale’ entities. If you want it to check sensors and binary_sensors, use expand()
.
{{ expand(states.sensor, states.binary_sensor)
| selectattr('last_changed', 'lt', now()-timedelta(seconds=3600))
| map(attribute='entity_id') | join(', ') }}
If you already have a group containing all of the entities you wish to check then the template changes to this:
{{ expand('group.check')
| selectattr('last_changed', 'lt', now()-timedelta(seconds=3600))
| map(attribute='entity_id') | join(', ') }}
You can implement the template in an automation that periodically checks the desired entities (much like iridris is doing).
- alias: Stale Entities
trigger:
- platform: time_pattern
hours: '/4'
action:
- variables:
stale: >
{{ expand(states.sensor, states.binary_sensor)
| selectattr('last_changed', 'lt', now()-timedelta(seconds=14400))
| map(attribute='entity_id') | list }}
- condition: template
value_template: "{{ stale | count > 0 }}"
- service: notify.persistent_notification
data:
title: 'Stale Entities Found'
message: "{{ stale | join(', ') }}"