I have a more complicated template problem: I get the signal strengths of my smartphone for different locations via ESP Home BLE sensors. I want to select the value with the lowest attenuation from these values. After a bit of trial and error, I’m almost at the solution:
Filter the relevant sensors via unit_of_measurement and state
Sort the sensors via sort
In the If loop
only consider the sensors that reported data in the last 32 seconds
Only consider values greater than -100
I am now missing the option of simply selecting the first value that represents the location of my smartphone. Unfortunately, I get a string from the if loop to which I can not apply a “|first” filter.
I currently list all results with a colon for testing purposes as one can see in the code below. I think the if loop is the problem, but I’m stuck now. Can someone help me, please?
I would also advice to apply a label to all these sensors and use that, instead of iterating over all sensors. You also won’t need a lot of these selectattr parts then, as you already know you have the right entities
With both suggestions applied @Didgeridrew’s template would become
thank you very much - I am overwhelmed by the helpfulness and awesomeness of our community!
The solution worked almost straight away - I just had to change the greater than/less than sign because the numbers are negative: | select('>;', -100) Actually, I can just omit this filter since the state is properly mapped as an integer.
By the way: Is there any good tutorial for advanced jinja templating?
Edit: So that my question and the underlying project can be of use to others, here is the corresponding ESP code. With my solution, I achieve quite reliable detection of the location (assuming clever placement of the ESP boards in suitable corners of the rooms, sometimes even several boards in one room). The system reacts to a change of location within 1-3 seconds.
Not really… the closest thing to an organized tutorial is Jinja for Ninjas . There’s also a Jinja Cheatsheet you can download. Unfortunately, both of them are showing their age. Since the HA devs and community are regularly adding template functions, there are often better/easier ways to do some of the examples shown in either source. For the most up-to-date list of available functions, the best source is still the Home Assistant Templating docs.
The forum community has been working on creating and indexing useful examples and explanations which can be found in the Cookbook Index.