Calculating export to grid energy from PV Energy and Consumed Energy

I have a PV system to I currently can’t connect to the smart meter so I have only the following useful entities:

  • Total PV Energy (from the inverter itself)
  • Total Consumed Energy (from a Shelly 3EM on the wires to my home main panel)

So I currently can see what energy I’m consuming (without knowing from where PV or Grid) and how much energy the PV system is producing.

I know how to calculate how much I export (the diff between Produced and Consumed while it’s a positive value) but can’t make it so that it is constantly increasing.

This is what I have:

utility_meter:
  grid_energy_3:
    source: sensor.homeenergytotal
    cycle: monthly
    name: grid_energy_3
  pv_energy_3:
    source: sensor.pvenergytotal
    cycle: monthly
    name: pv_energy_3 

template:
  - sensor:
      - name: 'Grid Export 3'
        unique_id: grid_export_3
        state: "{{ [0, states('sensor.pv_energy_3')|float - states('sensor.grid_energy_3')|float] | max }}"
        unit_of_measurement: "kWh"
        state_class: total_increasing

With this setup I am facing two issues:

  1. The Energy graph is offset on PV production as when the PV system starts producing (ex 6AM) I already have around 5kWh Consumed Energy so until the energy produced by the PV system catches up, I will see no actual representation in the graph
  2. The difference between the two sensors is not cumulative even though I specified “total_increasing” (I tried with delta sum but the same thing happens).

Anyway, I tried various combinations but can’t get it to work as it should (tried with hourly energy meter cycles, yearly, delta_sum, etc) still either have the offset, or erroneous data due to not being cumulative.

Any tips for this scenario? Thanks