Template get climate entity with biggest temperatur difference

Hi

for my electric-floor-heating automation I need a template which returns the entity with the biggest temperature difference between temperature and current_temperature of a list of climate entities.

Maybe 4 out of 6 entities have the attribute hvac_action = heating, but I want to activate the heating zone where there is the biggest temperature difference.
the reason for this is that the 6 contactors in the control panel are interlocked against each other, otherwise the current consumption becomes too large.

I think there must be a better way than my template, or at least a better syntax: :see_no_evil:

{%- set return = namespace(return=false) -%}
{%- set ns = namespace(big_temp_diff=0) -%}
{%- for clim in ['climate.flur', 'climate.gastezimmer', 'climate.schlafzimmer', 'climate.buro', 'climate.badezimmer', 'climate.wohnzimmer_kuche'] | select('is_state', 'heat') | select('is_state_attr', 'hvac_action', 'heating') -%}
    {% set this_temp_diff = state_attr(clim, 'temperature') - state_attr(clim, 'current_temperature') %}
    {% if this_temp_diff > ns.big_temp_diff %}
      {%- set ns.big_temp_diff = this_temp_diff -%}
      {% set return.return = clim %}
    {%- endif -%}
    #debug {{ clim }} = {{ this_temp_diff }} 
{%- endfor %}
Return: {{ return.return }} is the climate entity with the biggest temperature difference with {{ ns.big_temp_diff }} °C

Output in template-editor:

#climate.flur = 1.5
#climate.badezimmer = 6.5
#climate.wohnzimmer_kuche = 4.0
Return: climate.badezimmer is the climate entity with the biggest temperature difference with 6.5 °C
{% set ns = namespace(items=[]) %}
{%- for clim in expand('climate.flur', 'climate.gastezimmer', 'climate.schlafzimmer', 'climate.buro', 'climate.badezimmer', 'climate.wohnzimmer_kuche') | select('is_state', 'heat') | select('is_state_attr', 'hvac_action', 'heating') -%}
  {% set ns.items = ns.items + [ {'diff': state_attr(clim, 'temperature') - state_attr(clim, 'current_temperature'), 'name': clim } ] %}
{% endfor %}
{% set item = (ns.items | sort(attribute='diff'))[-1] %}
{{ item.name }} is the climate entity with the biggest temperature difference with {{ item.diff}} °C

Not really better, just different.

Hi petro.

thx for your reply, but it didn’t work…

In the first line a ) is missing I think. After I change the first line to the following line, I get another error.
{% set ns = namespace(items=[]) %}

AttributeError: ‘TemplateState’ object has no attribute ‘lower’

I don’t know why, but the problem is using expand().

fixed

Not really… I still have to replace expand(…) with […] in your short code.

but no matter how, i now have the entity with the largest temperature difference. how can i continue to work with it in the action of my automation?

Is the action service_template no longer available?