Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save grantland/5575caff9898d7c5aaef93af560c5804 to your computer and use it in GitHub Desktop.
Save grantland/5575caff9898d7c5aaef93af560c5804 to your computer and use it in GitHub Desktop.
Home Assistant Blueprint: Last seen detection & notification for all sensors
# Modified from sbyx/low-battery-level-detection-notification-for-all-battery-sensors.yaml
# https://gist.github.com/sbyx/1f6f434f0903b872b84c4302637d0890
blueprint:
name: Last seen detection & notification for all sensors
description: Regularly test all sensors with 'last_seen' attribute for crossing
a certain last seen threshold and if so execute an action.
domain: automation
input:
threshold:
name: Last seen threshold
description: Sensors with 'las_seen' below threshold are assumed to be unavailable.
default: 24
selector:
number:
min: 0
max: 168
unit_of_measurement: hours
time:
name: Time to test on
description: Test is run at configured time
default: '10:00:00'
selector:
time: {}
day:
name: Weekday to test on
description: 'Test is run at configured time either everyday (0) or on a given
weekday (1: Monday ... 7: Sunday)'
default: 0
selector:
number:
min: 0.0
max: 7.0
mode: slider
step: 1.0
exclude:
name: Excluded Sensors
description: Sensors to exclude from detection. Only entities are supported, devices must be expanded!
default: {entity_id: []}
selector:
target:
entity:
device_class: timestamp
actions:
name: Actions
description: Notifications or similar to be run. {{sensors}} is replaced with
the names of sensors being low on battery.
selector:
action: {}
source_url: https://gist.github.com/sbyx/1f6f434f0903b872b84c4302637d0890
variables:
day: !input 'day'
threshold: !input 'threshold'
exclude: !input 'exclude'
sensors: >-
{% set result = namespace(sensors=[]) %}
{% for state in (expand(states.sensor, states.binary_sensor, states.light, states.switch) | selectattr('entity_id', 'search', 'last_seen')) -%}
{%- if (as_timestamp(now()) - as_timestamp(state.state) > (60 * 60 * threshold) ) and not state.entity_id in exclude.entity_id %}
{% set result.sensors = result.sensors + [(state.name | replace(" Last seen", "")) ~ ': ' ~ ((as_timestamp(now()) - as_timestamp(state.state)) / (24 * 60 * 60)) | round(0) ~ ' days ago'] %}
{%- endif -%}
{%- endfor %}
{{result.sensors}}
trigger:
- platform: time
at: !input 'time'
condition:
- '{{ sensors != '''' and (day | int == 0 or day | int == now().isoweekday()) }}'
action:
- choose: []
default: !input 'actions'
mode: single
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment