Template error spurious bracket

I get the following error from my template:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 583, in state
    numerical_value = float(value)  # type:ignore[arg-type]
                      ^^^^^^^^^^^^
ValueError: could not convert string to float: '737.0\n}'
<snip>
ValueError: Sensor sensor.solar_battery_to_grid_w has device class 'None', state class 'None' unit 'W' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: '0.0
}' (<class 'str'>)

The template is actually quite simple:

      - name: "Solar Battery To Grid W"
        unique_id: solar_battery_to_grid_w
        unit_of_measurement: "W"
        icon: mdi:solar-power
        state: >
          {% set exported_power_w = states('sensor.power_grid_export') | float(0) %}
          {% set panel_to_grid_w = states('sensor.solar_panel_to_grid_w') | float(0) %}

          {% if ((exported_power_w + 1) > panel_to_grid_w) %}
            {{ (exported_power_w - panel_to_grid_w) | float(0) }}
          {% else %}
            0
          {% endif %}}

I don’t quite get where that spurious bracket is coming from which is highlighted in the error message.
The input templates are the following:

      - name: "Power - Grid Export"                                             
        unit_of_measurement: "W"                                                
        device_class: "power"                                                   
        state_class: "measurement"                                              
        state: "{{ max([float(states('sensor.solaredge_modbus_m1_ac_power'), 0), 0]) | abs() }}"

      - name: "Solar Panel To Grid W"
        unique_id: solar_panel_to_grid_w
        unit_of_measurement: "W"
        icon: mdi:solar-power
        state: >
          {% set panel_production_w = states('sensor.power_solar_generation') | float(0) %}
          {% set exported_power_w = states('sensor.power_grid_export') | float(0) %}
          {% set house_consumption_w = states('sensor.power_consumption') | float(0) %}
          {% set b1_dc_power = states('sensor.solaredge_modbus_b1_dc_power') | float(0) %}
          
          {% if (exported_power_w > 0 and panel_production_w > 0) %}
            {% if (panel_production_w <= house_consumption_w or house_consumption_w < 50) %}
              0
            {% elif (panel_production_w > exported_power_w) %}
              {{ (exported_power_w) }}
            {% else %}
              {{ (exported_power_w + b1_dc_power) | float(0) }}
            {% endif %}
          {% else %}
            0
          {% endif %}