Shelly EM in net-metering system monitoring Solar Power generation + House Power Consumption

I have Shelly EM monitoring:
Channel 1: Solar Production
Channel 2: House’s consumption

The energy panel though requests the following parameters:
1)PV generation energy
2)Returned to Grid energy
3)Consumed from Grid energy
In my case, Shelly’s channel 1 is equal to (1). For (2) and (3) I need to calculate them.

If Shelly.Power.Channel1 > Shelly.Power.Channel2, that means I generate more power than I consume hence the excessive energy goes all to grid.

If Shelly.Power.Channel2 > Shelly.Power.Channel1, that means I consume more power than I generate hence subtracting Channel2-Channel1 gives the energy drew from the Grid.

So far here is what I have setup.

template:
  - sensor:
    - name: "Exported_Energy"
      unique_id: "Exported_Energy"
      unit_of_measurement: "W"
      device_class: power
      state_class: measurement
      state: >-
        {% if states('sensor.shellyem_244cab419c34_channel_1_power')|float(0) - states('sensor.shellyem_244cab419c34_channel_2_power')|float(0) > 0 %}
            {{ states('sensor.shellyem_244cab419c34_channel_1_power')|float(0) - states('sensor.shellyem_244cab419c34_channel_2_power')|float(0) }}
          {% else %}
            {{ (states('0.0') | float(0)) |round(3) }}
          {% endif %}
  - sensor:
    - name: "Grid Consumption"
      unique_id: "Grid_Consumption"
      unit_of_measurement: "W"
      device_class: power
      state_class: measurement
      state: >-
        {% if states('sensor.shellyem_244cab419c34_channel_2_power')|float(0) - states('sensor.shellyem_244cab419c34_channel_1_power')|float(0) > 0 %}
            {{ states('sensor.shellyem_244cab419c34_channel_2_power')|float(0) - states('sensor.shellyem_244cab419c34_channel_1_power')|float(0) }}
          {% else %}
            {{ (states('0.0') | float(0)) |round(3) }}
          {% endif %}
  - sensor:
      - name: "Grid_Exported_Energy1"
        unique_id: "Grid_Exported_Energy1"
        state: "{{ '%0.2f'|format(states('sensor.Avg_Exported_Energy')|float / 1000 * (now().hour + now().minute/60)) }} "
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total_increasing
      - name: "Grid_Consumed_Energy1"
        unique_id: "Grid_Consumed_Energy1"
        state: "{{ '%0.2f'|format(states('sensor.Avg_Consumed_Energy')|float / 1000 * (now().hour + now().minute/60)) }} "
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total_increasing
        
sensor:
  - platform: average  
    name: 'Avg_Exported_Energy'
    unique_id: "Avg_Exported_Energy"
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'
    entities: sensor.Exported_Energy
  - platform: average
    name: 'Avg_Exported_Energy_5_Min'
    unique_id: "Avg_Exported_Energy_5_Min"
    duration:
      minutes: 5
    entities: sensor.Exported_Energy
  - platform: average  
    name: 'Avg_Consumed_Energy'
    unique_id: "Avg_Consumed_Energy"
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'
    entities: sensor.Grid_Consumption
  - platform: average
    name: 'Avg_Consumed_Energy_5_Min'
    unique_id: "Avg_Consumed_Energy_5_Min"
    duration:
      minutes: 5
    entities: sensor.Grid_Consumption

Maybe someone could help because above setup doesn’t work well. It turns out above config has some fault.

Here is what I get in the energy setup screen:

Moreover, at the log I get this:
image

which is very strange, given the average has same configuration for both Avg_Exported_Energy and Avg_Consumed_Energy, but only of them has the problem.

Hi,

Maybe too little too late as I don’t know if you have resolved it.

My thought process is this:

It is not about whether grid consumption is higher or lower than solar production. It is about whether solar is negative (when producing) or positive (when consuming.) Whilst solar is producing, consumption will always equal [(net in/out) minus (solar generated)] given net in/out could be either positive or negative and solar produced is normally negative. So net consumption of -1.6kWh and solar of -2.5kWh (produced) will give consumption of 0.9kWh, or net consumption of 0.5kWh and solar of -1.7kWh (produced) would give you consumption of 2.2kWh.
My understanding is that consumption should always be positive and the production should always be negative. The time where it would be different is when there is no solar production and yet the inverter is still using power. In this case the solar production amount will show as being positive, not the usual negative as it is a consumer not a producer. The positive amount from the inverter would then need to be added to the consumption calculation to reconcile.

If solar production <0, then net minus solar, otherwise net plus solar.

Secondly, I would make sure that the units of measure are able to be used in the energy dashboard as there are only specific units that can be used.

I have the same setup that I haven’t worked out yet, so if you have worked it out I am keen to see how you did it.