WTH state based Jinja functions are not filters or tests

If I have a list of entities this seems like it should work to turn it into a list of the states of those entities:

{{ my_list | map('states') | list }}

Or this to turn it into a list of a particular attribute of each:

{{ my_list l map('state_attr', 'friendly_name') | list }}

But it doesn’t because these are only functions, not filters. You have to use expand and learn the state model instead.

Same with the is_state ones that should be Jinja tests. Like this should work but doesn’t:

{{ my_list | select('is_state', 'on') | list }}

Or this:

{{ my_list | select('is_state_attr', 'brightness', 50) | list }}

Instead you have to do stuff like this and learn the state model:

{{ my_list | expand | selectattr('attributes.brightness', 'eq', 50) | list }}

This is mostly a minor inconvenience but it is a WTH since Jinja doc talks entirely in filters and tests. And these super common utilities are neither.

This is a 4 line code change before tests. I think the limiting factor is politics…

5 lines

        states = AllStates(hass)
        self.globals["expand"] = hassfunction(expand)
        self.filters["expand"] = pass_context(self.globals["expand"])
        self.globals["closest"] = hassfunction(closest)
        self.filters["closest"] = pass_context(hassfunction(closest_filter))
        self.globals["distance"] = hassfunction(distance)
        self.globals["is_state"] = hassfunction(is_state)
        self.globals["is_state_attr"] = hassfunction(is_state_attr)
        self.globals["state_attr"] = hassfunction(state_attr)
        self.globals["states"] = states
        self.globals["utcnow"] = hassfunction(utcnow)
        self.globals["now"] = hassfunction(now)
        self.tests["states"] = states
        self.tests["is_state"] = hassfunction(is_state)
        self.tests["is_state_attr"] = hassfunction(is_state_attr)
        self.tests["state_attr"] = hassfunction(state_attr)

EDIT: It’s not quite this, but you get the idea.

For what it’s worth, I want many of our filters to be tests, same with out methods. Last time I tried to do that I was told “it doesn’t make sense to have these as filters/tests”, when the change was more about making code easy the opposition was more interested in how it read as a line.



There’s a bug when expand is used as a filter (as opposed to a function) within a Template Sensor.

No progress since the bug was reported a month ago so it may be never be corrected; avoid using it as a filter in a Template Sensor (and possibly other places).

It’s in the works.

1 Like

Oh yeah I forgot about that. It’s really frustrating since it works as a filter in dev tools so I forget about it all the time.

Nice! I hoped this was simple, seemed like a good WTH. :+1:

It wasn’t as simple as I thought. I had to look into the jinja python api to figure out why tests weren’t passing context, took a quite a bit of time. Either way, it’s working now.


Merged as part of Home Assistant 2022.11