List from Group that meet criteria

I want to create a list of devices in a group that meet a criteria, to show the result in a dashboard.

I’m assuming I would need a template sensor but don’t know where to start so would welcome some advice please.

One of the lists I want to achieve is all window sensors, grouped into a group so the entity is binary_sensor.windows, that have a state of on. If there are none that meet that criteria then I expect a blank list. The result would then be used in a dashboard card.

I should say, I have tried the Auto Entities custom card but this shows the icon and state against each entity but I just want the device/entity name to be displayed.

1 Like

The basic template would be as follows:

{{ state_attr('binary_sensor.windows', 'entity_id') 
| select('is_state', 'on') | list }}

FWIW, the state of any entity is always a string. If you need an actual list the template should be assigned to an attribute since they can be other data types.

EDIT: Fixed “fancy quotes”

2 Likes

Thanks. I’ve added that to a template sensor in config.yaml

  sensors:
    list_windows_open:
      friendly_name: "List Windows Open"
      unit_of_measurement: 'on'
      value_template: "{{ state_attr(`binary_sensor.windows`, 'entity_id') | select('is_state', 'on') | list }}"

but I get the following error when checking config:

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected char '' at 14) for dictionary value @ data['sensors']['list_windows_open']['value_template']. Got "{{ state_attr(binary_sensor.windows`, ‘entity_id’) | select(‘is_state’, ‘on’) | list }}". (See ?, line ?).

It’s a copy/paste error… the quotes around the entity id need to be standard quotes not “fancy” quotes.

Also, you may get errors from the unit of measurement. IIRC, any sensor with a unit of measurement defined expects a numeric value for the state.

Thanks, I just spotted it and amended the quotes. It checks out fine now but I’ve reloaded the config and for some reason the sensor is not showing up in my entities.

Scrub that, I’ve managed to get it showing in my entities but it took a full restart instead of just a reload of all yaml config.

However, I created a markdown card in my dashboard and added {{ states('sensor.list_windows_open') }} and it just returns a result of ‘Unknown’ when there is at least one window open. I’ve also tried amending it to {{ state_attr('sensor.list_windows_open') }} and that returns a blank.

I commented out unit_of_measurement: 'on' in my template sensor and I seem to be getting closer because trying the code in my markdown card in Dev Tools it shows the only door that is open but in this format: ['binary_sensor.kitchen_living_room_door_sensor'].

But I want the device name in my output (e.g. Kitchen/Living Room Door Sensor)

To summarise, this is now what I have:

Template Sensor:

- platform: template
  sensors:
    list_doors_open:
      friendly_name: "List Doors Open"
      value_template: "{{ state_attr('binary_sensor.doors', 'entity_id') | select('is_state', 'on') | list }}"

Markdown Card:
{{ states('sensor.list_doors_open', 'on') }}

Output:
[‘binary_sensor.kitchen_living_room_door_sensor’]

How do I get the output to show the device/friendly name?

Is anyone able to advise on the above, please, because I can’t find any suitable info on how to do this?

My suggestion is to use this template to cycle through each item in your list and print its friendly name:

In your Markdown Card Content:

{% for my_item in states('sensor.list_doors_open', 'on') %}
   {{ state_attr(my_item, 'friendly_name') }}
{% endfor %}

As an alternative suggestion, I use auto-entities to create a card in the front end that lists entities while applying specific filters.
It is a custom frontend card that you install using the HACS app.