Self-consumed solar energy, calculating per phase, but needs balanced

I’m halfway there but it is still not satisfactory.

I can correctly calculate the 3 phase net metering over a time period; 15 minutes, hourly, but it still doesn’t account correctly for short term spikes. Of course as the time period gets smaller the accuracy improves, so it maybe good enough.

Certainly a lot more accurate for three phase net metering than just using the raw Shelly energy sensors for each phase:

Configuration, not dissimilar to your concept description:

template:
# Create sum of shelly energy across three phases and take away energy_returned from single phase PV inverter
# Check availability of sensors to exclude zero entries
# Could replace with persistent shelly_total_energy when included in integration (not yet available)
  - sensor:
      - name: "Shelly Energy Total"
        unique_id: shelly_energy
        state: "{{ 
              states('sensor.shelly_channel_a_energy')|float + 
              states('sensor.shelly_channel_b_energy')|float +
              states('sensor.shelly_channel_c_energy')|float -
              states('sensor.shelly_channel_c_energy_returned')|float
              }}"
        availability: "{{ 
            [ states('sensor.shelly_channel_a_energy'), 
              states('sensor.shelly_channel_b_energy'),
              states('sensor.shelly_channel_c_energy'),
              states('sensor.shelly_channel_c_energy_returned')
            ] | map('is_number') | min
          }}"
        unit_of_measurement: kWh
        device_class: energy
        state_class: total

utility_meter:
# Break shelly total energy up into utility meters to create measurement periods
  settlement_energy:
    source: sensor.shelly_energy_total
    cron: "*/30 * * * *"
    net_consumption: true
  hourly_energy:
    source: sensor.shelly_energy_total
    cycle: hourly
    net_consumption: true
  daily_energy:
    source: sensor.shelly_energy_total
    cycle: daily
    net_consumption: true
  monthly_energy:
    source: sensor.shelly_energy_total
    cycle: monthly
    net_consumption: true
  yearly_energy:
    source: sensor.shelly_energy_total
    cycle: yearly
    net_consumption: true
# For the settlement period (defined in cron:) create a meter that sums the last_period
# for import and export - this feeds the Energy Dashboard
  settlement_import:
    source: sensor.settlement_import_last_period
    delta_values: true
  settlement_export:
    source: sensor.settlement_export_last_period
    delta_values: true

)

# template sensors
# split +ve settlement(shelly)_energy into import
    settlement_import_last_period:
      value_template: "{{max(0,state_attr('sensor.settlement_energy', 'last_period')|float)}}"
      unit_of_measurement: kWh

# split -ve settlement(shelly)_energy into export
    settlement_export_last_period:
      value_template: "{{(min(0,state_attr('sensor.settlement_energy', 'last_period')|float))|abs}}"
      unit_of_measurement: kWh