Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdziekon/237b53e2e7f97f704e159fb655948f8e to your computer and use it in GitHub Desktop.
Save mdziekon/237b53e2e7f97f704e159fb655948f8e to your computer and use it in GitHub Desktop.
ha_lovelace_autoentity_potentially_offline_mqtt_devices

Based on

type: custom:auto-entities
filter:
template: >-
{%
set offline_seconds = 12 * 60 * 60
-%}
{%
set device_first_entity = integration_entities('mqtt')
| map('device_id')
| reject('none')
| unique
| map('device_entities')
| map('first')
| list
-%}
{%
set entities_with_lastseen = namespace(entities = [])
-%}
{%- for entity in device_first_entity -%}
{%- if state_attr(entity, 'last_seen') is not none -%}
{%- set entities_with_lastseen.entities = entities_with_lastseen.entities + [ entity ] -%}
{%- endif -%}
{%- endfor -%}
{%-
set possibly_offline = namespace(entities = [])
-%}
{%-
set now_timestamp = as_timestamp(utcnow(), 0) | int
-%}
{%-
set offline_timestamp = now_timestamp - offline_seconds
-%}
{%- for entity in entities_with_lastseen.entities -%}
{%- set last_seen = as_timestamp(state_attr(entity, 'last_seen'), 0) | int -%}
{%- if last_seen < offline_timestamp -%}
{%- set possibly_offline.entities = possibly_offline.entities + [ entity ] -%}
{%- endif -%}
{%- endfor -%}
{%- for entity in possibly_offline.entities -%}
{{
{
'type': 'attribute',
'entity': entity,
'attribute': 'last_seen',
'prefix': 'Last seen',
'format': 'relative'
}
}},
{%- endfor -%}
show_empty: true
card:
type: entities
title: Potentially offline MQTT devices (>12h)
sort:
method: attribute
attribute: last_seen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment