Templating: How do I get the identify the sensor with the largest/smallest value (from multiple sensors)?

I have multiple sensors in my home reporting the power consumption for diverse appliances.
I would like to see in the fronted which appliance consumes the most (in an instant of time).
I create a template showing all the sensors I am interested in. I would like to sort them from large to small and retrieve only the first/last values, but the sort does not work properly.
This is what I have up to now and the result from the template.
Am I doing something wrong?
Thanks!

Template

{% for state in states.sensor  | sort(attribute="state") | reverse  %}
{%- if state.attributes.unit_of_measurement == 'W' %}  
{{ state.attributes.friendly_name }}: {{state.state}}  {{- state.attributes.unit_of_measurement}} 
{% endif -%}
{%- endfor -%}

Result

Desktop: 73.7W   
Boiler: 6.5W   
TV: 4.1W   
Router area: 27.1W   
Other: 25.04W   
Washing machine: 1.8W   
Oven: 0.9W   
Fridge: 0.9W   
Dishwasher: 0.9W   
Laptop: 0.5W

did you figure this out?

same problem here with sort being in incorrect order.

Yes, and I am already past it. :grinning:
Use this if you want something fancier:

If you just want the template, I managed with a lot of sets and ifs:

        max_consumer:
            value_template: '{%- set a = states.sensor.oven_power.state|float -%}{%- set b = states.sensor.boiler_power.state|float  -%}{%- set c= states.sensor.fan_power.state|float  -%}{%- set d = states.sensor.fridge_power.state|float  -%}{%- set e = states.sensor.laptop_power.state|float  -%}{%- set f = states.sensor.pc_power.state|float  -%}{%- set g = states.sensor.router_area_power.state|float  -%}{%- set h = states.sensor.tv_power.state|float  -%}{%- set i = states.sensor.washer_power.state|float  -%}{%- set j = states.sensor.power_other.state|float  -%}{%- set maximum = (a,b,c,d,e,f,g,h,i,j)|max -%}{% if a == maximum  %}Oven · {{ maximum }} W{% elif b == maximum  %}Boiler · {{ maximum }} W{% elif c == maximum  %}Fan · {{ maximum }} W{% elif d == maximum  %}Fridge · {{ maximum }} W{% elif e == maximum  %}Laptop - {{ maximum }} W{% elif f == maximum  %}Desktop · {{ maximum }} W{% elif g == maximum  %}Router area · {{ maximum }} W{% elif h == maximum  %}TV · {{ maximum }} W{% elif i == maximum  %}Washer · {{ maximum }} W{% elif j == maximum  %}Other · {{ maximum }} W {% else %}n/a{% endif %} '
            friendly_name: 'Highest power'

The only problem with that is float returns 0.0 if there is an error, which I can’t tell the difference between 0.0° C and bad data.

I’m using a conditional loop with filters to get a good set of data, but I can’t get it to sort in order.

{% for temps in [states.sensor.temp201aa, states.sensor.temp158bb,states.sensor.s1_temp, states.sensor.s2_temp, states.sensor.s4_temp,states.sensor.s5_temp]|reject('none')|rejectattr('state','equalto','unknown')|rejectattr('state','equalto','unavailable')|list|sort(attribute='state') if ((as_timestamp(now())-as_timestamp(temps.last_changed)) < 3000 ) %}
  {%if (loop.first) %}
    {{temps.state}}
  {% endif %}
{% endfor %}

should give me the lowest temperature reading ignoring temperatures older than 3000 seconds, however it isn’t sorting correctly (presumably because it is sorting as string not float).

1 Like

I don’t know what kind of application you need so it’s hard to device.
Why don’t you use one of these?