Formatting a value in if statement

Hi all, I have a problem with this IF statement:

  - name: consumo_diff_produzione
    unit_of_measurement: W
    state: >
      {% if states("sensor.fotovoltaico_power") > states('sensor.consumo_totale_power') %}
        0
      {%- else -%}
        {{(states('sensor.consumo_totale_power')|float - states("sensor.fotovoltaico_power")|float )}}
      {%- endif %}
    availability: "{{ states('sensor.consumo_totale_power')|is_number and states('sensor.fotovoltaico_power')|is_number }}"
    device_class: power

When the value ‘sensor.fotovoltaico_power’ exeeceds the 1000.00 and ‘sensor.consumo_totale_power’ remains smaller the the boolean expression becomes false! Why? I think it’s a problem in the variable declaration but I don’t understand where I’m wrong.
Excuse my ignorance.
Thanks

You need to convert the values to an integer

You’re comparing strings.

You need it use a float filter:

{% if states("sensor.fotovoltaico_power") | float > states('sensor.consumo_totale_power') | float  %}

The whole template can be reduced to:

{{ max(states('sensor.consumo_totale_power')|float(0)-states('sensor.fotovoltaico_power')|float(0), 0) }}

Thanks. Works!!