Template not working when select('is_state', 'on') is used

I’m trying to develop some template sensors that will provide how many lights are on in an area, for example. I have an area with just three lights in it for testing purposes:

As soon as I add in an is_state check, I start getting this error:

AttributeError: 'TemplateState' object has no attribute 'lower'

As far as I can tell I’m using the select filter correctly. I’ve been digging around in the docs (which show what I think are similar examples) and I cannot understand where that error is coming from. One of the three light entities happens to be unavailable, so I added a filter to remove it, but the same error still happens:

However, I finally tried switching the select filter to selectattr, and it works:

Is this a bug, or am I misunderstanding how select is supposed to be used?

I believe the problem is that the first argument to is_state is an “entity_id” string or array of strings. However, you are piping the output of selectattr, which will be an array of entity objects (once the generator generates them). Therefore you could do it after you use map to get the “entity_id”:

{{ 
  states.light 
  | selectattr('entity_id', 'in', area_entities('Family Room'))
  | map(attribute='entity_id')
  | select('is_state', 'on')
  | list
}}
2 Likes

What @michaelblight showed works. Alternatively, just use it in a selectattr statement on entity_id. It works just like the in Jinja test you’re already using that way:

{{ 
  states.light 
  | selectattr('entity_id', 'in', area_entities('Family Room'))
  | selectattr('entity_id', 'is_state', 'on')
  | map(attribute='entity_id')
  | list
}}

Just to present an alternative for next time you run into something similar.

1 Like

Use the suggestion I made in your other topic:

There’s no need for the template to begin by filtering the entire light domain. It can start with all the entities in the area itself and then select by matching the domain(s) you want.

{{ area_entities('Family Room') | select('match', 'light')
   | select('is_state', 'on') | list }}