Automation template with offset

Hi, I’m new in HA… can someone tell me how to trigger 2 temperatures (dallas) but with the difference of the 2 temp + an offset Ex. trig relay ON when temp of sensor 1 is 10 degres higher and more then sensor 2… trig relay OFF when temp of sensor 1 is less then 5 degres higher than sensor 2 ?

without knowing much about your system, i’m taking a bunch of guesses. but something like this… note that i’m doing what you litterally asked… it needs to be more than 10 … less than 5. change > to >= and change < to <= if you want…

trigger:
  - platform: template
    value_template: "{{ states('sensor.one') - states('sensor.two') > 10 }}"
    id: "on"
  - platform: template
    value_template: "{{ states('sensor.one') - states('sensor.two') < 5 }}"
    id: "off"
condition: []
action:
  - service: homeassistant.turn_{{trigger.id}}
    target:
      entity_id: switch.your_device

Thank you very much! I will try IT👍

You’re doing arithmetic with the sensor values (then performing a numeric comparison) so you’ll need to convert them to numbers, using int or float, before subtracting them.

1 Like

converting to floats as @123 says…

trigger:
  - platform: template
    value_template: "{{ states('sensor.one') | float - states('sensor.two') | float > 10.0 }}"
    id: "on"
  - platform: template
    value_template: "{{ states('sensor.one') | float - states('sensor.two') | float < 5.0 }}"
    id: "off"
condition: []
action:
  - service: homeassistant.turn_{{trigger.id}}
    target:
      entity_id: switch.your_device

wonderfull !! it works perfectly. Just a slight difference… HA had " >- " and change line after the first "float - "

  • platform: template
    value_template: >-
    {{ states(‘sensor.esphome_web_8be748_temp_tapis’) | float -
    states(‘sensor.esphome_web_8be748_temp_eau’) | float < 5.0 }}
    id: “off”

Thanks !

yup. having > or | will enble you to start a template on the next line. if you put it on the same line, you have to " it. either’s ok. it’s a matter of preference.

glad it works for you now!

OK… now that it was working in esphome… I try other way: with mqtt.
working with esp32, I send sensor 1 and sensor 2 values to homeassistant via mqtt. what is sent in string so in homeassistant, I used " value… | float " to convert it to a number (float). My problem is that when I look the history on the values, they are shown in a ribbon, not in a curve. Is there a way to see history in a curve graph?

you need to provide a device_class, state_class, and unit_of_measurement for it to be a numerical sensor.