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.
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)
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.
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.
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:
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)