How to get all the entities within a Domain with the same label

I have a label (Plant Light) that has both light entities and switch entities. I know I can use something like:

{% set Plant_lights = label_entities('Plant Light')  | list%}
{% for item in Plant_lights %}
 {% if states[item].domain == 'light'%}
 {{item}}
 {% endif %}
{% endfor %}

to filter the label entities by domain, but is there a shorter, 1 line filter I’m missing? I’m guessing by using select(), but I can’t seem to work it out.

Thanks

1 Like

The trick is to use expand to convert the list of entity ids into entities:

{{ label_entities('Plant Light')
  | expand
  | selectattr('domain', 'eq', 'light')
  | list
}}
{{ label_entities('Plant Light') | select('match', 'light') | list }}

{{ label_entities(‘Plant Light’) | select(‘match’, ‘light’) | list }}

I had considered this as well; isn’t a match going to match any entity with ‘light’ in it though? You could make it ‘light.’ to really narrow it down, but I feel like narrowing by the actual domain is better practice, maybe not?

this creates a weird formatting

[<template TemplateState(<state switch.fig_light=off; device_class=outlet, icon=mdi:light-flood-down, friendly_name=Fig Light @ 2024-12-30T20:45:01.543242-06:00>)>]

This selects strings that begin with the word light.

select('match', 'light')

This selects strings that contain the word light.

select('search', 'light')

So, no, what I suggested won’t select something like switch.kitchen_light. An entity’s entity_id is a string that always begins with its domain so using match is ideal (especially since the result of label_entities is a simple list of strings).

For your application, you would need to use expand only if you wanted to extract a list of light entity_ids in a particular state. Otherwise, select alone is sufficient.


BTW, the second parameter is actually a regex pattern and not merely a string. For example, you can configure search to behave like match using an appropriate regex pattern.

This selects strings that begin with the word light.

select('search', '^light')

Because it’s a regex pattern, a period character has special meaning. It means “match any character” and not “match a single period character”.

This selects strings that begin with the word light followed by any character.

select('search', '^light.')

This selects strings that begin with the word light followed by a period character.

select('search', '^light\.')

This selects strings that begin with the word light and end with the word kitchen with any number of other characters between the two words.

select('search', '^light.*kitchen$')
1 Like

Because the suggested template employs expand which creates a list of state objects, filters the objects by their domain property, and then reports all properties of each filtered object instead of just its entity_id .

In other words, it’s missing a step to select just the entity_id property of each filtered state object. So the template requires yet another step to get the result you want.

Five steps versus merely three if you use select the way I suggested.

1 Like

Yeah I didn’t know what you wanted to do with the entities next. If you just want the entity ids as a list, you can use map to get them out. Or do it as @123 suggested.

This is good info, I appreciate it. Thanks

1 Like