Calculating the sum of an energy sensor

Hi,
so the inverter of my photovoltaic died and I got a new one. It's all working now, but with the new inverter the produced energy counter reset to 0. So now I don't have a sensor anymore that tells me how much energy was produced by the photovoltaic over it's lifetime.

The energy dashboard doesn't seem to have a problem that the value started counting from 0 again. It just continued showing the correct values. So I can get the total produced energy if I select a large range in the energy dashboard. But how does it do that? How can I get the same data into a sensor? The raw data is in the statistics, but I'm unable to extract it the same way the energy dashboard does it.

Any help and tips appreciated.

There is an action recorder.get_statistics you could use to fetch the all time data for a statistic and sum it up with a template. That's roughly what energy dashboard does (via different API)

yes. I shouldn't need to sum it up manually. For my need as it's a TOTAL_INCREASING sensor, I just need to get the newest value from the sum field from long-term statistics (picture from the SQLite database):

I just need to figure out how to do this in a template.