Find maximum attribute difference entity / value / count

Hi everybody! I have several thermostats (climate devices) and I want to find out if and what amount of heating is required to satisfy the thermostat that is off it’s target temperature the most.

So, of all observed climates I’d like to find the one (if any) that is heating and that has the largest difference in their attributes temperature and current_temperature. Alternatively I’d like to know that difference.

I’m struggeling with Jinja. I tried the filter approach, but then I don’t know how to map the entities to the attribute difference or filter on that difference. When using a for loop, I don’t know how to collect the maximum.

Can somebody please help me?

EDIT: Ok, after posting the question I found a solution on my own. But I am still interested in suggestions - especially a nice filter pendant…

{%- set differences = namespace(worst = 0) -%}
{% for thermostat in expand('climate.wohnzimmer_couch', 'climate.wohnzimmer_esstisch', 'climate.comet_dect_1') if thermostat.state == "heat" -%}
  {% set difference = thermostat.attributes.current_temperature - thermostat.attributes.temperature -%}
  {% if differences.worst > difference -%}
     {% set differences.worst = difference -%}
   {% endif -%}
{% endfor -%}
{{ differences.worst }}

Only difference I’d make is your first for statement.

{% for thermostat in states.climate | selectattr('state', 'eq', 'heat') %}

Unless you only care about a select few, this one will just give you all thermostats that are on…should you add more or change their name in the future, this would still work.

1 Like