Shelly3EM with Energy and MQTT

Hi,

I have a Shelly 3EM configured in Home Assistant Core with MQTT like this:

sensor:
  - platform: mqtt
    name: "Shelly 3EM Phase 1 Power"
    state_topic: "shellies/shellyem3-DC4F227649A1/emeter/0/power"
    unit_of_measurement: "W"

  - platform: mqtt
    name: "Shelly 3EM Phase 1 Volt"
    state_topic: "shellies/shellyem3-DC4F227649A1/emeter/0/voltage"
    unit_of_measurement: "V"

  - platform: mqtt
    name: "Shelly 3EM Phase 2 Power"
    state_topic: "shellies/shellyem3-DC4F227649A1/emeter/1/power"
    unit_of_measurement: "W"

  - platform: mqtt
    name: "Shelly 3EM Phase 2 Voltage"
    state_topic: "shellies/shellyem3-DC4F227649A1/emeter/1/voltage"
    unit_of_measurement: "V"

  - platform: mqtt
    name: "Shelly 3EM Phase 3 Power"
    state_topic: "shellies/shellyem3-DC4F227649A1/emeter/2/power"
    unit_of_measurement: "W"

  - platform: mqtt
    name: "Shelly 3EM Phase 3 Voltage"
    state_topic: "shellies/shellyem3-DC4F227649A1/emeter/2/power"
    unit_of_measurement: "V"

  - platform: template
    sensors:
      shelly_3em_power_total:
        friendly_name: "Total Consumption"
        value_template: "{{ (states('sensor.shelly_3em_phase_1_power') | float + states('sensor.shelly_3em_phase_2_power') | float + states('sensor.shelly_3em_phase_3_power') | float) / 1000 }}"
        unit_of_measurement: 'kWh'

What do need to do to be able to add this to the new Energy feature in HA?
When I try to add “Grid consumption” I only get "No matching statistics found.

I read in the forums that I needed to add the following to my “Total Consumption” for HA to recognize it as an energy entity:

        last_reset: '1970-01-01T00:00:00+00:00'
        state_class: measurement
        device_class: energy

But when I do I get the following error:

ERROR (MainThread) [homeassistant.config] Invalid config for [sensor.template]: [attributes] is an invalid option for [sensor.template]. Check: sensor.template->sensors->shelly_3em_power_total->attributes. (See /config/configuration.yaml, line 481). Please check the docs at Template - Home Assistant

So does anyone know what I’m doing wrong here?

        state_class: total_increasing
        device_class: energy

No last_reset. That was removed in 2021.9

wow that was fast, thanks!
However I get the following error now:

ERROR (MainThread) [homeassistant.config] Invalid config for [sensor.template]: [state_class] is an invalid option for [sensor.template]. Check: sensor.template->sensors->shelly_3em_power_total->state_class. (See /config/configuration.yaml, line 481). Please check the docs at Template - Home Assistant

sensor:
  - platform: template
    sensors:
      shelly_3em_power_total:
        friendly_name: "Total Consumption"
        value_template: "{{ (states('sensor.shelly_3em_phase_1_power') | float + states('sensor.shelly_3em_phase_2_power') | float + states('sensor.shelly_3em_phase_3_power') | float) / 1000 }}"
        unit_of_measurement: kWh
        device_class: energy
        state_class: total_increasing

Your template sensor is incorrect. You can not add up powers (kW) to get energy (kWh).

Find the energy sensors and add them. You also have to reduce the total by a factor of SQRT(3) as the powers are out of phase.

See this post:

ah thanks, I will look in to that!

Or do it the easy way and use the Shelly integration. You just have to set this for it to work:

Never tried that integration before. It works flawlessly! Thank you very much.
Now I just need to find a way to get a “Total” value too but I think I saw someone in the forums that did just that.

Thanks again for the quick reply!

A link to the total template is in my post above.

ah that’s where I saw it.
Just one last question then. Where do I put this?

If I put this in configuration.yaml it says: Setup failed for consumed_energy: Integration not found.
So I’m guessing I should put this under sensors: somehow but I can’t figure out how.

sensor:
  - platform: template
    sensors:
      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') ) }}"

I’m now far from sure that you should divide with SQRT(3) to get the right consumption. Got an hourly .csv file from my electric company and it showed actually that just summing should be the way to go.

The difference is ~70% so it should be quite easy to see the right amount. In my case I’m a little bit worried that my Solar inverter might mess something up since I got very bad power factor values according to EM3 (as low as 25% on one phase).

I own a Shelly 3EM for about a month now and yesterday I had that spike problem.
The topic was discussed here and in some other threads but I think there are situations not taken into account.
Earlier here you talked about inconsistent connections but this also happens if I reboot the 3EM or if I have to reboot my WLAN APs because of a firmware update, which lead to my first meeting with this issue yesterday.
So in real life a stable connection is what you want but you have events where you have to disconnect the device.
There must be a better solution than switching to MQTT for example and take care of filtering that on your own or creating own sensors trying to do what a utility_meter does with just a few lines of code just to get rid of events that occur sooner or later in every installation.
I have to restart HA on every update, on some config changes, this is a living system. Also I have to update my APs or I have a power outage of an AP. And this will break all of my statistics?

Ok well I found a fairly simple solution for now based on all the threads.
I added one “filtered” template sensor to filter unwanted values from the Shelly 3EM real sensor:

template:
  - sensor:
    - name: nh_energy_channel_a_energy_filtered
      unique_id: nh_energy_channel_a_energy_filtered
      unit_of_measurement: kWh
      device_class: energy
      state_class: total_increasing
      state: >
        {% set state = states('sensor.nh_energy_channel_a_energy') %}
        {% if (is_number(state)) and state | float(0) > 0 %}
            {{ state }}
        {% else %}
            {{ None }}
        {% endif %}
      availability: >-
        {{
          is_number(states('sensor.nh_energy_channel_a_energy'))
        }}

I created this for each channel. I am going to bind this “filtered” sensor now to my utility meters which should then compensate what happens when the 3EM get rebooted or something else happens…