I’m trying to get a list of Sonoff sensors with battery level lower than a threshold.
Battery level is stored in sensor entities like: sensor.sonoff_a48430ce1b_battery
.
If I create a template like this:
{{states.sensor | selectattr('entity_id', 'search', 'sensor.sonoff')
| selectattr('entity_id', 'search', 'battery')
| map(attribute='state') | list}}
I obtain an output like this:
['unavailable', '8', '100', '100', '100', '99', '100', '100', '74', '81', '100', '56', '100', '100', '81', '100', '73', '100', '83', '75', '85', '82', '100', '80', '100', '100', '76', '100', 'unavailable', '65', '70', '68', '71', 'unavailable', 'unavailable', '76', '81', 'unavailable', '100', '100', '100', '80']
What I’d like is to filter and only obtain the sensor with battery level below some fixed threshold.
I’ve tried something like:
{{states.sensor | selectattr('entity_id', 'search', 'sensor.sonoff')
| selectattr('entity_id', 'search', 'battery')
| selectattr('state', '<=', '50')
| map(attribute='state') | list}}
But it doesn’t work as intended. The oputput is:
['100', '100', '100', '100', '100', '100', '100', '100', '100', '100', '100', '100', '100', '100', '100', '100', '100']
I suppose that the problem is that the comparison is made between strings
and not int
s.
How can I make the comparison between int
s?
Thank you!