Hi everybody here is my template sensor, inside the template file i already use:
- sensor:
- unique_id: critic_room_temp
device_class: temperature
name: "critic room temp"
icon: mdi:thermometer
unit_of_measurement: "°C"
state: >-
{% set a=( states('sensor.casa_temperatura_realmente_percepita')|float(0) )|round(1) %}
{% set b=( states('sensor.terrazzo_termometro_temperatura')|float(0) )|round(1) %}
{% set c=( state_attr('climate.camera_vittorio_termosifone_termostato','current_temperature')|float(0) )|round(1) %}
{% set d=( states('sensor.camera_padronale_termometro_temperatura')|float(0) )|round(1)%}
{% set e=( state_attr('climate.salotto_pranzo_termosifone_termostato','current_temperature')|float(0) )|round(1) %}
{% set f=( states('sensor.cucina_termometro_temperatura')|float(0) )|round(1) %}
{% set g=( states('sensor.bagno_bianco_termometro_temperatura')|float(0) )|round(1) %}
{% if a <=22 %}
{% if b <= c and b <= d and b <= e and b <= f and b <= g %}
{{ b }}
{% elif c <= b and c <= d and c <= e and c <= f and c <= g %}
{{ c }}
{% elif d <= b and d <= c and d <= e and d <= f and d <= g %}
{{ d }}
{% elif e <= b and e <= c and e <= d and e <= f and e <= g %}
{{ e }}
{% elif f <= b and f <= c and f <= d and f <= e and f <= g %}
{{ f }}
{% elif g <= b and g <= c and g <= d and g <= e and g <= f %}
{{ g }}
{% else %}
{{ a }}
{% endif %}
{% elif a >22 %}
{% if b >= c and b >= d and b >= e and b >= f and b >= g %}
{{ b }}
{% elif c >= b and c >= d and c >= e and c >= f and c >= g %}
{{ c }}
{% elif d >= b and d >= c and d >= e and d >= f and d >= g %}
{{ d }}
{% elif e >= b and e >= c and e >= d and e >= f and e >= g %}
{{ e }}
{% elif f >= b and f >= c and f >= d and f >= e and f >= g %}
{{ f }}
{% elif g >= b and g >= c and g >= d and g >= e and g >= f %}
{{ g }}
{% else %}
{{ a }}
{% endif %}
{% endif %}
as you can see, my purpose is returning the highest temperature when the outdoor temperature is higher than 22 and the lowest temperature when the outdoor temperature is lower than 22 or equal.
i’m trying to solve this problem for days but I can’t do it.
Hi, thanks for your answer, but i think you missed the problem : it’s not about min and max ( I read a couple of threads about it ,here) , I want to show on screen the temperature of the corresponding sensor in the house, according to the different outdoor temperature: if it’s cold(lower than 22°, I want to show the coldest room) if it’s warmer than 22 , if it’s hot, i want to show the hottest temperature(so the hottest room)
Probably because you did not indent your if tests correctly.
state: >
{% set a = states('sensor.casa_temperatura_realmente_percepita') | float(0) | round(1) %}
{% set b = states('sensor.terrazzo_termometro_temperatura') | float(0) | round(1) %}
{% set c = state_attr('climate.camera_vittorio_termosifone_termostato','current_temperature') | float(0) | round(1) %}
{% set d = states('sensor.camera_padronale_termometro_temperatura') | float(0) | round(1) %}
{% set e = state_attr('climate.salotto_pranzo_termosifone_termostato','current_temperature') | float(0) | round(1) %}
{% set f = states('sensor.cucina_termometro_temperatura')|float(0) | round(1) %}
{% set g = states('sensor.bagno_bianco_termometro_temperatura') | float(0) | round(1) %}
{% if a <= 22 %}
{% if b <= c and b <= d and b <= e and b <= f and b <= g %}
{{ b }}
{% elif c <= b and c <= d and c <= e and c <= f and c <= g %}
{{ c }}
{% elif d <= b and d <= c and d <= e and d <= f and d <= g %}
{{ d }}
{% elif e <= b and e <= c and e <= d and e <= f and e <= g %}
{{ e }}
{% elif f <= b and f <= c and f <= d and f <= e and f <= g %}
{{ f }}
{% elif g <= b and g <= c and g <= d and g <= e and g <= f %}
{{ g }}
{% else %}
{{ a }}
{% endif %}
{% elif a > 22 %}
{% if b >= c and b >= d and b >= e and b >= f and b >= g %}
{{ b }}
{% elif c >= b and c >= d and c >= e and c >= f and c >= g %}
{{ c }}
{% elif d >= b and d >= c and d >= e and d >= f and d >= g %}
{{ d }}
{% elif e >= b and e >= c and e >= d and e >= f and e >= g %}
{{ e }}
{% elif f >= b and f >= c and f >= d and f >= e and f >= g %}
{{ f }}
{% elif g >= b and g >= c and g >= d and g >= e and g >= f %}
{{ g }}
{% else %}
{{ a }}
{% endif %}
{% endif %}
Your spacing was also all over the place and you had a whole heap of extraneous parentheses, but that’s just style and would not have prevented it from working.
I wouldn’t bother trying to figure out why that template fails to work correctly. It’s needlessly long. This does the same thing using far less code.
- sensor:
- unique_id: critic_room_temp
device_class: temperature
name: "critic room temp"
unit_of_measurement: "°C"
state: >-
{% set a = states('sensor.casa_temperatura_realmente_percepita') | round(1, default=0) %}
{% set b = states('sensor.terrazzo_termometro_temperatura') | round(1, default=0) %}
{% set c = state_attr('climate.camera_vittorio_termosifone_termostato','current_temperature') | round(1, default=0) %}
{% set d = states('sensor.camera_padronale_termometro_temperatura') | round(1, default=0) %}
{% set e = state_attr('climate.salotto_pranzo_termosifone_termostato','current_temperature') | round(1, default=0) %}
{% set f = states('sensor.cucina_termometro_temperatura') | round(1, default=0) %}
{% set g = states('sensor.bagno_bianco_termometro_temperatura') | round(1, default=0) %}
{% set x = [a, b, c, d, e, f, g] %}
{{ x | min if a <= 22 else x | max }}
The curious thing is that if, for testing purposes, I substitute integers for a, b, c, etc the template does produce the correct result. So I’m not sure what the OP means by “why by using variables in a template sensor, I can’t do it”.
Yes, the sensor’s value can be a non-numeric string such as unavailable or unknown. However, your template’s float(0) will convert a non-numeric string to 0.
{% set a=(states('sensor.casa_temperatura_realmente_percepita')|float(0))|round(1) %}
Is this the issue you are observing? That sometimes the minimum reported value is 0?
So you’re saying you had two entities with the same name and you were looking at the wrong one?
For future reference, Home Assistant will only create the Template Sensors that you define. They can have the same “friendly name”, just not the same entity_id (Home Assistant will not allow multiple identical entity_ids).