Numeric sort instead of string sort

I have a group of sensors, holding power values. When I expand the group and use the sort filter, the group is sorted with the power values treated as strings.

Example:

{% for panel in expand ('sensor.panel_power') |
    sort (attribute="state", reverse=true) -%}

    {{ states (panel.entity_id) }}
{% endfor %}

The result is:

98.54
77.43
72.09
51
49.56
47.92
45.55
45.55
44.60
43.06
41.60
264.66
259.61
257
256.55
255.78
254.57
254.39
251.61
251.22
174.62
139.37
118.47
109.44
104.4

I want the sort to be done according the values. Does somebody know a way? Any hint welcome - thanks!

{{ expand('sensor.panel_power')
  | map(attribute='state')
  | map('float', 0)
  | sort(reverse=true)
  | join('\n')
}}

Thank you - great stuff!

My full template looks like this:

{% for panel in expand ('sensor.pv_panel_power_min') -%}
  {# unsorted output - I want it sorted by states (panel.entity_id) #}
  {{ state_attr(panel.entity_id, 'friendly_name') }}: {{ states (panel.entity_id) }}
{% endfor %}

By mapping to the attribute “state”, I am loosing the reference to the entity_id. Is there a way to use the filters above to the expand statement and still be able to access the entity_id?

Is sensor.pv_panel_power_min a Sensor Group Helper?

Please post a screenshot of the details of sensor.pv_panel_power_min from Developer Tools > States so I can see the information it contains.

sensor:
  - platform: group
    type: min
    name: pv_panel_power_min
    ignore_non_numeric: true
    unit_of_measurement: W
    device_class: power
    state_class: measurement
    entities:
      - sensor.power_1_1_1
      - sensor.power_1_1_2
      - sensor.power_1_1_3
      - sensor.power....

Copy-paste the following template into the Template Editor and let me know if it produces the desired result:

{% set ns = namespace(s=[]) %}
{% for x in expand('sensor.pv_panel_power_min') %}
  {% set ns.s = ns.s + [(x.name, x.state|float(0))] %}
{% endfor %}
{% for k, v in dict.from_keys(ns.s)|dictsort(false, 'value', reverse=true) -%}
{{ k }}: {{ v }}
{% endfor %}
1 Like

This is fantastic! Thank you!

Since all power sensors are currently 0, I tested with a similar sensor group, that counts energy. The result is perfect! All values sorted from highest to lowest!

pv_panel_energy_daily_21_lifetime: 0.62075
pv_panel_energy_daily_22_lifetime: 0.61
pv_panel_energy_daily_24_lifetime: 0.608
pv_panel_energy_daily_23_lifetime: 0.6045
pv_panel_energy_daily_20_lifetime: 0.6035
pv_panel_energy_daily_17_lifetime: 0.6035
pv_panel_energy_daily_25_lifetime: 0.60225
pv_panel_energy_daily_18_lifetime: 0.59975
pv_panel_energy_daily_19_lifetime: 0.5985
pv_panel_energy_daily_14_lifetime: 0.566
pv_panel_energy_daily_13_lifetime: 0.555
pv_panel_energy_daily_11_lifetime: 0.5505
pv_panel_energy_daily_10_lifetime: 0.54675
pv_panel_energy_daily_15_lifetime: 0.54625
pv_panel_energy_daily_06_lifetime: 0.54375
pv_panel_energy_daily_16_lifetime: 0.54275
pv_panel_energy_daily_01_lifetime: 0.53875
pv_panel_energy_daily_09_lifetime: 0.53675
pv_panel_energy_daily_02_lifetime: 0.5355
pv_panel_energy_daily_12_lifetime: 0.53475
pv_panel_energy_daily_07_lifetime: 0.53375
pv_panel_energy_daily_08_lifetime: 0.53
pv_panel_energy_daily_03_lifetime: 0.5225
pv_panel_energy_daily_05_lifetime: 0.51275
pv_panel_energy_daily_04_lifetime: 0.466
1 Like