Jinja / Template Sensor Question

I spent ages trying to do this without a generator/for loop, as I wanted to have the sensor as “on/off” and have an array/list attribute with the names of the sensors.

As you say, select/rejectattr treats the state as a string, and there doesn’t appear to be any test in jinja/HA templating that will do the lt/gt and cast the state to an integer. I think this would need either custom tests in HA or a custom filter that allows us to treat attributes as numbers instead of strings

I don’t think that 2nd one does work properly as you get different results. The states are being compared as strings instead of numbers so it’s not returning a correct list.

That’s correct. I’m investigating adding this as we speak.

2 Likes

As mentioned, the template I suggested provides different results due to the string comparison.

What is the battery_level of one of the entities that it fails to include? For example, deck_temp_sensor_battery and phone_battery_jeremy? I’m curious to know what failed the string comparison. For example, ‘8’ fails to be less than ‘30’ in a string comparison.

deck_temp_sensor_battery is ‘unavailable’ (I have issues with that sensor)
phone_battery_jeremy is ‘unknown’ (I am going through a re-install currently, so that is probably messed up at the moment)

Yup, that will definitely goof it up.

Anyway, just curious; a string comparison is simply unreliable for this application.

It’s possible to use map('int') to convert values but you can only pass it values, not objects, so you lose all other information about the entity like it’s entity_id.

I’m hoping Petro can enhance the templating engine so that we can be more selective in how a filter is applied.

1 Like

I know this is a long dead thread, but I’m just wondering if anything ever came of this. I’m running into the same issue now, but am not seeing any mentions of new solutions.

what are you trying to do, many template changes have been made

Sorry, thought the thread would provide enough context.

I’m wondering if there are any new ways to cast values in something like selectattr() before the comparison is done, as you’ve mentioned you were looking into above. More specifically, I want to do something like the following template, but ensure that the comparison is treating all values as ints, not strings:

{{ states.sensor | selectattr('entity_id', 'search', 'shelly') | selectattr('state', 'le', '20') | join(' and ', attribute='attributes.friendly_name') }}

I did not add them, at the time it wasn’t possible to add tests into the jinja environment. I’ll see what it will take to get these added.

2 Likes

Awesome, I’ll keep my eye out. Thanks!

I’m also struggling with something similar: I want to select all battery levels <= treshold

{% for item in states.sensor
| selectattr('attributes.device_class', '==', 'battery')
| selectattr('attributes.unit_of_measurement', '==', '%')
| selectattr('state', '<=', '50') %}

@StevenBrs

You do not need a for loop:

{{ states.sensor
| selectattr('attributes.device_class', '==', 'battery')
| selectattr('attributes.unit_of_measurement', '==', '%')
| selectattr('state', '<=', '50')
| map(attribute='entity_id')
| list }}

@Didgeridrew This is not working because you do the check on a string in your case while you should do the check on an int imho.

1 Like

You are absolutely right… I should have cast the state to an integer.

{% set sensors = states.sensor | selectattr('entity_id', 'has_value') 
| selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', '==', 'battery') 
| selectattr('attributes.unit_of_measurement', 'defined') | selectattr('attributes.unit_of_measurement', '==', '%') | list %}
{% set low = sensors | map(attribute='state') | map('int') | select('<=', 50) | map('string') | list %}
{{ sensors | selectattr('state', 'in', low) | map(attribute='entity_id') | list }}
7 Likes

Hi Drew,

Have you tried the template you provided ?
I cannot get it to work.

There was a change in how undefined attributes are handled, so now you need to select for entities who have them defined before selecting against their value. In this case only the first section needs to be changed. I will update it in the original post.

Thx for the quick reaction.
I had added the selection for Defined entities already, but am still not able to get this template working (been testing in Developer tools)

Post your template and any error messages that appear in the Template editor tool.

Drew,

I have to apologize !
Testing once more with the above code provided, it does work !
Brilliant.
Many thanks for your kind help.

1 Like