Shelly 3EM 3-phase Net Metering templates for Import, export and 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.

1 Like

Long story short:
The Shelly 3EM is not working like a accumulation meter (only sum of all three phases is counted) with a power source. This is why we do these calculations here.

I noticed some difference between my “energy_import_daily” and the readings from my official meter (ferraris/induction meter). The Shelly is measuring 10-15% less kWh than the meter.
Is it because we only get a measurement every x seconds (sometimes every 5 secs, sometimes no update for 30 secs) and there is too much missing in between?

I’m using the official Shelly integration and I assume it’s using CoIoT for pushing values. Is using MQTT giving more steady values?

I wonder if this could be done on-device using “shelly script” to perform net metering and present new sensors. It may be more accurate particularly if there are periods where the connection is lost (bad wifi etc). I might look into it.

It’s been a while, but for the benefit of others who are experiencing the same issue: I think this happens when there has not been any exported power for a while. Then the power_export template sensor will keep outputting exactly 0, thus no new values will be recorded by HA (as it only records changing values), leading to problems with statistics/meters.

To work around this, I added the following to the sensor template:

  attributes:
    minute_counter: "{{ now().minute }}"

This will force the state to change at least once a minute.

See also here:

1 Like

Hello, I have been having the problem for days that the sensor ‘energy_export_daily’ can be seen with a value in the developer tools, but it cannot be added in the Energy Dashoboard because it does not appear there under ‘Return to Grid’.
Only ‘Energy Export Monthly’ is available.
How can I tackle a problem like this?

1 Like

Hi Guys, new to HA and net monitoring. We have recently installed a new solar system with a goodwe inverter that has native HA integration for solar generation. Currently on 3 phase mains with a 3 phase inverter connected through an inverter circuit breaker. Can i use the shelly 3em to achieve net monitoring or should i go for the Pro instead?

No need for the Pro if your wifi at your installation spot is good enough.

Hello all,

I am trying for a while to setup the part “returned to the grid” of the Energy dashboard, without success.

I bought and installed a Shelly Pro 3EM with 3 pieces of 120A Current Transformers.
The Pro 3EM was well installed on the Shelly App and in HA.

I have solar panels, pluged on the phase A of the electric installation ; when I produce, I have a negative value on phase A, and positives values on B and C, which is normal. But the sensor “Total active energy returned” seems to calculate all of my solar production, not only what is returned to the grid.
I guess this is a parameter issue ; and of course if I tried to add this sensor on the Energy board in the “Returned to the grid” part, this is not correct.

I tried to follow your tutorial

But I am unable to make it work, so there should be an error somewhere, here is my config :

In the sensor.yaml :

# Shelly 3EM templates for 3-phase Net Import, Export and Consumption(if you have solar generation details)
# This uses the shelly instantaneous power sensors to achieve the best possible accuracy. 
# Shelly Sensors are  sensor.l1_power, sensor.l2_power, sensor.l3_power for the three phases
# Solar generation in W is used to calculate consumption via sensor.power_solargen
# V1.0 Initial release by Uksa007
# V1.1 add float(0) to Consumption template to stop log warnings.
# V1.2 add Friendly names to Utility Meter sensors
# V1.3 remove negative spikes from power consumption due to different update timing of solar sensor
# V1.4 change round: 2 for small value users.

  - 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.shelly_pro_edf_tri_phase_a_active_power')|float + states('sensor.shelly_pro_edf_tri_phase_b_active_power')|float + states('sensor.shelly_pro_edf_tri_phase_c_active_power')|float) > 0 %}
            {{ states('sensor.shelly_pro_edf_tri_phase_a_active_power')|float + states('sensor.shelly_pro_edf_tri_phase_b_active_power')|float + states('sensor.shelly_pro_edf_tri_phase_c_active_power')|float }}
          {% else %}
            {{ 0 }}
          {% endif %}
        availability_template: "{{
            [ states('sensor.shelly_pro_edf_tri_phase_a_active_power'),
              states('sensor.shelly_pro_edf_tri_phase_b_active_power'),
              states('sensor.shelly_pro_edf_tri_phase_c_active_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.shelly_pro_edf_tri_phase_a_active_power')|float + states('sensor.shelly_pro_edf_tri_phase_b_active_power')|float + states('sensor.shelly_pro_edf_tri_phase_c_active_power')|float) < 0 %}
            {{ (states('sensor.shelly_pro_edf_tri_phase_a_active_power')|float + states('sensor.shelly_pro_edf_tri_phase_b_active_power')|float + states('sensor.shelly_pro_edf_tri_phase_c_active_power')|float) * -1 }}
          {% else %}
            {{ 0 }}
          {% endif %}
        availability_template: "{{
            [ states('sensor.shelly_pro_edf_tri_phase_a_active_power'),
              states('sensor.shelly_pro_edf_tri_phase_b_active_power'),
              states('sensor.shelly_pro_edf_tri_phase_c_active_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.total_production_solaire')|float(0) - states('sensor.power_export')|float(0)) < 0 %}
          {% elif (states('sensor.power_export')|float(0)) > 0 and (states('sensor.total_production_solaire')|float(0) - states('sensor.power_export')|float(0)) > 0 %}
            {{ (states('sensor.total_production_solaire')|float(0)) - states('sensor.power_export')|float(0) }}    
          {% else %}
            {{ states('sensor.power_import')|float(0) + states('sensor.total_production_solaire')|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

In configuration.yaml :

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

I am not able to add the sensor “active_power_export” in the Energy part as it is not in the list.
All the created sensors are not showing anything.
But when testing them in Development tools, the datas are correct :

I don’t know where I am wrong, so don’t hesitate to tell me if you see an error or something missing.
Don’t hesitate if you need more details.

Thanks in adavnce for your help !
Franck

my wifi is excellent

Has anyone got a Shelly 3em hooked up with a powerwall with solar?

We had one installed but due to the way the powerwall balances the house usage it causes the Shelly to only see around 200W in use.

Not sure if the gateway is using some magic to calculate the load.

Im noticing this same issue, also it seems to think the house used more of the solar and exported less. The next day when i check my supplier’s dashboard, it’s exported more. Did you get to the bottom of this? Im also using native shelly integration. I have a 3 phase meter with 3 phase inverter

@uksa007 what is the point of this if condition when there are no steps for it, it just moves to the elseif statement. am i missing something?

{% if (states('sensor.power_export')|float(0)) > 0 and (states('sensor.power_solargen')|float(0) - states('sensor.power_export')|float(0)) < 0 %}

It removes incorrect negative spikes in the data due to timing differences between when the data arrives from the different sensors, eg when the solar quickly reduces due to clouds.

1 Like

thanks for the clarification mate.