If else statement in a value template

I am trying hard to assign a value to a sensor depending on the state of an entity (Delta T: if the pump runs = calc and 0 if the pump doesn’t run. This is what I did:

- platform: template
    sensors:
      delta_t_solar_heating:
        friendly_name: Delta T
        unit_of_measurement: °C
        value_template:  {% if is_state('switch.sonoff_1000bee815', 'on') %} 
                         {{ (( states('sensor.sonoff_1000bee0f9_temperature') | float ) -  ( states('sensor.sonoff_1000bee815_temperature') | float )
                                ) | float | round(1) }}
                         {% elif %}
                         0.0
                         {% endif %}       

it gives an error in the template tester, but can’t figure out what I should do. The calculation itself works without the if syntax.

Obviously I shuffled the syntax in all kind of ways but can’t find the right structure. I must be close but who can point where I go wrong …?

value_template: >
  {% if is_state('switch.sonoff_1000bee815', 'on') %}
    {{ ((states('sensor.sonoff_1000bee0f9_temperature')|float
    (states('sensor.sonoff_1000bee815_temperature')|float))
    |float | round(1) }}
  {% else %} 0.0 {% endif %}

Don’t know why but the forums mangling my code box this evening :man_shrugging: 12 minutes of editing, finally got it!

Basically you need else instead of elif and a > for the multiline template

1 Like

Many thanks!!! Solved. Silly from hindsight but I didn’t see it :face_with_hand_over_mouth:

1 Like