I have an automation that creates a variable length list of entity_ids. Currently I’m storing the list in the attributes of a sensor but I can change the automation to provide the list to HA in whatever way is most efficient if there is a better method.
All I want to do is display that list of entities in the lovelace UI along with secondary_info: last_change but I’ve found this remarkably difficult to accomplish.
I’ve tried custom cards like config-template-card or auto-entities but they seem to have a hard time iterating though a list to achieve what I want.
There’s even an example on that page that does the job you’re after.
card:
type: markdown
content: |
My attribute list:
{% for e in state_attr('sensor.MY_SENSOR','MY_ATTRIBUTE') %}
- {{ e }}
{%- endfor %}
Last updated: {{ states['sensor.MY_SENSOR']['last_changed'] }}.
Using a “template” option, you can parse & iterate entries.
These entries may be stored inside an attribute (as you mentioned) - but I would choose storing inside an input_text (easier in an automation). Place ids separated by some delimiter like “@” or whatever.
Example: create a list in an automation:
{% set LIST = ['sun.sun','zone.home','person.ildar'] -%}
{%- set DELIMITER = '@' -%}
{%- set ns = namespace(entries='') -%}
{%- for ENTRY in LIST -%}
{%- set ns.entries = ns.entries + ENTRY + DELIMITER -%}
{%- endfor -%}
{%- set STRING = ns.entries -%}
Then call a service to assign this “STRING” to some input_text.
How to extract ids:
type: custom:auto-entities
card:
type: entities
filter:
template: >-
{% set STRING = states('input_text.xxxxxx') -%}
{%- set DELIMITER = '@' -%}
{%- for ENTRY in STRING.split(DELIMITER) -%}
{%- if not loop.last -%}
{{
{
'entity': ENTRY,
'secondary_info': 'last-changed'
}
}},
{%- endif -%}
{%- endfor %}
Thanks @Ildar_Gabdullin. You set me on the right path that I finally got the following to work:
card:
type: custom:auto-entities
card:
type: entities
title: Not Reporting Sensors
filter:
template: >-
[
{%- set entities = state_attr('sensor.not_reporting_sensors','entity_id') %}
{%- for entity in entities %}
{
"entity": "{{ entity }}",
"secondary_info": "last-changed"
}{%- if not loop.last %},{%- endif %}
{%- endfor %}
]
I still need to use the entity attributes because the it seems that a sensor state has a character limit (I presume 255 which I know is the limit for input_text).