Using Template For List Entities In Entities Card

Hi All,

I’m trying to create an entities card with dynamically generated entities in it.
I’ve tried multiple way, but none of them worked.

First Method:

type: entities
entities: {{ states.sensor | rejectattr('state', 'in', ['unavailable',  'unknown', 'none']) | selectattr('attributes.device_class', 'eq', 'battery') |  map(attribute='entity_id') |list  }}

This template returns a json list like this (from developer tools):

[
  "sensor.janoss_iphone_battery_level",
  "sensor.temperature_humidity_sensor_2264_battery",
  "sensor.temperature_humidity_sensor_27a2_battery",
  "sensor.temperature_humidity_sensor_5a98_battery",
  "sensor.lumi_lumi_sens_battery",
  "sensor.lumi_lumi_sens_battery_2",
  "sensor.lumi_lumi_sensor_motion_aq2_battery",
  "sensor.lumi_lumi_sens_battery_3",
  "sensor.lumi_lumi_sens_battery_4",
  "sensor.xiaomi_lywsd03mmc_z_battery",
  "sensor.xiaomi_lywsd03mmc_z_battery_2"
]

But I get the following error:

Configuration errors detected:
missed comma between flow collection entries (2:91)

1 | …
2 | … available’, ‘unknown’, ‘none’]) | selectattr('attributes.devic …
-----------------------------------------^
3 | …

I’ve tried surrounding the template with double and single quote as well.

But! This works fine:

type: entities
entities: ["sensor.janoss_iphone_battery_level",
    "sensor.temperature_humidity_sensor_2264_battery",
    "sensor.temperature_humidity_sensor_27a2_battery",
    "sensor.temperature_humidity_sensor_5a98_battery",
    "sensor.lumi_lumi_sens_battery",
    "sensor.lumi_lumi_sens_battery_2",
    "sensor.lumi_lumi_sensor_motion_aq2_battery",
    "sensor.lumi_lumi_sens_battery_3",
    "sensor.lumi_lumi_sens_battery_4",
    "sensor.xiaomi_lywsd03mmc_z_battery",
    "sensor.xiaomi_lywsd03mmc_z_battery_2"
  ]

Second Method:

type: entities
entities: |
  {% set group_list = states.sensor | rejectattr('state', 'in', ['unavailable',  'unknown', 'none']) | selectattr('attributes.device_class', 'eq', 'battery') |  map(attribute='entity_id') |list  %}
  {% for g in group_list %}
  - {{ g }}
  {%- endfor %}   

Error message:
image

This template returns this in Developer tools:

Here I facing another issue: Wrong indention in the first line. (Missing two spaces in the first row of the list).

Is it even possible to use template in the entities card this way?

Any help would be appreciated.

Thanks,
Janos Vincze

Templates are not supported in standard cards. Exception - markdown.
Google about auto-entities custom card. There is a dedicated thread for this card to discuss it.

1 Like
  1. Just return a list, do not worry about what the yaml looks like because the template resolver does not parse yaml. It parses Jinja/python objects, so the output of your template better look like a jinja/python list of entity_ids.

  2. you need to use auto-entities custom card, you don’t even need templates.

type: custom:auto-entities
card:
  type: entities
filter:
  include:
  - domain: sensor
    attributes:
      device_class: battery
  exclude:
  - state: unavailable
  - state: unknown

Thank you very much for the answers.
I aware of “auto-entities” custom card, just wanted to know if it is possible to use standard template for this.

Thank you again!

Auto Entites works fine: