Listing all booleans with certain label in on state

Hi all

Looking for a little help here and know it will be simple for many but not for me as I can do most things with HA but struggle a little with templating.

How do I create a list (preferably with line breaks) of all input Booleans that have the label X and are in the ON state?

Thanks in advance

Pico

Line breaks in the template are just for readability:

{{ states['input_boolean'] 
   |selectattr('entity_id','in',label_entities('X'))
   |selectattr('state','==','on')
   |map(attribute='entity_id')
   |list
   |join('\n') }}

Line 1 gets all the input boolean state objects. It’s equivalent to states.input_boolean but I prefer the bracket notation which is almost always either equivalent or better. (Accessing state objects)

Line 2 filters that list to those elements that have the label you want. (Docs for selectattr, in, and label_entities)

Line 3 then filters again to only select state objects with a state of on.

Line 4 then returns just the entity IDs of the state objects. (map)

Line 5 turns this “generator” into a list (in effect, it “runs” the set of statements and gets the output). (more on generators only if interested)

Line 6 then joins the list items together with a line break character. You might want to use <br> instead of \n depending on your application. (join)

Remember that if you’re using this as a state for a template sensor, states are limited to 255 characters.

Example on my system (using Tablet label, binary sensors and off state so that there’s more than one):

2 Likes

Thank you Troon.

Super helpful as not only did you answer my question but really appreciate the explanation behind it also.

Cheers

1 Like