Compare two temperatures then do something

I’m having trouble with a simple sounding automation and have looked over several relevant post here but can’t get anything to work.

I want to check if the temperature in one room 1 is greater than the temperature in room 2, and if so, turn on an extractor.

I’ve tried a trigger:

platform: template
value_template: >-
  {{ states('sensor.temp_sensor1') | float >
  states('sensor.temp_sensor2') | float }}

And that didn’t work. I also tried it with and without casting to float and also with different formatting:

{{ (states.sensor.temp_sensor1.state | float ) > (states.sensor.temp_sensor2.state | float) }}

I have seen posts suggesting that its better to base the trigger off a change in state of a binary_sensor, so I made one:

binary_sensor:
  - platform: template
    sensors:
      room_temp_low:
        friendly_name: Room Temperature Low
        value_template: "{{ (states('sensor.temp_sensor1') | float > states('sensor.temp_sensor2') | float) }}"

The binary_sensor never updates. I’m sure I am missing up something really simple, but I can’t see to find what.

The template for your binary_sensor seems correct

You could try this :

binary_sensor:
  - platform: template
    sensors:
      room_temp_low:
        entity_id:
          - sensor.temp_sensor1
          - sensor.temp_sensor2
        friendly_name: Room Temperature Low
        value_template: "{{ (states('sensor.temp_sensor1') | float > states('sensor.temp_sensor2') | float) }}"

1 Like

There’s nothing wrong with either your template trigger our your binary sensor (and you don’t need the extra set of parentheses.)

The problem is more than likely neither sensor.temp_sensor1 nor sensor.temp_sensor2 are changing. A template trigger only gets evaluated, and potentially fires, when one of the referenced entities changes state. Same thing goes for a template binary sensor – a referenced entity has to change state for the binary sensor to update.

If you want the automation to trigger when HA starts, even before either of the sensors updates, then you could do this:

- trigger:
  - platform: state
    entity_id:
    - sensor.temp_sensor1
    - sensor.temp_sensor2
  - platform: homeassistant
    event: start
  condition:
  - condition: template
    value_template: >
      {{ states('sensor.temp_sensor1') | float >
         states('sensor.temp_sensor2') | float }}
  action:
  - ...

The automation will trigger when either of the sensors changes state, or when HA has started. When any of these happen the condition will then be checked, and if true, the action(s) will run.

Just be aware that if either of the sensors does not have a valid numeric state when the condition is evaluated, the corresponding float filter will return zero. To guard against the actions running when one of the sensors isn’t valid yet, you could change the template to use the float filter’s default parameter:

  - condition: template
    value_template: >
      {{ states('sensor.temp_sensor1') | float(-500) >
         states('sensor.temp_sensor2') | float(500) }}
2 Likes

Thanks heaps. The comments here have got me almost there.
There were multiple issues and they were way more stupid than one would reasonably expect. The HA install was on Docker. Somehow a restart had taken the config from /user/drive/folder to /user/drive/folder/config - leaving config that looked identical in a folder and in its parent. I was only updating files in the parent folder, so it wasn’t being used. Knowing the config was good led to me finding the issue.

The other issue was a duplicate entry from ESPHome, which is quite hard to fix but I eventually found a duplicate config for a device in .storage.
Thanks again - I have learnt a few things here.

Hi!

I’m new to HA, also new to this type of script writing (prefer C++ :slight_smile: so please be patient with me!

So, my problem is almost the same what this topic is about. I would like to control a fan (relay) that can heat or cool down a room depending on 3 temperature: inner, outer and set temp. Hardware side is working perfectly, HA can get temperatures and control relay, the only problem is the automation/script. I have read several topics, tried many things but none of them worked. I can’t even save my automation, throws error. I am using newest HA OS on an rpi.
My full automation code is the following:

alias: Kamra temperálás
description: ''
mode: single
trigger:
  - platform: time_pattern
    minutes: '5'

action:
      - choose:
# IF heating
          conditions:  
              - condition: template
                value_template: {{ (states.kamra_inner | float) < (states.kamra_outer | float)}}
              - condition: template
                value_template: {{ (states.kamra_inner | float) < (states.input_number.homerseklet_setpoint | float)}}
            sequence:
              - service: switch.turn_on
                data: {}
                target:
                   entity_id: switch.kamra_relay
         
# ELIF cooling
          conditions:
              - condition: template
                value_template: {{ (states.kamra_inner | float) > (states.kamra_outer | float)}}
              - condition: template
                value_template: {{ (states.kamra_inner | float) > (states.input_number.homerseklet_setpoint | float)}}
            sequence:
              - service: switch.turn_on
                data: {}
                target:
                   entity_id: switch.kamra_relay
# ELSE switch off
        default:
          - service: switch.turn_off
            data: {}
            target:
              entity_id: switch.kamra_relay

Now the error message is “Integration ‘’ not found”.
Earlier the error message were sth like this: " something needed data[action]"

Thanks in advance
David