Energy statistic from template sensor

Hi Guys, i have this small issue, i’ll try to explain what i’m doing.

i have a small photovoltaic panel connected on grid so, when my home consumtion il lower than sun production some energy will flow to the national grid. i want to monitor this value in order to see on storical graph and configuring it to Home assistant energy panel.

I have a standard SHELLY power meter with 2 phisical energy sensor: one measure the photovoltaic production and the second measure whole house consumption.

In order to see the istant power sent to the national grid i have prepared this sensor:

sensor:
  - platform: template
    sensors:
        shelly_immissioni_in_rete:
            friendly_name: "Immissioni in rete e-Distribuzione"
            unit_of_measurement: 'W'
            value_template: '{{ states("sensor.shelly_shem_e45c39_2_current_consumption") |int - states("sensor.shelly_somma_consumi") |int }}'

sensor.shelly_shem_e45c39_2_current_consumption = istant total photovoltaic production
sensor.shelly_somma_consumi = istant total house energy consumption

it works.

Now i want to store/create new historical entity for this new sensor, i used platform statistics

  - platform: statistics
    entity_id: sensor.shelly_immissioni_in_rete
    name: "Immissioni totali in rete e-Distribuzione"
    state_characteristic: total
    max_age:
        year: 2

at this point i have some trouble, Max_age only accept time data, year/years is not accepted and this sounds strange for me because for main house consumption the shelly integration create an apposite entity with more than 1 years of data inside.

thanks for support !

I have found a solution for this request, maybe will help other user.
The solution use platform integration instead of platform statistics:

  - platform: integration
    source: sensor.shelly_immissioni_in_rete
    name: shelly_immissioni_in_rete_TOTALE
    unit_prefix: k
    round: 2

Consider using the “left” mode otherwise your results may be wrong.