- How to transform each entity to apply custom params
- thomasloven/lovelace-auto-entities#247
- https://community.home-assistant.io/t/auto-entities-automatically-fill-cards-with-entities/147801/511?u=asnnetworks
- Remember about escaping (
{%- -%}
) and returned object quotations
- How to append to list in Jinja2
- https://community.home-assistant.io/t/solved-how-to-append-items-to-list-using-template/309899/2
namespace
is necessary, otherwise the list is not updated (maybe the reference can't be changed?)
Last active
December 10, 2022 02:52
-
-
Save mdziekon/237b53e2e7f97f704e159fb655948f8e to your computer and use it in GitHub Desktop.
ha_lovelace_autoentity_potentially_offline_mqtt_devices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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