About jinja filtering

suppose i have

{% set x = states[…].attributes %}

that returns an object of (key,value) items…

is it possible to “remove” an item using jinja (filtering?!) and without creating another object and cycling the given one?!

i think not, but it’s worth to ask…

thanks

No, you have to generate a new collection. Also keep in mind that even if you generated a new collection of kvps, it won’t be able to be set on the entity. It would just be for use later on in your jinja template.

If you’re just filtering it out, real quick and dirty way to do this…

{% set entity_id = 'some.entity' %}
{% set remove = ['a','b'] %}
{{ dict.from_keys(states[entity_id].attributes.items() | rejectattr('0', 'in', remove ) | list) }}
4 Likes

thanks, it does what i was looking for… but…

dict.from_keys and rejectattr(‘0’, ‘in’, remove ) aren’t documented on jinja website…

am i missing something?!

rejectattr is covered in the jinja docs. → here

dict is a python object, you can find the documentation on .from_keys there. → here (apparently from_keys isn’t documented, but fromkeys is.

thanks again… i supposed that dict was jinja, no mentions about python on their website

about rejectattr, i mean the 3 params syntax isn’t on jinja website too…

Yes it is… see my previous links

Keep in mind that this is advanced Jinja. So there’s a bunch of key things you’ll need to learn about to understand it. I.e. what an attribute is on an object, what a test is.

Because you need to understand what a test is and what selectattr is doing with the test. I.e. selectattr accepts a variable number of arguments and those are passed to the test. This is shown in the example with 2 arguments and it’s described in the input types.

Their example is good, it shows a test and how it’s used, and then the same thing with selectattr.

image

thanks, going to reread the docs… i completely missed this syntax