HVAC and PV inverter on same circuit + Shelly 3EM -> how to calculate HVAC energy consumption

Can anyone help with such a scenario:

  • Shelly 3EM installed on PV inverter to measure PV production
  • HVAC hooked to same circuit as PV, so HVAC directly consumes PV generated power or import from grid if there’s no PV production
  • HVAC is duct system so 3 phases such as inverter

I want to create a template that will:

  • calculate how much energy HVAC consumed while PV inverter is working + how much energy HVAC consumed while PV is not working (so imported from grid)

So far I’ve got mixed results and my idea is:

  • create Shelly 3EM energy exported total sensor
  - sensor:
      - name: "PV energy exported"
        icon: mdi:solar-power-variant
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total_increasing
        state: >
          {% set val1 = states('sensor.shelly3em_total_returned_0') %}
          {% set val2 = states('sensor.shelly3em_total_returned_1') %}
          {% set val3 = states('sensor.shelly3em_total_returned_2') %}
          {{ val1 | float(0) + val2 | float(0) + val3 | float(0) 
            if 
              is_number(val1) and is_number(val2) and is_number(val3)
            else 
              None
          }}
        availability: >- 
          {{ 
          is_number(states('sensor.shelly3em_total_returned_0')) and 
          is_number(states('sensor.shelly3em_total_returned_1')) and
          is_number(states('sensor.shelly3em_total_returned_2'))
  • take total energy produced by PV inverter (the problem is it’s updated every 5 min)
    Calculate how much energy was used by HVAC by calculating:
  - sensor:
      - name: "HVAC energy used"
        icon: mdi:hvac
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total_increasing
        state: >
          {% set val1 = states('sensor.PV_total_energy') %}
          {% set val2 = states('sensor.PV_energy_exported') %}
          {{ val1 | float(0) + val2 | float(0)
            if 
              is_number(val1) and is_number(val2)
            else 
              None
          }}
        availability: >- 
          {{ 
          is_number(states('sensor.PV_total_energy')) and 
          is_number(states('sensor.PV_energy_exported'))
          }}
  • calculate grid used energy by HVAC:
  - sensor:
      - name: "HVAC energy imported from grid"
        icon: mdi:hvac
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total_increasing
        state: >
          {% set val1 = states('sensor.shelly3em_total_0') %}
          {% set val2 = states('sensor.shelly3em_total_1') %}
          {% set val3 = states('sensor.shelly3em_total_2') %}
          {{ val1 | float(0) + val2 | float(0) + val3 | float(0) 
            if 
              is_number(val1) and is_number(val2) and is_number(val3)
            else 
              None
          }}
        availability: >- 
          {{ 
          is_number(states('sensor.shelly3em_total_0')) and 
          is_number(states('sensor.shelly3em_total_1')) and
          is_number(states('sensor.shelly3em_total_2'))
          }}
  • now it should be an easy one but since inverter production is updated every 5 min I cannot get this to work:
    HVAC energy imported from grid + (PV total energy - PV energy exported)

Highly appreciate if any have and idea how to solve that problem.