Service Template if, elif, else, endif not working, config check says OK!

Hi
i made a service template but i think there is a bigger problem. it is not working. config check and template check says OK.

could somebody give me a direction where to search for the probleme?

    -  service_template: >
        {% if (states('sensor.openweathermap_forecast_wind_speed') | float > 3 and
          states('sensor.openweathermap_forecast_temperature') | float > 26)  %}
            homeassistant.turn_on
            entity_id: group.teichpumpen_wind_hitze
        {% elif (states('sensor.openweathermap_forecast_wind_speed') | float > 3 and
          states('sensor.openweathermap_forecast_temperature') | float < 26)  %}
            homeassistant.turn_on
            entity_id: group.teichpumpen_wind
        {% elif (states('sensor.openweathermap_forecast_wind_speed') | float < 3 and
          states('sensor.openweathermap_forecast_temperature') | float > 26)  %}
            homeassistant.turn_on
            entity_id: group.teichpumpen_hitze
        {% else %}
            homeassistant.turn_on
            entity_id: group.teichpumpen
        {% endif %}
    - service: homeassistant.turn_on
      target:
        entity_id: >
          {% if (states('sensor.openweathermap_forecast_wind_speed') | float > 3 and
          states('sensor.openweathermap_forecast_temperature') | float > 26)  %}
            group.teichpumpen_wind_hitze
          {% elif (states('sensor.openweathermap_forecast_wind_speed') | float > 3 and
          states('sensor.openweathermap_forecast_temperature') | float < 26)  %}
            group.teichpumpen_wind
          {% elif (states('sensor.openweathermap_forecast_wind_speed') | float < 3 and
          states('sensor.openweathermap_forecast_temperature') | float > 26)  %}
            group.teichpumpen_hitze
          {% else %}
            group.teichpumpen
          {% endif %}

This is a key value pair:

key: value

You can only template the value, not the key and definitely not both.

1 Like

How can I handle the problem?

Exactly the way I just showed you in the template I wrote above. Or you could make it more readable like this:

    - service: homeassistant.turn_on
      target:
        entity_id: >
          {% set wind_speed = states('sensor.openweathermap_forecast_wind_speed') | float(0) %}
          {% set temperature = states('sensor.openweathermap_forecast_temperature') | float(0) %}
          
          {% if wind_speed > 3 and temperature > 26 %}
            group.teichpumpen_wind_hitze
          {% elif wind_speed | float > 3 and temperature < 26 %}
            group.teichpumpen_wind
          {% elif wind_speed | float < 3 and temperature > 26 %}
            group.teichpumpen_hitze
          {% else %}
            group.teichpumpen
          {% endif %}
1 Like

thank you.
it runs like a charme

1 Like

If you’re interested, the template can be reduced to this:

    - service: homeassistant.turn_on
      target:
        entity_id: >
          {% set x = iif(states('sensor.openweathermap_forecast_temperature') | float(0) > 26,  '_hitze', '') %}
          {% if states('sensor.openweathermap_forecast_wind_speed') | float(0) > 3 %}
            group.teichpumpen_wind{{ x }}
          {% else %}
            group.teichpumpen{{ x }}
          {% endif %}
1 Like