Shelly 3EM 3-phases Energy sensor

Newbie question.
How do I calculate total energy consumed by my house if I have Solar panels?

 a + b + c + (-a returned) + (-b returned) + (-c returned) + solar energy.

Is that correct? Because if my shelly sits before all appliances, the energy a+b+c readings from shelly do not include energy consumed by the house which was produced with solar?

Hi @gerasalus,

HA can calculate that for you.
You have L1, L2 and L3 for energy consumption and the same for “energy_return” in the entities. (L… are the phases)
You can set all in the energy dash and then you are good to go.
Or maybe I missunderstood you and you are looking for a code?

In my case for example, I summarize all three phases together using that code in a template.yaml file:

- sensor:
    - name: "Energy Total"
      unique_id: energy_total
      state: >-
        {{ states('sensor.phase_1_energy')|float(0) + 
           states('sensor.phase_2_energy')|float(0) + 
           states('sensor.phase_3_energy')|float(0) }}
      unit_of_measurement: kWh
      device_class: energy
      state_class: total_increasing
      attributes:
        last_reset: "1970-01-01T00:00:00+00:00"
      availability: >
          {{ not 'unavailable' in 
             [ states('sensor.phase_1_energy'), 
               states('sensor.phase_2_energy'),
               states('sensor.phase_3_energy') ] }}
    - name: "Power Total"
      unique_id: power_total
      state: >-
        {{ (states('sensor.phase_1_power')|float(0) + 
            states('sensor.phase_2_power')|float(0) + 
            states('sensor.phase_3_power')|float(0)) }}
      unit_of_measurement: W
      device_class: power
      state_class: measurement
      availability: >
          {{ not 'unavailable' in 
             [ states('sensor.phase_1_power'), 
               states('sensor.phase_2_power'),
               states('sensor.phase_3_power') ] }}


Info: My WIFI gets deactivated during nighttime. Therefore there is no recording and a “peak” at the beginning of each day.

The only thing I am wondering is, that even I generate more energy than I am currently using, I still can see a consumption. I thougt, this might be because of a peak but I can´t see any:

Solar Panel feed (Shelly Plug S):

Summarized power consumption (Shelly 3EM):

Maybe somenbody has an idea what the issue might be here :wink:

Cheers and enjoy your weekend guys.

4 Likes

Hi,
I am quite new to HA (3-4- weeks) and I still have very limited skills even if I have read tons of posts.
I have photovoltaic panels and an electric car with bidirectional charging capacity. So my car is used as a battery for my home.
I have just installed 2 Shelly 3EM: The first one measures the energy imported/exported from the grid and the second one measures the energy flow between the car (battery) and my home.
I also have access to the energy values from the inverter.
My question is about the total (3 phases) energy measured by the Shelly devices.
The calculation kindly provided in this thread is: Total Energy = Energy L1 + Energy L2 + Energy L3. So far so good.
Shelly 3EM provides two sensors per phase according to the direction of the energy flow. One sensor is “imported energy” and the other one is “exported energy”. OK.
Now my real concern:
My understanding is that these sensors always have positive (or increasing as they are sums) values.
Now imagine the following caricatural situation:
My home consumes 2kW during 1 hour, ie 2kWh, exclusively from phase L1.
My battery provides this energy but exclusively on phase L2.
So, formally I have imported 2 kWh from the grid on L1 and exported 2 kWh to the grid on L2.
The net flow is zero kWh and (I hope) my electricity provider will charge 0$.
But the calculated energy measured by Shelly will be: 2kWh imported and 2kWh exported.
As I pay 25 cts/kWh for imported energy and I receive only 8 cts/kWh for exported energy, the cost calculation in HA will be 2x25-2x8 = 34 cts, which is wrong.
Besides, I am interested by the net instant flow of energy.

Did I miss anything?

I hope my text was understandable despite my limited english.

1 Like

I live in a very old house and the wiring is not the newest.

I found out that the power supply system is a TN-C system. Does the shelly 3em work with this kind of system?

Since the most complete version is somewhere in this thread, here is my solution after going through the whole discussion:

template:
  - sensor:
    - name: "Energy Total"
      unique_id: energy_total
      state: >-
          {{ 
            [ states('sensor.shellyem3_channel_a_energy'), 
              states('sensor.shellyem3_channel_b_energy'),
              states('sensor.shellyem3_channel_c_energy'),
            ] | map('float') | sum
          }}
      unit_of_measurement: kWh
      device_class: energy
      state_class: total_increasing
      attributes:
        last_reset: "1970-01-01T00:00:00+00:00"
      availability: >-
          {{ 
            [ states('sensor.shellyem3_channel_a_energy'), 
              states('sensor.shellyem3_channel_b_energy'),
              states('sensor.shellyem3_channel_c_energy'),
            ] | map('is_number') | min
          }}
    - name: "Power Total"
      unique_id: power_total
      state: >-
          {{ 
            [ states('sensor.shellyem3_channel_a_power'), 
              states('sensor.shellyem3_channel_b_power'),
              states('sensor.shellyem3_channel_c_power'),
            ] | map('float') | sum
          }}
      unit_of_measurement: W
      device_class: power
      state_class: measurement
      availability: >-
          {{ 
            [ states('sensor.shellyem3_channel_a_power'), 
              states('sensor.shellyem3_channel_b_power'),
              states('sensor.shellyem3_channel_c_power'),
            ] | map('is_number') | min
          }}
    - name: "Current Total"
      unique_id: current_total
      state: >-
          {{ 
            [ states('sensor.shellyem3_channel_a_current'), 
              states('sensor.shellyem3_channel_b_current'),
              states('sensor.shellyem3_channel_c_current'),
            ] | map('float') | sum
          }}
      unit_of_measurement: A
      device_class: current
      state_class: measurement
      availability: >-
          {{ 
            [ states('sensor.shellyem3_channel_a_current'), 
              states('sensor.shellyem3_channel_b_current'),
              states('sensor.shellyem3_channel_c_current'),
            ] | map('is_number') | min
          }}

6 Likes

When copying and pasting exactly this, I get “Unavailable” for all sensors. Anybody an idea why?
I don’t see anything related to that anywhere (Logbook, History etc). The Shelly 3EM itself is working fine in HA, also the energy board.

Hope you solved it by now. If not: Did you make sure to adjust sensor.shellyem3_channel_ to your setup? Search the entities for 3em to find the correct naming.

1 Like

Thanks, yes. It took me a while but I solved it.
What was it? My Shelly 3em sensors were named slightly different than the above :roll_eyes::roll_eyes:
So one really has to pay attention and not just assume that all will be named identical

1 Like

I live in US (Colorado), hence two phase electricity, and I have a 3.2 kW PV grid-tied system. The solar inverter feeds through a sub-panel into the main service panel. I have an inverter/charger on that sub-panel, along with crucial circuits, so if the grid goes down I can still run those circuits from solar and batteries. I placed channels A and B of my new EM3 around the main panel service feeds, and channel C around one of the solar feeds.

For power calculations, I sum A and B for grid power and double the value of C for solar power (power output of the solar inverter is the same on each phase). All power is a combination of grid and solar power. For power factor, I average A and B for the house’s power factor. solar power factor is in there for completeness but isn’t really useful.

For energy calculations, I map channel C twice for all energy, and A and B for grid energy. Energy returned is a map of A and B; the solar inverter never runs backward. Incidentally, when I first put in the system in 2009, the local power company didn’t yet have a digital meter that could count backwards, so for awhile I had the old-fashioned kind with a wheel that could spin either way. I sort of miss that.

Below is the template file I wrote, based on the templates I’ve read in this thread. I would appreciate keen eyes checking it; it reads as valid and loads but I haven’t set up the energy monitor. I would also appreciate suggestions in using the values in energy monitor. I pay a straight value for each kWh and the utility pays me the same for returned energy. As always, thanks for any help.

- sensor:
  - name: "All Power Total"
    unique_id: all_power_total
    state: >-
      {{ (states('sensor.shellyem3_channel_a_power')|float + 
          states('sensor.shellyem3_channel_b_power')|float +
          (states('sensor.shellyem3_channel_c_power')|float * 2)) }}
    unit_of_measurement: W
    device_class: power
    state_class: measurement
    availability: >
          {{ not 'unavailable' in 
             [ states('sensor.shellyem3_channel_a_power'), 
               states('sensor.shellyem3_channel_b_power'),
               states('sensor.shellyem3_channel_c_power') ] }}

  - name: "Grid Power Total"
    unique_id: grid_power_total
    state: >-
      {{ (states('sensor.shellyem3_channel_a_power')|float + 
          states('sensor.shellyem3_channel_b_power')|float) }}
    unit_of_measurement: W
    device_class: power
    state_class: measurement
    availability: >
          {{ not 'unavailable' in 
             [ states('sensor.shellyem3_channel_a_power'), 
               states('sensor.shellyem3_channel_b_power') ] }} 

  - name: "Solar Power Total"
    unique_id: solar_power_total
    state: >-
      {{ (states('sensor.shellyem3_channel_c_power')|float * 2) }}
    unit_of_measurement: W
    device_class: power
    state_class: measurement
    availability: >
          {{ not 'unavailable' in 
             [ states('sensor.shellyem3_channel_c_power') ] }}

  - name: "Grid Power Factor Total"
    unique_id: grid_power_factor_total
    state: >-
      {{ ((states('sensor.shellyem3_channel_a_power_factor')|float + 
          states('sensor.shellyem3_channel_b_power_factor')|float)) / 2 | int }}
    unit_of_measurement: "%"
    device_class: power_factor
    state_class: measurement
    availability: >
          {{ not 'unavailable' in 
             [ states('sensor.shellyem3_channel_a_power_factor'), 
               states('sensor.shellyem3_channel_b_power_factor') ] }}

  - name: "Solar Power Factor Total"
    unique_id: solar_power_factor_total
    state: >-
      {{ (states('sensor.shellyem3_channel_c_power_factor')|float ) | int }}
    unit_of_measurement: "%"
    device_class: power_factor
    state_class: measurement
    availability: >
          {{ not 'unavailable' in 
             [ states('sensor.shellyem3_channel_c_power_factor') ] }}

  - name: "All Energy Total"
    unique_id: all_energy_total
    state: >-
      {{ [ states('sensor.shellyem3_channel_a_energy'), 
           states('sensor.shellyem3_channel_b_energy'),
           states('sensor.shellyem3_channel_c_energy'),
           states('sensor.shellyem3_channel_c_energy'),
         ] | map('float') | sum }}
    unit_of_measurement: kWh
    device_class: energy
    state_class: total_increasing
    attributes:
      last_reset: "1970-01-01T00:00:00+00:00"
    availability: >-
          {{ [ states('sensor.shellyem3_channel_a_energy'), 
               states('sensor.shellyem3_channel_b_energy'),
               states('sensor.shellyem3_channel_c_energy'),
             ] | map('is_number') | min }}

  - name: "Grid Energy Total"
    unique_id: grid_energy_total
    state: >-
      {{ [ states('sensor.shellyem3_channel_a_energy'), 
           states('sensor.shellyem3_channel_b_energy'),
         ] | map('float') | sum }}
    unit_of_measurement: kWh
    device_class: energy
    state_class: total_increasing
    attributes:
      last_reset: "1970-01-01T00:00:00+00:00"
    availability: >-
          {{ [ states('sensor.shellyem3_channel_a_energy'), 
               states('sensor.shellyem3_channel_b_energy'),
             ] | map('is_number') | min }}

  - name: "Grid Energy Returned Total"
    unique_id: grid_energy_returned_total
    state: >-
      {{ [ states('sensor.shellyem3_channel_a_energy_returned'), 
           states('sensor.shellyem3_channel_b_energy_returned'),
         ] | map('float') | sum }}
    unit_of_measurement: kWh
    device_class: energy
    state_class: total_increasing
    attributes:
      last_reset: "1970-01-01T00:00:00+00:00"
    availability: >-
          {{ [ states('sensor.shellyem3_channel_a_energy_returned'), 
               states('sensor.shellyem3_channel_b_energy_returned'),
             ] | map('is_number') | min }}

  - name: "Solar Energy Total"
    unique_id: solar_energy_total
    state: >-
      {{ [ states('sensor.shellyem3_channel_c_energy'), 
           states('sensor.shellyem3_channel_c_energy'),
         ] | map('float') | sum }}
    unit_of_measurement: kWh
    device_class: energy
    state_class: total_increasing
    attributes:
      last_reset: "1970-01-01T00:00:00+00:00"
    availability: >-
          {{ [ states('sensor.shellyem3_channel_c_energy'),
             ] | map('is_number') | min }}

When using this I get a message that state_class is not allowed. How did you solve this?

1 Like

Witam
Udalo ci sie aktywowac ten 4 zacisk Shelly Em3

Niestety nie… nawet nie znalazłem żadnej infimrmacji o tym jak to naprawdę działa ani jak to wykorzystac w innych systemach :frowning: Dead end…

Hi @modernhistorian ,
I have a similar situation in Australia. I have a 3-Phase meter box + Shelly EM3 + 5KW inverter. The energy provider does net metering per/sec so I don’t need the details of all the phases just net results.

My .YAML game is poor I need some help, as i am getting an error for the following code (it’s a Frenstein’s monster from your original code)

sensor:

Merging 3-phase power memter to a single sensor [Shelly 3EM 3-phases Energy sensor - #130 by modernhistorian]

  • name: “All Power Total”

    unique_id: bsct_sen_3em_power

    state: >-

    {{ (states(‘sensor.bsct_sen_3em_channel_a_power’)|float +

      states('sensor.bsct_sen_3em_channel_b_power')|float +
    
      (states('sensor.bsct_sen_3em_channel_c_power')|float )) }}
    

    unit_of_measurement: W

    device_class: power

    state_class: measurement

    availability: >

      {{ not 'unavailable' in
    
        [ states('sensor.bsct_sen_3em_channel_a_power'),
    
          states('sensor.bsct_sen_3em_channel_b_power'),
    
          states('sensor.bsct_sen_3em_channel_c_power') ] }}
    
  • name: “All Energy Total”

    unique_id: bsct_sen_3em_energy

    state: >-

    {{ [ states(‘sensor.bsct_sen_3em_channel_a_energy’),

    states('sensor.bsct_sen_3em_channel_b_energy'),
    
    states('sensor.bsct_sen_3em_channel_c_energy'),
    
    states('sensor.bsct_sen_3em_channel_c_energy'),
    
    ] | map('float') | sum }}
    

    unit_of_measurement: kWh

    device_class: energy

    state_class: total_increasing

    attributes:

    last_reset: “1970-01-01T00:00:00+00:00”

    availability: >-

      {{ [ states('sensor.bsct_sen_3em_channel_a_energy'),
    
        states('sensor.bsct_sen_3em_channel_b_energy'),
    
        states('sensor.bsct_sen_3em_channel_c_energy'),
    
        ] | map('is_number') | min }}
    

I am getting the following error

The first thing I see is that you didn’t declare a platform, in this case template. That’s not needed in mine because they’re in a file called templates.yaml which is marked included in configuration.yaml. So write this:

sensor:
  - platform: template
    name: "All Power Total"

and so on. Remove the dashes in front of names, line them up, and re-check. I don’t think this will eliminate all errors (I can’t see line 2), but post remaining errors and the referenced lines in configutation. By the way, I think that the state_class: total_increasing no longer uses the last_reset attribute, so you can remove those lines. I have. One more thing: where are the shelly clamps mounted?

Czesc mirekmal
W najnowszej wersji shelly 3EM jest mozliwosc dodania czwartego transformatora na N neutralny kabel.
Trzeba tylko transformator zalozyc na jedna z faz i przeprowadzic kalibracje faz w panelu tam też w ustawieniach ustawiamy czułość domyslnie 1A. Po wszystkim przepinamy transformator z powrotem na linie N i mamy w panelu info o neutralnym. Nie widzę tego sensora w Home Assistant ale niewiem czy powinien sie sam dodac czy coś trzeba jeszcze zrobić.

In the new shelly 3EM version it is possible to add a fourth transformer to the N neutral cable.
You just need to put it on one of the phases and calibrate the phases in the panel, there also set the default sensitivity to 1A in the settings. After all, we reconnect the transformer back to the N line and we have info about the neutral in the panel

To naprawde dobra wiadomość! Właśnie i u mnie pojawiła się nowa werska firmware i też już widzę możliwość przeprowadzenia kalibracji. Pewnie jutro, w wolnej chwili się tym zajmę. Dam znać jak postępy!

Ok, cool i am giving that a go. no error so far :smiley:

I have the clamps on my main 3-phases, not 100% where that is internally… had an electrician install it for me a while back. Why is that?

This is what i see, if that helps

after I get this working I’ll be trying to group the light (PowerCalc) entities, just to make things a bit more meaningful

So a quick question:
If I got the 3EM and set it up it would tell me the total amperage across the three phases?

Would that then mean I could use this to influence the Lux single phase inverter if I could hack the register for the Ct clamp and create an automation?

Or is there any point or product that would allow the Lux Ct cable to plug into to take this reading back to the inverter as if it was the Ct clamp doing it?

Just a quick question, is it possible to disconnect/connect power with Shelly 3EM? I would like to control when to charge my car based on the electricity price. I have not found suitable smart plug for that as regular plugs do not fit to my 3 phase socket.