Can't seem to get syntax for if-then-else template to work

I can get a basic automation to turn the fan off, on, or set a speed, but when I try to get it all into one automation that sets the fan speed based on the temperature differential between upstairs and downstairs, it doesn’t seem to work. I’m assuming there’s something wrong with my templating syntax, but I can’t seem to get it ironed out.

automation:
  - alias: fans to different speeds
    trigger:
      platform: time_pattern
      minutes: "/1"
    action:
      service: fan.set_speed
      entity_id:
        - fan.kitchen_fan
      data_template:
        speed: >
          {% if (abs(climate.main_floor.current_temperature - climate.upstairs.current_temperature) <2) -%} off
          {%- elif (abs(climate.main_floor.current_temperature - climate.upstairs.current_temperature) >5) -%} 4
          {%- else -%} 2 {%- endif -%}

It doesn’t look like your code matches the example in the docs.

Here’s the example:

automation:
  trigger:
    platform: time
    at: "07:15:00"
  action:
    - service: fan.set_speed
      data:
        entity_id: fan.kitchen
        speed: low

Note that the entity_id: is under data:, which in your case would be data_template:.

It also doesn’t show numbers for speeds, but I don’t know anything about the fan integration. I don’t see anything wrong with your template syntax, other than it outputs “off” and numbers, which may be correct for your device.

The entities you are using in the code aren’t set up correctly.

Since I don’t have your entities (or any climate entities at all) I don’t know how they are configured.

I’m going to assume that the “current_temperature” is an attribute of the entity?

and you have to remember that all states are strings so you will need to convert the strings to numbers before you can do math functions.

However…

I can’t find anywhere that says that the “abs()” function is valid in HA. the jinja docs say it’s valid but it isn’t listed in the HA templating docs.

And when I try it in the template editor it says that ‘abs’ is undefined.

However also …

after googling a bit I found that to get the absolute value you need to use ‘abs’ as a filter not a function

and I think you had an extra “)” in there too.

All of that means that you will need to set up the template as such:

{% if (state_attr('climate.main_floor', 'current_temperature') | int - state_attr('climate.upstairs', 'current_temperature') | int ) | abs < 2 %} 
  off
{% elif (state_attr('climate.main_floor', 'current_temperature') | int - state_attr('climate.upstairs', 'current_temperature') | int ) abs > 5 %}
  4
{% else %} 
  2 
{% endif %}

I think…

Thanks to both. It totally makes sense to cast those ‘current_temperatures’ as ints before doing the < comparison. Also seems like the abs() function doesn’t work either way.

So I’m onto the next idea, which is to try to write this as 2 or more separate automations.

Like I said above the abs() function doesn’t work but using the abs filter should. See my code above for its use.