Help with listing utility meters helpers sorted by numeric state value

Hi all,

I’ve done about 30 x daily, weekly, monthly utility meter helpers for all the entities that have energy values.

I would like to send each end of day, week and month, a notify through Telegram with the list of these helpers name, sorted by the entity with the highest consumption to the less consumption, without listing all of them but checking the entity_id value that starts with “sensor.day_energy”, “sensor.week_energy” and so on.

The state.name[20:] is because I don’t want to see the first 20 chars.

I’ve done the template for the daily and everything worked correctly :smiley:

{% for state in states.sensor | selectattr('entity_id', 'match', '.*sensor.day_energy_.*') 
| sort(reverse=true, attribute='state')%}
{{state.name[20:]}}: {{state.state | float(default=0.0) | round(2) }} kW/h{% endfor %}

but doing it for the weekly it doesn’t work, because of course the “sort” function it sorts them like strings and not numbers:

{% for state in states.sensor | selectattr('entity_id', 'match', '.*sensor.week_energy_.*') 
| sort(reverse=true, attribute='state')%}
{{state.name[20:]}}: {{state.state | float(default=0.0) | round(2) }} kW/h{% endfor %}

So I will have first a value like 3.23 kWh and the second one is 17.89 kWh because 3 is greater than 1 in string values.

Can someone help me achieve this template?

Thanks! :wink: