Long term statistics in automations or templates

How can we actually use/access the long term statistics? For example, in an automation or with templating? Thanks!


Use case: I’d like to have a template sensor computing the co2 emission of my house since “the beginning”
I have a total_increasing sensor for electricity consumption and sensor.co2_intensity (from electricity map integration).

Would anyone have an idea on this?

Also see this WTH post and its cross-links (I found no solution yet): WTH can't I access the stored statistics data in my automations and templates?