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 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.
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.
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.
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’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.
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.
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.