Set value of template sensor based of comparison between two other values

Hello all,

I am trying to setup a template sensor that I would like to use in the with the climate integration. The code I have is as follows but clearly I am doing something wrong with the syntax because instead of a temperature value I get somthing else entirely:

- platform: template
  sensors:
    room_target_temp:
      friendly_name: "House Target Temperature"
      unit_of_measurement: 'degrees'
      value_template: >-
        {% set avg_h_tmp = states('sensor.average_house_temp') | float %}
        {% set hall_tmp = states('sensor.roomtemp_hallway') | float %}
        {% if avg_h_tmp <= hall_tmp %}
          states('sensor.average_house_temp')
        {% else %}
          states('sensor.roomtemp_hallway')
        {% endif %}

What I get for the newly created sensor.room_target_temp is: states('sensor.average_house_temp'). What I intend to obtain is the value of the sensor.average_house_temp at that instant. How can I achieve this? What is the correct syntax?

Can someone point me in the right direction please?

        {% set avg_h_tmp = states('sensor.average_house_temp') | float %}
        {% set hall_tmp = states('sensor.roomtemp_hallway') | float %}
        {% if avg_h_tmp <= hall_tmp %}
        {{  states('sensor.average_house_temp') }}
        {% else %}
        {{  states('sensor.roomtemp_hallway') }}
        {% endif %}
2 Likes
value_template: >-
        {% set avg_h_tmp = states('sensor.average_house_temp') | float %}
        {% set hall_tmp = states('sensor.roomtemp_hallway') | float %}
        {% if avg_h_tmp <= hall_tmp %}
          {{ states('sensor.average_house_temp') }}
        {% else %}
          {{ states('sensor.roomtemp_hallway') }}
        {% endif %}

Edit: too slow

2 Likes

thank you! it worked perfectly!

1 Like

thank you for your help! it worked perfectly. I had to accept the other answer because it cam first!

1 Like