Shelly 3EM 3-phases Energy sensor

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

Thanks for the code in this thread! I managed to sum up all my shelly devices to bigger consumption units (i.e. the 3 Phases of the wall connector or all lights). However when I use the energy entity generated with the code from the first post, on day 1 it will show the entire energy of the specific devices since they were installed. On day two it is only the daily consumption. This only happens to the entities that are added up. Is there a way I can tell home assistant to start monitoring energy from a specific day and forget about energy that was monitored before that date?

I use shelly4hass and customized my energy entities to be visible in the energy section of home assistant.

all right… so not only it’s not working as expected, as i still see deep to zero instead of going into unavailable … but we now also get this in the logs

2021-10-07 12:39:40 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'float' got invalid input 'unavailable' when rendering template '{{ [states('sensor.shelly_3em_meter_total_0'),
states('sensor.shelly_3em_meter_total_1'),
states('sensor.shelly_3em_meter_total_2') ]
| map('float') | sum }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2021.12

Version 2021.10.0 now requires that, for certain functions such as float, the default value is explicitly specified. If it’s missing, it’s reported as a warning message. In the December release, it will become an error message.

Change it from this:

map('float')

to this:

map('float', 0)

If float is unable to convert a value, it will report 0 for that value. If you want it to report something else, just change the 0 to whatever you prefer.

2 Likes

thank you!

Hi @amitfin . Thanks for your post, I’m new to HA and I’m trying to configure my shelly 3em into HA. I defined the sensor into a sensor.yaml file where I’ll put all my virtual sensors, them I recall this file into configuration.yaml with template: !include sensors.yaml, I check the configuration and it’s valid. Anyway when I go to energy section for adding the consumptions I still find the three sensors for each faces and not the overall energy sensor created. Can please you tell me why? Many thanks for your time and patience :slight_smile:

Hi, why do you need to do this? Isn’t the energy per phase the total energy used?

BR.,
Lars

That is likely the case, when I first tested it it looked like the sum needed to be divided by srt3 and on the Shelly forum there was also an explanation to this. However since I now have removed my unit I can not confirm either anymore so stick with just adding if that gives you the same values as your electric company debit you.

Guys, anyone able to help her?
i am not sure what’s going wrong …

@genga91, here are a few screenshots from the “Developer Tools” section which can help you troubleshooting:


image

@Spitfire, the 3EM device itself reports the total energy consumption. It’s only the home assistant integration that is missing this total sensor.
Here is a side-by-side screenshot of our September numbers from the 3EM itself and the simple sum in home assistant.
As can be seen, the numbers are aligned (and dividing it by SQRT(3) would make them different):

1 Like

Hi @amitfin,

thanks so far for your code.
The issue I am facing since a few days is, that I get these spikes.

I mean in general the measurement works fine:

image

(Just a small appartement with a 385W Solar Cell on the balcony, Hoymiles 300 Inverter and measured with a Xiaomi Mi Smart Plug.)

Therefore maybe you have an advise for me, what is wrong in my case?

Thanks in advance.

I can’t see them as entities. I please ask you to check if I defined well the sensor. Here the code:

sensor:

  • name: “Power Total”
    unique_id: power_total
    state: >-
    {{ (states(‘sensor.shellyem3_c45bbe5f75bc_channel_a_power’)|float +
    states(‘sensor.shellyem3_c45bbe5f75bc_channel_b_power’)|float +
    states(‘sensor.shellyem3_c45bbe5f75bc_channel_c_power’)|float) }}
    unit_of_measurement: W
    device_class: power
    state_class: total_increasing
  • name: “Power Factor Total”
    unique_id: power_factor_total
    state: >-
    {{ (((states(‘sensor.shellyem3_c45bbe5f75bc_channel_a_power’)|float) * (states(‘sensor.shellyem3_c45bbe5f75bc_channel_a_power_factor’)|float) +
    (states(‘sensor.shellyem3_c45bbe5f75bc_channel_b_power’)|float) * (states(‘sensor.shellyem3_c45bbe5f75bc_channel_b_power_factor’)|float) +
    (states(‘sensor.shellyem3_c45bbe5f75bc_channel_c_power’)|float) * (states(‘sensor.shellyem3_c45bbe5f75bc_channel_c_power_factor’)|float)) /
    (states(‘sensor.shellyem3_c45bbe5f75bc_channel_a_power’)|float + states(‘sensor.shellyem3_c45bbe5f75bc_channel_b_power’)|float + states(‘sensor.shellyem3_c45bbe5f75bc_channel_c_power’)|float))
    |int }}
    unit_of_measurement: “%”
    device_class: power_factor
    state_class: total_increasing
  • name: “Energy Total”
    unique_id: energy_total
    state: >-
    {{ states(‘sensor.shellyem3_c45bbe5f75bc_channel_a_energy’)|float +
    states(‘sensor.shellyem3_c45bbe5f75bc_channel_b_energy’)|float +
    states(‘sensor.shellyem3_c45bbe5f75bc_channel_c_energy’)|float }}
    unit_of_measurement: kWh
    device_class: energy
    state_class: total_increasing

@genga91, I think that the configuration should be placed in template.yaml and not sensor.yaml.
I’m not using “!include” directive in our config, so it’s easy to see the hierarchy (think about “!include” as dumping the content of the other file in the place you want to.)

In my case the config is as follows in a seperate template.yaml file

I still get the stupid “overall-since-installing”-value:

Does anyone maybe know the reason for that?

@Chickenbreast, “state_class” for “enregy_total” should be “total_increasing” instead of “measurement”.

I moved into the configuration file directly and it’s working well know. Don’t understand why it doesn’t if I make a new file into config folder and then include that file into configuration… Thanks for your help anyway :slight_smile:

Thank you for your advice.