I found this automation from someone. I can read what it does. I am curious to find all the possibilities of the selectattr and rejectattr filters; all those attributes side from device_class or unit_of_ measuring.
I don’t know that there is a single source that lists “all possibilities”.
You can find a number of tests in the Jinja Template Builder docs, but Home Assistant also includes some custom tests and functions which can be found in the Templating docs.
I found the tests and functions in the documentation. I was pointing to the things like attributes.device_class, attribute=‘state’, etc.
I somehow can’t seem to wrap my head around all possibilities to select or filter within home assistant devices/entities.
So, on a ELI5-level, is there a list of items I can ask in the first portion, second and third portion of the line below. selectattr('first', 'second', 'third')
I’ve seen for second, eq (equal), but also search and match. And can I use ‘==’? It seems that there are more roads leading to Rome with jinja templating.
The possibilities are endless. There is no way to “find all possibilities” or for anyone to explain them. The only limitations are your imagination and programming skills.
device_class is the name of one of the entity’s attributes, if the entity has it.
Go to Developer Tools → States, select an entity, and its attributes will be displayed at the top of the page.
If you are using a browser on a PC, the list of entities displays a third column showing each entity’s attributes (the same list on a phone’s narrow screen doesn’t display the third column).
The first argument is like an address for the attribute/key
that is of interest. That can be either the name of one of the properties of the state object like entity_id or a sub-property like attributes.device_class.
The second argument is the test. I linked those previously. Be aware that some tests can be expressed multiple ways such as:
eq, ==
gt, >
le, <=
ne, !=
Not all uses of selectattr() will require a third argument, this is based on the type of test being used. Test that are comparisons usually require a third argument as the comparand.
As Taras noted above, you can find an entity’s available attributes in the Dev Tool > States tab. You can also list them using the state object method in the Template editor:
To print the state object with its list of properties:
{{ states.sensor.example }}
To list the subcomponents of the property "attributes":
{{ states.sensor.example.attributes }}
Though the above explanation focuses on using selectattr() with state objects, it can be used with user-created dictionaries as well.