Help finding the lowest attribute for a set of entities

Hi there.
Im trying to figure out how to get the lowest temperature value for my generic thermostat climate entities that are in the state ‘cool’ and then set that temperature to my main thermostat.

I think this is the code to get my list

(states.climate
     | selectattr('state', 'eq', 'cool')
     | rejectattr('attributes.friendly_name', 'eq', 'MHI Ducted')
     | list)

But then unsure how to sort it and then set to the MHI ducted entity.
Can someone assist?

I will assume that climate.mhi_ducted is the entity_id of the climate entity whose friendly_name is “MHI Ducted”. If it’s not, change the entity_id in the following example.

In the actions section of your automation:

actions:
  - variables:
      therm: climate.mhi_ducted
      temps: >
        {{ states.climate
          | selectattr('state', 'eq', 'cool')
          | rejectattr('entity_id', 'eq', therm)
          | map(attribute='attributes.temperature')
          | list }}
  - if: "{{ temps | count > 0 }}"
    then:
      - action: climate.set_temperature
        target:
          entity_id: "{{ therm }}"
        data:
          temperature: "{{ temps | min }}"
          hvac_mode: cool

Reference

Script variables
climate.set_temperature


EDIT

Correction. Made a transcription error when copying example from Template Editor to automation. There’s no need to define a Jinja2 variable named temps. All that’s needed is a script variable named temps.

Ok… So its setting the right value when i put the the following in template editor

        {% set temps = states.climate
          | selectattr('state', 'eq', 'cool')
          | rejectattr('entity_id', 'eq', therm)
          | map(attribute='attributes.temperature')
          | list %} 
          {{(temps | min ) | float(2) }}

However when i run the automation it fails

expected float for dictionary value @ data['temperature']

Here is my Automation YAML

alias: Set Main AC to the lowest temp set in rooms
description: ""
triggers:
  - trigger: time_pattern
    seconds: /30
conditions:
  - condition: state
    entity_id: climate.mhi_ducted
    state: cool
actions:
  - variables:
      therm: climate.mhi_ducted
      temps: |
        {% set temps = states.climate
          | selectattr('state', 'eq', 'cool')
          | rejectattr('entity_id', 'eq', therm)
          | map(attribute='attributes.temperature')
          | list %}
  - action: climate.set_temperature
    target:
      entity_id: climate.mhi_ducted
    data:
      temperature: "{{(temps | min ) | float(2) }}"
mode: single

I have also tried {{(temps | min ) | float(2) }} without quotes to no avail.

123 pointed you in the right direction here and solved your problem.

I think there is a small, very technical semantic issue that I fail with all the time… You probably need (the final line is what you’re missing):

actions:
  - variables:
      therm: climate.mhi_ducted
      temps: |
        {% set temps = states.climate
          | selectattr('state', 'eq', 'cool')
          | rejectattr('entity_id', 'eq', therm)
          | map(attribute='attributes.temperature')
          | list %}

        {{ temps }}
      # remainder of the code...

EDIT: I added the comment at the end of the snippet above. Obviously, that’s not what was missing; I think it’s the {{ temps }} that needs to be added.

Thanks. That did the trick!

Simply change this:

temps:
  {% set temps = states.climate
    | selectattr('state', 'eq', 'cool')
    | rejectattr('entity_id', 'eq', therm)
    | map(attribute='attributes.temperature')
    | list %}

To this:

temps:
  {{ states.climate
    | selectattr('state', 'eq', 'cool')
    | rejectattr('entity_id', 'eq', therm)
    | map(attribute='attributes.temperature')
    | list }}

The only reason I had set temps = was because, before posting the automation, I had tested the template in the Template Editor and needed to define a Jinja2 variable named temps.

In the automation I define a script variable (something you cannot do in the Template Editor) and so there’s no need for its template to contain a Jinja2 variable bearing the same name.

Unfortunately, in my haste to provide you with a solution, I overlooked to remove the Jinja2 variable from the template.

I have corrected my original example.

What d921 suggested makes it work but in a redundant way (it reports the value of an unnecessary variable). The template itself can report its own value. Simply remove the Jinja2 variable definition as shown above.

It works, though.

It’s not a practice I would recommend to others so that’s why I explained how to correct it in my example.

For example, if I had to express the number one, I would suggest simply 1 and not 1+1-1.

Anyways, the complete solution also involved the climate.set_temperature action and it was important to execute it only if there was a minimum temperature available so it required wrapping it in an if.

Yessir. But you suggested

      temps: |
        {% set temps = states.climate
          | selectattr('state', 'eq', 'cool')
          | rejectattr('entity_id', 'eq', therm)
          | map(attribute='attributes.temperature')
          | list %}

And that was wrong.

I respect you @123 greatly, but I do not understand why you chose to make this last post. You made a small omission in your suggestion–otherwise correct–about how to fix a problem, and I made it a point to explain in my post that you did the heavy lifting.

Given that I had created the template, I felt responsible for fixing it the way I believe to be best.

Thanks–that’s consistent with why I think that you’re awesome, @123 .

One more thing. The latest example you posted doesn’t have the if statement that exists in my example. That makes it vulnerable to an error condition.

Should it ever occur that all your thermostats are not in cool mode, the value of the temps variable will be an empty list. The min value of an empty list is no value at all. The climate.set_temperature action expects a value.

You also changed the template by adding a final float(2). Not only is it unnecessary, it will cause an error if temps is an empty list. You can prove it for yourself by copy-pasting the following template into the Template Editor.

{% set temps = [] %}
{{ (temps | min) | float(2) }}

I recommend you consider using the example I posted above as-is with the if statement. It doesn’t bear the Solution tag but it is the robust solution for the requirements you stated in your initial post.

get the lowest temperature value for my generic thermostat climate entities that are in the state ‘cool’ and then set that temperature to my main thermostat.