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.
petro
(Petro)
October 2, 2022, 11:15am
2
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.
petro
(Petro)
October 2, 2022, 11:24am
3
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.
123
(Taras)
October 2, 2022, 2:51pm
5
FYI
There’s a bug when expand
is used as a filter (as opposed to a function) within a Template Sensor.
opened 04:29PM - 02 Sep 22 UTC
closed 05:12PM - 14 Dec 23 UTC
integration: template
stale
### The problem
When `expand` is used as a **filter** in a _legacy_ Template … Sensor, it fails to pass Check Configuration. However, if used as a **function** it passes. When tested in the Template Editor, no error is reported when `expand` is used either as a filter or a function.
Error reported by Check Configuration:
> Invalid config for [sensor.template]: invalid template (**TemplateAssertionError: No filter named 'expand**'.) for dictionary value @ data['sensors']['demo2']['value_template']. Got "{{ states.light | selectattr('attributes.entity_id', 'defined')\n | expand | list | count }}". (See ?, line ?).
In contrast, Check Configuration reports no error when `expand` is used as function or filter when the Template Sensor is configured using _modern_ format. HOWEVER, after reloading Template Entities, an error is then reported in the Log:
> Invalid config for [**template**]: invalid template (**TemplateAssertionError: No filter named 'expand**'.) for dictionary value @ data['sensor'][1]['state']. Got "{{ states.light | selectattr('attributes.entity_id', 'defined')\n | expand | list | count }}". (See /config/templates/sensor.yaml, line 0).
It's unclear why the Template Editor accepts `expand` used as a filter but not when it's employed in a Template Sensor (legacy or modern).
### What version of Home Assistant Core has the issue?
2022.8.7
### What was the last working version of Home Assistant Core?
unknown
### What type of installation are you running?
Home Assistant Supervised
### Integration causing the issue
Templating
### Link to integration documentation on our website
https://www.home-assistant.io/docs/configuration/templating/#working-with-groups
### Diagnostics information
_No response_
### Example YAML snippet
```yaml
##
## Legacy format (Demo1 passes Check Configuration, Demo2 fails)
##
sensor:
- platform: template
sensors:
# This passes Check Configuration
demo1:
friendly_name: "Demo1"
value_template: >-
{{ expand(states.light | selectattr('attributes.entity_id', 'defined'))
| list | count }}
# This fails Check Configuration
demo2:
friendly_name: "Demo2"
value_template: >-
{{ states.light | selectattr('attributes.entity_id', 'defined')
| expand | list | count }}
##
## Modern format (Demo3 and Demo4 pass Check Configuration but Demo4 ultimately fails after reloading Template Entities)
##
template:
- sensor:
- name: "Demo3"
state: >-
{{ expand(states.light | selectattr('attributes.entity_id', 'defined'))
| list | count }}
- name: "Demo4"
state: >-
{{ states.light | selectattr('attributes.entity_id', 'defined')
| expand | list | count }}
```
### Anything in the logs that might be useful for us?
```txt
Error is reported by Check Configuration (posted above) when expand is employed in a legacy Template Sensor.
When employed in a modern Template Sensor, there's no error in Check Configuration but there's an error in the Log after Template Entities are reloaded (posted above).
```
### Additional information
In the Template Editor, `expand` can be used as a function or filter without causing an error.
#### Example 1
![Screenshot from 2022-09-02 12-10-45](https://user-images.githubusercontent.com/44732909/188195720-dcfe53f1-a532-4d4f-aa65-e2e0c09fbf96.png)
#### Example 2
![image](https://user-images.githubusercontent.com/44732909/188210168-000d76f8-f8c7-464f-a0aa-7d2e6f534354.png)
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).
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.
petro
(Petro)
October 2, 2022, 5:55pm
9
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.
5 Likes
frenck
(Franck Nijhof)
October 25, 2022, 5:50pm
10
Merged as part of Home Assistant 2022.11
6 Likes