Shelly 3EM 3-phase Net Metering templates for Import, export and consumption

The scripts will give you Import, Export and (consumption if you supply the solar generation).
It is not possible to get the solar generation from the Shelly on the mains, as the house load with use some/all of it.

You would need another Shelly on the Inverter to get Solar generation

Regards

Hi,

Iā€™m quite new in the HA world. I bought recently a shelly 3EM and Iā€™m trying to add it correctly in the power section of HA without success.

I have a shelly 3EM for the IN/OUT measurement of the 3 phases, coulpled with the info coming from my pv inverter SMA sunny boy 4.

My problem now is that the energy export is considered at 0kWh but when Iā€™m going into the details, the value seems to be correct.

The history of the 3 main values are ok too

I just adapted the example code of the 1st page. Here is my config file :

sensor:
  - platform: template
    sensors:

      # Template sensor for values of power import (active_power > 0)
      power_import:
        friendly_name: "Power Import"
        unit_of_measurement: 'W'
        value_template: >-
          {% if (states('sensor.main_power_monitor_channel_a_power')|float + states('sensor.main_power_monitor_channel_b_power')|float + states('sensor.main_power_monitor_channel_c_power')|float) > 0 %}
            {{ states('sensor.main_power_monitor_channel_a_power')|float + states('sensor.main_power_monitor_channel_b_power')|float + states('sensor.main_power_monitor_channel_c_power')|float }}
          {% else %}
            {{ 0 }}
          {% endif %}
        availability_template: "{{
            [ states('sensor.main_power_monitor_channel_a_power'),
              states('sensor.main_power_monitor_channel_b_power'),
              states('sensor.main_power_monitor_channel_c_power')
            ] | map('is_number') | min
          }}"

      # Template sensor for values of power export (active_power < 0)
      power_export:
        friendly_name: "Power Export"
        unit_of_measurement: 'W'
        value_template: >-
          {% if (states('sensor.main_power_monitor_channel_a_power')|float + states('sensor.main_power_monitor_channel_b_power')|float + states('sensor.main_power_monitor_channel_c_power')|float) < 0 %}
            {{ (states('sensor.main_power_monitor_channel_a_power')|float + states('sensor.main_power_monitor_channel_b_power')|float + states('sensor.main_power_monitor_channel_c_power')|float) * -1 }}
          {% else %}
            {{ 0 }}
          {% endif %}
        availability_template: "{{
            [ states('sensor.main_power_monitor_channel_a_power'),
              states('sensor.main_power_monitor_channel_b_power'),
              states('sensor.main_power_monitor_channel_c_power')
            ] | map('is_number') | min
          }}"

      # Template sensor for values of power consumption
      power_consumption:
        friendly_name: "Power Consumption"
        unit_of_measurement: 'W'
        value_template: >-
          {% if (states('sensor.power_export')|float(0)) > 0 and (states('sensor.sb4_0_1av_40_062_grid_power')|float(0) - states('sensor.power_export')|float(0)) < 0 %}
          {% elif (states('sensor.power_export')|float(0)) > 0 and (states('sensor.sb4_0_1av_40_062_grid_power')|float(0) - states('sensor.power_export')|float(0)) > 0 %}
            {{ (states('sensor.sb4_0_1av_40_062_grid_power')|float(0)) - states('sensor.power_export')|float(0) }}    
          {% else %}
            {{ states('sensor.power_import')|float(0) + states('sensor.sb4_0_1av_40_062_grid_power')|float(0) }}
          {% endif %}


  # Sensor for Riemann sum of energy import (W -> Wh)
  - platform: integration
    source: sensor.power_import
    name: energy_import_sum
    unit_prefix: k
    round: 2
    method: left

  # Sensor for Riemann sum of energy export (W -> Wh)
  - platform: integration
    source: sensor.power_export
    name: energy_export_sum
    unit_prefix: k
    round: 2
    method: left

  # Sensor for Riemann sum of energy consumption (W -> Wh)
  - platform: integration
    source: sensor.power_consumption
    name: energy_consumption_sum
    unit_prefix: k
    round: 2
    method: left

utility_meter:
  energy_import_daily:
    source: sensor.energy_import_sum
    name: Energy Import Daily
    cycle: daily
  energy_import_monthly:
    source: sensor.energy_import_sum
    name: Energy Import Monthly
    cycle: monthly
  energy_export_daily:
    source: sensor.energy_export_sum
    name: Energy Export Daily
    cycle: daily
  energy_export_monthly:
    source: sensor.energy_export_sum
    name: Energy Export Monthly
    cycle: monthly
  energy_consumption_daily:
    source: sensor.energy_consumption_sum
    name: Energy Consumption Daily
    cycle: daily
  energy_consumption_monthly:
    source: sensor.energy_consumption_sum
    name: Energy Consumption Monthly
    cycle: monthly

What is wrong in my code ?

Hi,
i have a problem using the codeā€¦ I can not find the sensors in enttiesā€¦
Wahst wrong?

Here is a part of my configuration.yaml, where i include the files for the shelly:

        - sensor:
                   !include shelly3em.yaml
                   
        - utility_meter:
                   !include utility_meter.yaml

Here is the shelly3em.yaml:

sensor:
  - platform: template
    sensors:

      # Template sensor for values of power import (active_power > 0)
      power_import:
        friendly_name: "Power Import"
        unit_of_measurement: 'W'
        value_template: >-
          {% if (states('sensor.l1_power')|float + states('sensor.l2_power')|float + states('sensor.l3_power')|float) > 0 %}
            {{ states('sensor.l1_power')|float + states('sensor.l2_power')|float + states('sensor.l3_power')|float }}
          {% else %}
            {{ 0 }}
          {% endif %}
        availability_template: "{{
            [ states('sensor.l1_power'),
              states('sensor.l2_power'),
              states('sensor.l3_power')
            ] | map('is_number') | min
          }}"

      # Template sensor for values of power export (active_power < 0)
      power_export:
        friendly_name: "Power Export"
        unit_of_measurement: 'W'
        value_template: >-
          {% if (states('sensor.l1_power')|float + states('sensor.l2_power')|float + states('sensor.l3_power')|float) < 0 %}
            {{ (states('sensor.l1_power')|float + states('sensor.l2_power')|float + states('sensor.l3_power')|float) * -1 }}
          {% else %}
            {{ 0 }}
          {% endif %}
        availability_template: "{{
            [ states('sensor.l1_power'),
              states('sensor.l2_power'),
              states('sensor.l3_power')
            ] | map('is_number') | min
          }}"

      # Template sensor for values of power consumption
      power_consumption:
        friendly_name: "Power Consumption"
        unit_of_measurement: 'W'
        value_template: >-
          {% if (states('sensor.power_export')|float(0)) > 0 and (states('sensor.power_solargen')|float(0) - states('sensor.power_export')|float(0)) < 0 %}
          {% elif (states('sensor.power_export')|float(0)) > 0 and (states('sensor.power_solargen')|float(0) - states('sensor.power_export')|float(0)) > 0 %}
            {{ (states('sensor.power_solargen')|float(0)) - states('sensor.power_export')|float(0) }}    
          {% else %}
            {{ states('sensor.power_import')|float(0) + states('sensor.power_solargen')|float(0) }}
          {% endif %}


  # Sensor for Riemann sum of energy import (W -> Wh)
  - platform: integration
    source: sensor.power_import
    name: energy_import_sum
    unit_prefix: k
    round: 2
    method: left

  # Sensor for Riemann sum of energy export (W -> Wh)
  - platform: integration
    source: sensor.power_export
    name: energy_export_sum
    unit_prefix: k
    round: 2
    method: left

  # Sensor for Riemann sum of energy consumption (W -> Wh)
  - platform: integration
    source: sensor.power_consumption
    name: energy_consumption_sum
    unit_prefix: k
    round: 2
    method: left

Here is my utility_meter.yaml

  energy_import_daily:
    source: sensor.energy_import_sum
    name: Energy Import Daily
    cycle: daily
  energy_import_monthly:
    source: sensor.energy_import_sum
    name: Energy Import Monthly
    cycle: monthly
  energy_export_daily:
    source: sensor.energy_export_sum
    name: Energy Export Daily
    cycle: daily
  energy_export_monthly:
    source: sensor.energy_export_sum
    name: Energy Export Monthly
    cycle: monthly
  energy_consumption_daily:
    source: sensor.energy_consumption_sum
    name: Energy Consumption Daily
    cycle: daily
  energy_consumption_monthly:
    source: sensor.energy_consumption_sum
    name: Energy Consumption Monthly
    cycle: monthly

I wonder, if this workaroud could be integrated into the HA shelly integration (until Allterco brings that inside the firmware)?

Purchase a ShellyPro 3 EM and you will save yourself all the sensor toning.
The automatically calculates the returned energy :wink:

1 Like

I dont understand your post. Was that an answer to me or someone else?

The Pro 3EM has the issue as well.

So in my opinion not :thinking:

Have a look at the energy dashboard. It shows power fed back in the grid but its actually not the case.

Returned Energy is not the same as Net Export.

Returned Energy is the sum of all phases where Energy returned to grid.
Net import or export is the sum of all phases, both Energy in and out.
Net is how your meter works it sums up all the + and - energy flows, which is not the same aa just the returned energy.

1 Like

Okay, I figure it doesnā€™t matter much and my goal is Zero Export anyway. I am still a beginner in this matter and have to read up first :wink:

You figure wrong, consider:
Phase A = 500W
Phase B= 500W
Phase C= -1000W (solar generation)

For one hour if the above was static:
Energy returned will be 1000Wh
Net Energy would really be 500+500-1000 = 0Wh

So you can see Returned energy will always give an inflated and incorrect value, if you are on NET metering.

I have 3 Hoymiles HMT 2250, I feed in 3-Phase :wink:

It will be incorrect when house load is bigger than solar production:

Phase A = 1000W - 500W(3 phase solar gen) = 500W
Phase B= 1000W - 500W(3 phase solar gen) = 500W
Phase C= 0W - 500W(3 phase solar gen) = -500W

For one hour if the above was static:
Total Active Energy = 1000Wh
Total Active Returned Energy will be 500Wh
Net Energy would really be 500+500-500 = 500Wh(import)

Ok, I understand what it is about, I feed 3-phase but the consumption on the 3 phases is different and the sum of the 3-phases of input and output is the actual consumption.

Consumption is what the house/load is using, which is different from Net import and export, which is the sum of the phases.
It is different due to self consumption of solar production or a battery being present.

It seems like the empty string returned by the first if statement in power_consumption causes errors in the current version of Home Assistant. Iā€™m getting the following error in my log:

ValueError: Sensor sensor.power_consumption has device class 'None', state class 'None' unit 'W' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: '' (<class 'str'>)

Iā€™ve updated the sensor to return None in the first if statement, although Iā€™m not sure if itā€™s better to return Unavailable or Unknown. Note: Iā€™ve just made this change so Iā€™m not 100% sure if this fix works.

      # Template sensor for values of power consumption
      power_consumption:
        friendly_name: "Power Consumption"
        unit_of_measurement: 'W'
        value_template: >-
          {% if (states('sensor.power_export')|float(0)) > 0 and (states('sensor.power_solargen')|float(0) - states('sensor.power_export')|float(0)) < 0 %}
             None
          {% elif (states('sensor.power_export')|float(0)) > 0 and (states('sensor.power_solargen')|float(0) - states('sensor.power_export')|float(0)) > 0 %}
            {{ (states('sensor.power_solargen')|float(0)) - states('sensor.power_export')|float(0) }}    
          {% else %}
            {{ states('sensor.power_import')|float(0) + states('sensor.power_solargen')|float(0) }}
          {% endif %}
1 Like

Have you try my Setup ? It Works since the new Version of HA flawless without errorsā€¦

Interestingly, the Shelly Cloud is able to do ā€œnet meteringā€?

=> Dimitar Dimitrov

I would like to show you a solution for communicating with Shelly 3PM using InDriver software and visualizing it with Grafana.
You can download InDriver for free from the http://inanalytics.io website.
How to connect InDriver with Shelly 3PM is explained in the video you can find on the InAnalytics web page.
Hereā€™s what a sample Grafana data visualization looks like. Grafana

So, you have a complete set of information on how to make use of the Shelly meter.

Also this forum thread is full of confusion of what net metering etc. means.

What the Shelly (Pro) 3EM directly supports actually is net metering:
simply take the sum of the three phases, including negative values (when at least on one phase there is more production than consumption).
This way, it does not matter whether the energy accumulation (Riemann sum) over time is done before or after.
In effect, the household essentially takes the grid as a free energy storage,
or in other words, sells excess energy to the utility at retail price,
which is why utilities do not like this model, thus it is forbidden in many countries.

What the HA configuration described in this thread provides is something else, namely two-way metering (typically used for feed-in tariff, German word: ZweiwegezƤhler, see also SolarInfo: StromzƤhler und RĆ¼cklaufsperre):
In each moment, at first the three power values on the three phases are summed up (balance, German word: Saldierung).
Then accumulate all positive balances in one register (import) and all negative ones in another (export).
This way, energy import (consumption) can be billed independently of export (production).
At least in Germany, utilities by default do not compensate for energy export.

2 Likes