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

Go to the History tab and add the sensors you want, beyond that I don’t know.

If the shelly sensor for the phases do not update in Homeassistant due to wifi turned off then it can’t calculate the net import export. Homeassistant needs the data from shelly real time.

2 Likes

Hi @uksa007,

Your code works really well so far, but I could observe something strange:
Power Consumption is: approx. 120-130W
Solar Feed: approx. 200W
Power Import: 0W
Power Export: approx. 70-80W

If the solar feed increases abruptly, for example because the sun comes out, then power consumption drops.
Example as follows:

I now have the following errors in the log:
image

The solar feed is measured with the Shelly Plug S.

configuration.yaml

  - platform: template
    sensors:
    
      # Template sensor for values of power import (active_power > 0)
      power_import:
        friendly_name: "Power Import"
        unit_of_measurement: 'W'
        device_class: power
        value_template: >-
          {% if (states('sensor.phase_1_power')|float + states('sensor.phase_2_power')|float + states('sensor.phase_3_power')|float) > 0 %}
            {{ states('sensor.phase_1_power')|float + states('sensor.phase_2_power')|float + states('sensor.phase_3_power')|float }}
          {% else %}
            {{ 0 }}
          {% endif %}
        availability_template: "{{
            [ states('sensor.phase_1_power'),
              states('sensor.phase_2_power'),
              states('sensor.phase_3_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'
        device_class: power
        value_template: >-
          {% if (states('sensor.phase_1_power')|float + states('sensor.phase_2_power')|float + states('sensor.phase_3_power')|float) < 0 %}
            {{ (states('sensor.phase_1_power')|float + states('sensor.phase_2_power')|float + states('sensor.phase_3_power')|float) * -1 }}
          {% else %}
            {{ 0 }}
          {% endif %}
        availability_template: "{{
            [ states('sensor.phase_1_power'),
              states('sensor.phase_2_power'),
              states('sensor.phase_3_power')
            ] | map('is_number') | min
          }}"

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

Thanks for your feedback in advance.

This is a result of timing differences between the Shelly and the solar output sensor.
When the solar output changes quickly the shelly3em may update this sooner than the solar sensor(inverter) which results in some big incorrect swings in consumption calculation, most of which I try to ignore. Unless you have one sensors device that can measure 3phase mains and the solar generation at the same instant in time, you have to live with it.

Thanks for your feedback.
In general I was just wondering, what is the reason for that.
For others as info, what I am using:
Solar: Shelly Plug S and Hoymiles HMS-500
Measuring: Shelly 3EM
→ Solar Feed on the 2nd phase
Wifi-Router: AVM Fritz 7530AX
RPi4 with SSD connected with via Ethernet Cable to the Router.

excelent solution. thank you!

Guys, i had a smappee solar for measuring appliances and solar etc… but went dead :frowning:
maybe i’m gonna buy a shelly3 3-phase…

My smappee solar had 6 clamps, 3 for my solar and 3 for my electricity

This shelly3 em has only 3 clamps? how does it work? can it measure only electricity and not solar?

It measures power in both directions similar to your network provided meter, so it can tell how much you are importing or exporting per phase(not how much your solar is generating due to self consumption of solar by your house), however it does not calculate net metering(sum of all phases), hence these scripts I created.

More info can be found here https://www.shelly.cloud/en-mt/products/product-overview/shelly-3-em

So if I also want to know how much I’m generating, I need a two shellys? Can they configured like one for consumption and for generating? So I have all data visible in the Shelly app?

I don’t use the shelly app, so not sure how that works.

I get my solar production data from my inverter directly into Home assistant via direct integration.

Yeah, I can do that too, but I have a kostal Piko converter, old type, there is a custom compoy, but based on scraping and on scan interval… So the data is odfcourse not accurate

This doesn’t work? Kostal Plenticore Solar Inverter - Home Assistant
If not you may need another shelly, there are many options(single and 3phase), check out their web site.

No, that’s the new generation… But I can read the total power and then just substract from last hour to import, that should be accurate

The last HA update somehow killed my sd-card. All backups on the sd-card are gone (yes, my fault). I expect there is no chance to import the data stored within the shellys to get my historic data back? :sleepy:

Hello Guys

im new in the HA Section and i found a script for measuring the energy, my problem is, it shows the number right but if i add energy consumption and solar energy together to the dashboard it adds both together

it shows 2.1kwh consumption and 0,5 Solar daily, thats wrong cause it should be 1.6 kwh from the net and 0.5 from the solar

my setup is like that main power suply → house energymeter → power outlet + solar plugged in → wire into the apartment into the fusebox with the em3

so the em3 measures the energy from the energymeter and solar power, is there a way that i can put it with the right measurements to the dashboard? like em3 power - Solar power?

If you are on Net metering, please follow the instructions on first page of this topic, it creates the correct sensors to use in the Energy dashboard, there is further details of how to set up further down on the first page.

For it to work correctly the Shell 3EM needs to see Net Import an export, so it needs to be before the solar inverter plugged in, near the house meter. Not sure if this is how it is, seems like it’s after the solar inverter, so it only sees house load not the net of house and solar.

Essentially all you need is Net Import and Net Export and solar generation (you will need to get this from the inverter directly(HA integration) or via another Shelly).

Hi fila612, I have the same problem and can’t explain why it’s the case.
Did you find the explanation of that?

Sounds to me like you have one or more of the Shelly clamps installed backwards, make sure all phases are installed with the L pointing to the Grid side:

In our case clamps are connected correctly as all values are ok when no solar generation we have - we see positive correct energy consumption and power values on all three phases.

When solar starts to generate energy power values on all three phases become negative (which is correct as generated energy goes to grid), however for some reason not all generated solar energy goes to grid for some reason (or at least Shelly say that). And this is million dollar question - why Shelly reports that only about half of solar energy goes to grid?

On the graph below due to not all energy goes from solar to grid, HA thinks that part of generate solar energy were used in the house, but it’s not the case in reality - I know for sure that we have around 0.1kWh energy usage all the time.

How do you have the energy dashboard configured, can you show a screenshot of it?
Are you using my scripts in the energy dashboard?
It seems you have three colours for import and export which does not look like my script output of one for export an import.

The native shelly sensors do NOT calculate Net metering, so they will not be accurate when used in the energy dashboard, hence why I created my scripts to calculate the NET of the phases for import and export.

If you are using my scripts it should look like this, (I have two solar inverters hence the two solar production entries)