Power template error

My inverter only provides data every 5 minutes. I have a DSMR reader that generate data more frequently. Since that occasionaly I send more energy to the grid that my solar production. I would like to avoid this situation using conditional sensor.
There is a syntax error regarding the last conditional sensor. Do you have any idea what can be the problem?

template:
  - sensor: 
    - name: dsmr_returned_in_watts
      unit_of_measurement: "W"
      state: "{{ states('sensor.dsmr_reading_electricity_currently_returned')|float * 1000 }}"
    - name: dsmr_delivered_in_watts
      unit_of_measurement: "W"
      state: "{{ states('sensor.dsmr_reading_electricity_currently_delivered')|float * 1000 }}"
    - name: "Actual PV Power"
      unit_of_measurement: W
      device_class: power
      icon: mdi:solar-power-variant
      state: >
        {% set pwreturn = states('sensor.dsmr_returned_in_watts')|float %}
        {% set pv = states('sensor.sma_ac_power')|float %}
        {% if pwreturn > pv ) %}
          {{ (pwreturn|float) %}}
        {% else %} {{ (pv|float) %}}
        {% endif %}

Extra ) and two extra %s:

        {% if pwreturn > pv ) %}
----------------------------^
          {{ (pwreturn|float) %}}
------------------------------^
        {% else %} {{ (pv|float) %}}
---------------------------------^

Always try out templates in Developer Tools / Template first.

You don’t need the last two |floats as you’ve already done the casting. You could replace the whole if statement with:

{{ pwreturn if pwreturn > pv else pv }}

or even:

{{ (pwreturn, pv)|max }}

Thanks! Great and elegant!