TemplateSyntaxError: expected token 'end of statement block

I give up, what’s wrong with this code?

- sensor:
    - name: "SolarEdge - Export/Import"
      unit_of_measurement: "kW"
      state_class: total_increasing
      device_class: "energy"
      state: >
        {% if states("sensor.solaredge_m1_ac_power") | float(0) > 1 %}
          {% states("sensor.solaredge_ac_power") %}
        {% else %}
          {% states("sensor.solaredge_m1_ac_power") %}
        {% endif %}
  • Invalid config for ‘template’ at templates.yaml, line 71: invalid template (TemplateSyntaxError: expected token ‘end of statement block’, got ‘{’) for dictionary value ‘sensor->0->state’, got ‘{% if state: {{ (states(“sensor.solaredge_ac_power”)) > (states(“sensor.solaredge_m1_ac_power”)) }\n states(“sensor.solaredge_ac_power”)\n{% else %}\n states(“sensor.solaredge_m1_ac_power”)\n{% endif %}’

Wrong bracketry for the output lines:

        {% if states("sensor.solaredge_m1_ac_power") | float(0) > 1 %}
          {{ states("sensor.solaredge_ac_power") }}
        {% else %}
          {{ states("sensor.solaredge_m1_ac_power") }}
        {% endif %}

See the bottom of the Synopsis section here.

Also note that these two lines don’t match and will probably cause issues:

      unit_of_measurement: "kW"
      device_class: "energy"

Power is measured in kW, energy in kWh.

1 Like