Shelly 3EM 3-phases Energy sensor

The new Energy section is really cool.
We use Shelly 3EM with the built-in Shelly integration (not ShellyForHASS) to measure all 3-phases coming to the house.
There are 2 options to integrate it with the new Energy section:

  1. Add all 3 phases (A, B, and C) under “Configuration → Energy”. A stacked bar will be used to show the total.
  2. Create a template sensor to combine the 3. It’s somewhat tricky to get it to work with the statistics engine. Here is what we use:

[Update: 2021.9 introduced a change, and the “state_class” should be “total_increasing” instead of “measurement”. The “last_reset” attribute is not needed anymore.]

template:
  - sensor:
      - name: "Energy Total"
        unique_id: energy_total
        state: >-
          {{ 
            [ states('sensor.phase_a_energy'), 
              states('sensor.phase_b_energy'),
              states('sensor.phase_c_energy'),
            ] | map('float') | sum
          }}
        availability: >-
          {{ 
            [ states('sensor.phase_a_energy'), 
              states('sensor.phase_b_energy'),
              states('sensor.phase_c_energy'),
            ] | map('is_number') | min
          }}
        unit_of_measurement: kWh
        device_class: energy
        state_class: total_increasing
6 Likes

I have prepared my shelly 3EM with following code:

homeassistant:
  customize_glob:
    sensor.*_total_consumption:
      unit_of_measurement: "kWh"
      last_reset: "1970-01-01T00:00:00+00:00"
      device_class: energy
      state_class: measurement

@msvb04, I think that you are referring to ShellyForHASS entities. The entities created by the built-in Shelly integration already have all required attributes, and there is no need to add anything for using them in the energy section. However, there is no “total” entity to sum all 3-phases, which is the reason for this post.

2 Likes

I’m using the builtin Shelly integration. I wanted to add up channel A and channel B to get a total for Watts used, but when I try to add a statistics graph card, I don’t see Power Total as an option to select. This is what I have for my template:

sensor:
  - name: "Power Total"
    unique_id: power_total
    state: >-
      {{ states('sensor.shelly_3em_channel_a_power')|float + states('sensor.shelly_3em_channel_b_power')|float }}
    unit_of_measurement: W
    device_class: energy
    state_class: measurement
    attributes:
      last_reset: "1970-01-01T00:00:00+00:00"

Is it possible to do the same for power (and not energy)?

@flyingmoose, one thing which needs to be fixed is the “device_class”. It should be “power” (instead of “energy”). The “last_reset” attribute is not needed, since this is not a cumulative sensor. The rest looks fine.
Please note that you should give it an hour before adding the new card, since the statistics engine does it magic only once per hour (in the12th minute of the hour).
Here is our configuration for “power total” and “power factor total”, along with the new statistics graph cards:

template:
  - sensor:
      - name: "Power Total"
        unique_id: power_total
        state: >-
          {{ (states('sensor.phase_a_power')|float + 
              states('sensor.phase_b_power')|float + 
              states('sensor.phase_c_power')|float) }}
        unit_of_measurement: W
        device_class: power
        state_class: measurement
      - name: "Power Factor Total"
        unique_id: power_factor_total
        state: >-
          {{ (((states('sensor.phase_a_power')|float) * (states('sensor.phase_a_power_factor')|float) +
                (states('sensor.phase_b_power')|float) * (states('sensor.phase_b_power_factor')|float) +
                (states('sensor.phase_c_power')|float) * (states('sensor.phase_c_power_factor')|float)) /
              (states('sensor.phase_a_power')|float + states('sensor.phase_b_power')|float + states('sensor.phase_c_power')|float))
              |int }}
        unit_of_measurement: "%"
        device_class: power_factor
        state_class: measurement

Thank you! I made the change you suggested yesterday afternoon and waited overnight for statistics to populate, and it is working as expected now. Thank you again for your help!

1 Like

I’m also using the Shelly EM3 and a similar template to sum the energy usage for the 3 phases:

  - sensor:
      - name: "house_energy_kWh"
#        friendly_name_template: "House Energy (Total)"
        unit_of_measurement: "kWh"
        state_class: measurement
        device_class: energy
        state: >
          {% set phase1 = states('sensor.shellyem3_40F52000EDD0_channel_a_energy') | float %}
          {% set phase2 = states('sensor.shellyem3_40F52000EDD0_channel_b_energy') | float %}
          {% set phase3 = states('sensor.shellyem3_40F52000EDD0_channel_c_energy') | float %}
          {{ float(phase1) + float(phase2) + float(phase3) }}
        attributes:         
        last_reset: '1970-01-01T00:00:00+00:00'

However I am having problems due to (apparently) occasional unavailability of the EM3 causing the float filter to produce a 0 value which the Utility Meter then interprets as a reset causing spikes and big steps in the values of the daily/monthly totals. See below:


I have tried using the Outlier filter to remove the 0 values but without success.

Any ideas on how I can remove these random 0 values?

Thanks

@pfrock42, you can find software workarounds, like keeping the old value if one of the phases is zero. However, you should probably fix the Wi-Fi coverage around the Shelly 3EM so you get a stable connection and reliable sensors.

Use the availability option to suppress reporting a value when any of the three sensors is unavailable. tom_l provided an example here:

Try this version:

  - sensor:
      - name: "house_energy_kWh"
        unit_of_measurement: "kWh"
        state_class: measurement
        device_class: energy
        state: >
          {{ [ states('sensor.shellyem3_40F52000EDD0_channel_a_energy'), 
               states('sensor.shellyem3_40F52000EDD0_channel_b_energy'),
               states('sensor.shellyem3_40F52000EDD0_channel_c_energy') ]
             | map('float') | sum }}
        attributes:         
          last_reset: '1970-01-01T00:00:00+00:00'
        availability: >
          {{ not 'unavailable' in 
             [ states('sensor.shellyem3_40F52000EDD0_channel_a_energy'), 
               states('sensor.shellyem3_40F52000EDD0_channel_b_energy'),
               states('sensor.shellyem3_40F52000EDD0_channel_c_energy') ] }} 
2 Likes

Thanks. Good point about finding the root cause of the unavailability of the Shelly… I don’t think it’s the WiFi (I’ve put a lot of work into ensuring good Wifi around the house) but I’ll definitely double check!

Many thanks for this!

I’ve just put it in so I should be able to see if it works within a few hours.

I can confirm that this works! Although the Shelly has been more stable over the last 48h there have been 3 unavailability events that were correctly filtered out by this so thank you again.

I’m still looking into the root cause of the unavailability events… the WiFi signal at the Shelly is -65 dBm so, not great, but then neither is it streaming 4k video… Currently it is <10m from the access point but admittedly behind a heavy fire proof door (for building regs) so I’ll install another AP within the room and see if it improves.

Basic Question.

Where are you putting the above code in?

I am unable to create the sensor yet my config passes they server test.

The above config snippet should placed in the “configuration.yaml” file.

1 Like

Thank you. I have spited my config file and it works as you suggested in the main configuration.yaml but not in the other folders I tried. Eg Sensor.

Would you know where to store it in this case?

The “!include” operator dumps the content of the other file.
So, basically you do the following in the “configuration.yaml” file:

template: !include template.yaml

And then add the file “template.yaml” with the following content:

- sensor:
    - name: "Energy Total"
      unique_id: energy_total
      state: >-
        {{ states('sensor.phase_a_energy')|float + 
           states('sensor.phase_b_energy')|float + 
           states('sensor.phase_c_energy')|float }}
      unit_of_measurement: kWh
      device_class: energy
      state_class: measurement
      attributes:
        last_reset: "1970-01-01T00:00:00+00:00"
    - name: "Power Total"
      unique_id: power_total
      state: >-
        {{ (states('sensor.phase_a_power')|float + 
            states('sensor.phase_b_power')|float + 
            states('sensor.phase_c_power')|float) }}
      unit_of_measurement: W
      device_class: power
      state_class: measurement
    - name: "Power Factor Total"
      unique_id: power_factor_total
      state: >-
        {{ (((states('sensor.phase_a_power')|float) * (states('sensor.phase_a_power_factor')|float) +
              (states('sensor.phase_b_power')|float) * (states('sensor.phase_b_power_factor')|float) +
              (states('sensor.phase_c_power')|float) * (states('sensor.phase_c_power_factor')|float)) /
            (states('sensor.phase_a_power')|float + states('sensor.phase_b_power')|float + states('sensor.phase_c_power')|float))
            |int }}
      unit_of_measurement: "%"
      device_class: power_factor
      state_class: measurement

Fantastic… All working. Thank you.

1 Like

Hi,
I’m using Shelly 3EM together with Solar PV and 3-phase system. I noticed that the “current_consumption” entities show negative values when my solar panels are working (makes sense).

However, I cannot figure out how to get real “consumption” of power in my house - that is the actual Power in W being used in the house (without solar). I tried to use the “total_returned” entity available but that shows only the accumulated energy (not power). Unfortunately - I don’t have a separate meter on my solar panels :frowning:

Is it possible for me to see current Power usage (without taking into account the solar panels)?

I am trying to setup the return to grid (Negative values of total 3EM) but it doesn’t seems to work , does anybody has it setup and working ?

Hi @Papi77

Yes, that’s possible by calculation OR by placing the Shelly Clamps on the “right spots”.

How to calculate real consumption in a PV setup depends on how the PV has been integrated. There are multiple ways to do so…
Hybrid inverters (those with battery and power out support) are creating a virtually second neutral which could produce errors in measurement.

Measurement:

  • With Hybrid inverters one has to measure at least three points (Inverter-Grid, Grid-House, Inverter-House)
  • With Non Hybrid Inverters one has to measure at least two points (Inverter-Grid, Grid-House)
  1. Do you have a hybrid inverter (with batteries)? Those
  2. how exactly did you connect the phases of the shelly?