Template for comparing temperature sensor values to find the lowest or highest returns "unknown"

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.

thanks for your help

You could do this a lot easier with the min-max helper:

Create a min and a max helper of all your sensors b to g.

Then your template becomes:

- sensor:
    - unique_id: critic_room_temp
      device_class: temperature
      name: "critic room temp"
      unit_of_measurement: "°C"
      state: >
         {% if states('sensor.casa_temperatura_realmente_percepita')|float(0) <= 22 %}
           {{ states('sensor.min_sensor_here') }}
         {% else %}
           {{ states('sensor.max_sensor_here') }}
         {% endif %}

You don’t need to specify an icon, that comes from the device class (mdi:thermometer).

1 Like

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)

Yes, that is what my solution does.

i didn’t explain,my bad:, i’m sorry: I want to understand why by using variables in a template sensor, I can’t do it:

I did the same about a comparison between battery states of different sensors, and it works smoothly.

so, why not in this case?

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 }}

Tip

This:

float(0) | round(1)

can be replaced by this:

round(1, default=0)

I already offered them a better solution. They’re not interested. They want to know why their Rube Goldberg machine does not work.

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

indeed i was having the same doubt…

can the output of a temperature sensor be a string instead of a number?

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?

Hi,

found the problem, while creating the sensor, for an -unknown- reason, it reated 2 entities with the same name, and this gave a conflict as a result.

days of work thrown out of the window.

at least i found the reason.

thanks everybody!

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

Hi,

yes, I learnt this , when I went to check the entity in the entities tab I found that there were 4 of them renamed *name , _1 , _2 , _3

I just deleted the numbered ones and now it works

can’t see what happened ,but it’s ok,now it works :slight_smile:

thank you very much for your help,it made me reason!