I know this is an old topic, but I had the same question. For anyone coming across this topic in the future, the following is hopefully a nice overview of the calculations:
Using the numbers from the picture above:
{% set energy_imported = float(2.45) %}
{% set energy_production = float(15.2) %}
{% set energy_exported = float(11.6) %}
{% set energy_used = energy_imported + energy_production - energy_exported %}
Energy used: {{ energy_used }}
Net returned to grid: {{ energy_exported - energy_imported }}
Self-consumed solar energy: {{ (energy_production - energy_exported ) / energy_production * 100.0 | float | round(2) }}%
Self sufficiency: {{ (energy_used - energy_imported ) / energy_used}}
Gives the following result:
Energy used: 6.049999999999999
Net returned to grid: 9.149999999999999
Self-consumed solar energy: 23.684210526315788%
Self sufficiency: 0.5950413223140495
Which is in line with: