Count energy returned to the network

Hi, I’ve two sensors configured; one for generated energy by photovoltaic panels and one for total energy consumed by the whole home. In homeassistant energy panel all seems to work as expected.
Now I’d like to create a sensor to count energy I don’t use for returned energy in the “Energy panel”.

I’ve tried with something like this:

      - name: Energia non utilizzata
        state: >
          {% if (states('sensor.energia_prodotta_fotovoltaico_kwh') | float  - states('sensor.energy_spent_total_kwh') | float) <= 0 %}
          {% else %}
             {{ states('sensor.energia_prodotta_fotovoltaico_kwh') | float  - states('sensor.energy_spent_total_kwh') | float   }} 
          {% endif %}
        unit_of_measurement: "kWh"
        state_class: total_increasing 
        device_class: energy

but it seems not work.

Can someone point me in the right direction?