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 %}
{% 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?
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 %}
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!