Turn on based on 2 temp sensor readings

I am trying to figure out how to configure an automation based on 2 temp sensor readings.
I am initially attempting to do this in the web interface but I may wind up having to do this in yaml.

I am looking for some advice on how to do this.

I have 2 temp sensors. Temp1 and Temp2
if Temp1 is 10 degrees greater than Temp2 then turn on Device1

If I can figure this out then I can do the exact same thing backwards that would turn off the device.

Any help would be greatly appreciated.
J

    trigger:
      - platform: template
        value_template: >
          {% if states('sensor.temp1') not in ['unknown','unavailable']
              and states('sensor.temp2') not in ['unknown','unavailable'] %}
            {{ (states('sensor.temp1')|int(0) + 10) > states('sensor.temp2')|int(0) }}
          {% endif %}
1 Like

Thanks Jason I will give that a try and if I have any issues I will post.

Shrink, shrank, shrunk.

trigger:
  - platform: template
    value_template: >
      {% set (t1, t2) = (states('sensor.temp1'), states('sensor.temp2')) %}
      {{ iif(t1|is_number and t2|is_number, t1|int(0) - t2|int(0) > 10, false) }}
2 Likes

Ugh. Why do you keep teaching me stuff!! Do you have any idea how many templates I have to go back and evaluate how I can make more efficient now! :sob:

When I attempt to put that into the automation by using yaml I am getting an error on save stating the Message is malformed.

Am I going to be able to do it from there or will I need to put this into my configuration.yaml?

@123 commented with a more efficient version of the template so I’ve used that here.

Create a new template trigger in an automation and paste just this into the template box.

{% set (t1, t2) = (states('sensor.temp1'), states('sensor.temp2')) %}
      {{ iif(t1|is_number and t2|is_number, t1|int(0) - t2|int(0) > 10, false) }}

I am going to take some time to read up on this.
I understand the logic behind what you sent I just need to read up on this a bit.

Thank you for your assistance.

That worked!
Thank you for pointing that out.
Now to tweak this to work for my setup.

Thank you both for the assistance.