Shelly 3EM 3-phases Energy sensor

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

1 Like

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
2 Likes

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?

I’m quite sure that you can not just add the energy from the 3 phases. You have to divide the result with SQRT(3) to get the actual amount that the electrical company registers (note that this is a good thing, less energy spent!).

Here’s my calculation:

    consumed_energy:
      friendly_name: "Total Consumption"
      device_class: energy
      unit_of_measurement: 'kWh'
      value_template: "{{ ((states('sensor.em_channel_a_energy')|float + states('sensor.em_channel_b_energy')|float + states('sensor.em_channel_c_energy')|float)/sqrt(3))|round(2) }}"
      availability_template: "{{ not ( is_state('sensor.em_channel_a_energy', 'unavailable') or is_state('sensor.em_channel_b_energy', 'unavailable') or is_state('sensor.em_channel_c_energy', 'unavailable') ) }}"

does anyone have this working with the MQTT based setup (mqtt shelly discovery script)? I’m trying to backport your findings to it, but it isn’t exactly straight forward i found

i use this:

- sensor:
  - name: "House Energy kWh"
    unit_of_measurement: "kWh"
    state_class: measurement
    device_class: energy
    state: >
      {{ [ states('sensor.shelly_3em_meter_total_0'), 
           states('sensor.shelly_3em_meter_total_1'),
           states('sensor.shelly_3em_meter_total_2') ]
           | map('float') | sum }}
    attributes:         
      last_reset: '1970-01-01T00:00:00+00:00'
    availability: >
      {{ not 'unavailable' in 
        [ states('sensor.shelly_3em_meter_total_0'), 
          states('sensor.shelly_3em_meter_total_1'),
          states('sensor.shelly_3em_meter_total_2') ] }}

but i get those zero values after HA is reloaded anyhow

image

I’m thinking that we need to filter out also “unknown” state