Shelly 3EM 3-phases Energy sensor

How did you identify that it was the shelly which reported the buggy data?

Weren’t you using Riemann Sums before in your solution?

sorry my " stupid answer " : you just created template.yaml and put it into the config directory ?

For those who haven’t seen this before.
Shelly Pro 3EM
https://www.shelly.cloud/en-de/products/product-overview/pro-3-em

I use Riemann sums for the Engery provided by the template sensors that measure the power going in and out as reported by the Shelly 3EM.
However for the Engery produced by the Solar Panels I directly used the Value reported by the Energy Sensor of the Shelly 1PM that is measuring my inverter. This was the mistake. Somehow it seems the Energy Sensor of the 1PM flipped back to 0 and thus messed up all my monitoring and statistics in HA.
Advise: Use whatever you need (Riemann Sums, Utilty Meters whatever) to track the values in HA directly. Do not rely on the Shelly Devices to keep track of your Measurements forever.

I hope that makes it more clear… strickly speaking it does not belong in this thread here, as it is unrelated to the Shelly 3EM, but as you mentioned you observed wiered readings in the Energy Dashboard and I wittnessed something very similar I thought I share it here.

There is also an option to create an energy sensor with state_class: total_increasing. It ignores the reinitialization of value.

Thanks for the hint @maxym
Good Point!

@fridayAr
I think I got the same that you had today.
The Engery Dashboard is just a buggy piece of junk.
According to it my 350 VA UPS (listed as a Battery system in the Dashboard) consumed 12kWh between 13:00 and 14:00 today.
What a nonsense…
Even better:
I have two Shellys measuring what goes into the Battery (UPS and Battery Charger. Values get combined into “UPS Energy to Battery”) and I have one Shelly that measures what comes out of the battery (named "UPS Output Energy).
The difference is calculated in a sensor (named “UPS Energy Loss” and listed as Consumer in the Energy Dashboard.
That number did not explode to some rediculous 12kWh…
Furthermore the Power Consumption of the two input sensory remained normal the entire day.
And how would a Battery Charger with 160Watts max. plus a UPS with ~350Watts max consume 12kWh in 1 hour?
My own statistiks in terms of Power consumption does not show that spike.
Even the Engery Dashboards own logic is flawed…

~1 kWh from Solar
~10 kWh from the Grid
and then
~15kWh went to the Battery
??

So the only conclusion I can draw from this is:
The Values of the Shellys are rock solid. I should not have tried to fix some values in my Database on Christmas eve and kill my Database in the Process. Its just that the Engery Dashboard is buggy and thus useless and should be ignored / removed.

And just in Case someone believes its anything but the Dashboard:
Here is the growth of the “UPS Energy to Battery” Sensor that exploded by over 12kWh between 13:00 and 14:00 on the 12.01.23 according to the Dashboard:

Pretty interesting. I’ll also look into it. I’ll keep you updated if I find a solution.

I’ve noticed another problem. My 1.5kw inverter is linked to a sonoff pow r3(tasmota). I recently turned off the circuit breaker and when I turned it back on again 3 days later there was 0.7kWh instantly for that day. (dark winters :sweat_smile:)

for some reason i always get it mixed up somehow :confused:

i get all my sensor right with the shelly integration thats fine … but

this is the content of my template.yaml

- sensor:
    - name: "HausEnergy"
      unique_id: energy_total
      state: >-
        {{ 
          [ states('sensor.shellyem3_349454756d1a_channel_a_energy'), 
            states('sensor.shellyem3_349454756d1a_channel_b_energy'),
            states('sensor.shellyem3_349454756d1a_channel_c_energy'),
          ] | map('float') | sum
        }}
      availability: >-
        {{ 
          [ states('sensor.shellyem3_349454756d1a_channel_a_energy'), 
            states('sensor.shellyem3_349454756d1a_channel_b_energy'),
            states('sensor.shellyem3_349454756d1a_channel_c_energy'),
          ] | map('is_number') | min
        }}
      unit_of_measurement: kWh
      device_class: energy
      state_class: total_increasing
    - name: "HausPower"
      unique_id: power_total
      state: >-
        {{ 
          [ states('sensor.shellyem3_349454756d1a_channel_a_power'), 
            states('sensor.shellyem3_349454756d1a_channel_b_power'),
            states('sensor.shellyem3_349454756d1a_channel_c_power'),
          ] | map('float') | sum
        }}
      availability: >-
        {{ 
          [ states('sensor.shellyem3_349454756d1a_channel_a_power'), 
            states('sensor.shellyem3_349454756d1a_channel_b_power'),
            states('sensor.shellyem3_349454756d1a_channel_c_power'),
          ] | map('is_number') | min
        }}
      unit_of_measurement: W
      device_class: power
      state_class: measurement
    - name: "WaermepumpeEnergy"
      unique_id: energy_wp
      state: >-
        {{ 
          [ states('sensor.phase_1_energy'), 
            states('sensor.phase_2_energy'),
            states('sensor.phase_3_energy'),
          ] | map('float') | sum
        }}
      availability: >-
        {{ 
          [ states('sensor.phase_1_energy'), 
            states('sensor.phase_2_energy'),
            states('sensor.phase_3_energy'),
          ] | map('is_number') | min
        }}
      unit_of_measurement: kWh
      device_class: energy
      state_class: total_increasing
    - name: "WaermepumpePower"
      unique_id: power_wp
      state: >-
        {{ 
          [ states('sensor.phase_1_power'), 
            states('sensor.phase_2_power'),
            states('sensor.phase_3_power'),
          ] | map('float') | sum
        }}
      availability: >-
        {{ 
          [ states('sensor.phase_1_power'), 
            states('sensor.phase_2_power'),
            states('sensor.phase_3_power'),
          ] | map('is_number') | min
        }}
      unit_of_measurement: W
      device_class: power
      state_class: measurement

so i expect there to be 4 sensors
sensor.power_total → HausPower
sensor.energy_total → HausEnergy
sensor.power_wp → WaermepumpePower
sensor.energy_wp → WaermepumpeEnergy
with there according names

but for some reason i get


BUT

any idea why?

Not a pro, but everything looks fine to me.

Sometimes one misses the easiest things do you have any other config in another file or did you forget to restart?

Hi!
My Shelly was installed yesterday. On the shelly webinterface all values are correct but I don’t receive any data in HA in the energy entity. Do you have any idea what I can have messed up?

Is the cloud still disabled? or did you perhaps re-enabled it?

Hi! Yes the Cloud is still disabled.

Dont sum up the Energy Values of the Shelly. It will allways give you false readings even if you deduct the return to the “Grid”(or whatever flows backwards through it) from it.
See my solution above on how to get reliable current Power and Energy measurements for further processing in HA.

Regarding your sensors it seems that the sensor.phase_1_power / sensor.phase_1_energy either simply do not exist or are not providing any number.
What name did you give to the sensors of the second Shelly 3EM that is measuring your heat pump?

@fridayAr I opened a new thread for the bogus values in the Dashboard:

I had another occurance today… so statisticaly I am at ~once per week
lets see what comes out of it…

As it seems to affect various different values this is definately not down to the Shelly 3EM and I would even go so far to say for the Shellies in general, as the numbers / “Dashboard” I calculate myself are rock solid and dont show these events.

@fridayAr

Here is how my code looks at the moment. The Riemann Sensors I created just via the UI… not sure where those are stored.

# ---- Netz Total Power ---- #
- sensor:
    - name: "Netz Total Power"
      unit_of_measurement: W
      device_class: power
      state_class: measurement
      state: >
        {{ (float(states('sensor.l1_power')) + float(states('sensor.l2_power')) + float(states('sensor.l3_power'))) | round(2) }}
      availability: >
        {{ is_number(states('sensor.l1_power')) and is_number(states('sensor.l2_power')) and is_number(states('sensor.l3_power'))}}
# ---- Netz Total Power ---- #

# ---- Netz Total Power Returned ---- #
- sensor:
    - name: "Netz Total Power Returned"
      unit_of_measurement: W
      device_class: power
      state_class: measurement
      state: >
        {% if float(states('sensor.Netz_Total_Power')) < 0 %}
        {{0 - float(states('sensor.Netz_Total_Power'))}}
        {% else %}
        {{ 0 }}
        {% endif %}
      availability: >
        {{ is_number(states('sensor.Netz_Total_Power')) }}
# ---- Netz Total Power Returned ---- #

# ---- Netz Total Power Consumed ---- #
- sensor:
    - name: "Netz Total Power Consumed"
      unit_of_measurement: W
      device_class: power
      state_class: measurement
      state: >
        {% if float(states('sensor.netz_total_power')) > 0 %}
        {{float(states('sensor.netz_total_power'))}}
        {% else %}
        {{ 0 }}
        {% endif %}
      availability: >
        {{ is_number(states('sensor.netz_total_power')) }}
# ---- Netz Total Power Consumed ---- #
1 Like

Hi,
today my PV has been activated and I am a little confused.

I have a three phase system in my house and have a three phase Inverter that expose a single entity with actual power produced and daily yeld.

Shelly 3EM is attached to all three phases and currently I have values going negatives when PV is producing (I cannot understand how my huawei inverter is managing phases but the technician told me that is automatic, giving power on the phase needing it).

I have build a template sensor with the sum of the three Shelly sensors (A+B+C) and I use it to split total energy to three different timing window because my provider is charging me with different prices during the day.

Currently I have an helper with total power from the three Shelly sensors (that could go negative when I am producing) and I use this as source for the Energy meter in my configuration yaml.

In Energy Dashboard I have added these Energy meters “from grid”, the three shelly “energy returned” “to grid”, and the “inverter daily yeld” for the PV section.

Is it correct?

I have some questions that can help me to use these sensors correctly (i am writing automation to charge my electric car only when there is a surplus of energy):

  • when the value of one or more of the three sensor is negative, is that the quantity of power “compensated” from PV?
  • is there any way in my configuration to have total house power consumption (currently I do A+B+C+PV)?

Thank you
Chris

I am having trouble doing this. I added the following template for my shelley em3

- 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

and I tried to add ‘total power’ to my config, but I get this:

I noticed that my TOTALE- channel B energe seems a little different than the Yaml file. Any way to get this working? I am running

Home Assistant 2023.3.5
Supervisor 2023.03.1
Frontend 20230309.1 - latest

Anything need to be changed on the YAML file?

Here are my error logs:

2023-03-17 23:55:56.125 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration hacs which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2023-03-17 23:56:05.300 WARNING (MainThread) [homeassistant.components.sensor] Entity sensor.energy_total (<class 'homeassistant.components.template.sensor.SensorTemplate'>) is using state class 'measurement' which is impossible considering device class ('energy') it is using; expected None or one of 'total_increasing', 'total'; Please update your configuration if your entity is manually configured, otherwise create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+template%22
2023-03-17 23:56:15.856 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template("{{ states('sensor.phase_a_energy')|float + 
   states('sensor.phase_b_energy')|float + 
   states('sensor.phase_c_energy')|float }}")
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1771, in forgiving_float_filter
    return float(value)
ValueError: could not convert string to float: 'unknown'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 458, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2007, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 2, in top-level template code
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1774, in forgiving_float_filter
    raise_no_default("float", value)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1472, in raise_no_default
    raise ValueError(
ValueError: Template error: float got invalid input 'unknown' when rendering template '{{ states('sensor.phase_a_energy')|float + 
   states('sensor.phase_b_energy')|float + 
   states('sensor.phase_c_energy')|float }}' but no default was specified

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 576, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 460, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: float got invalid input 'unknown' when rendering template '{{ states('sensor.phase_a_energy')|float + 
   states('sensor.phase_b_energy')|float + 
   states('sensor.phase_c_energy')|float }}' but no default was specified
2023-03-17 23:56:15.871 ERROR (MainThread) [homeassistant.helpers.template_entity] TemplateError('ValueError: Template error: float got invalid input 'unknown' when rendering template '{{ states('sensor.phase_a_energy')|float + 
   states('sensor.phase_b_energy')|float + 
   states('sensor.phase_c_energy')|float }}' but no default was specified') while processing template 'Template("{{ states('sensor.phase_a_energy')|float + 
   states('sensor.phase_b_energy')|float + 
   states('sensor.phase_c_energy')|float }}")' for attribute '_attr_native_value' in entity 'sensor.energy_total'
2023-03-17 23:56:15.887 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template("{{ (states('sensor.phase_a_power')|float + 
    states('sensor.phase_b_power')|float + 
    states('sensor.phase_c_power')|float) }}")
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1771, in forgiving_float_filter
    return float(value)
ValueError: could not convert string to float: 'unknown'

I also looked at my sensor values and they seem to use this:
sensor.totale_channel_a_energy etc.

I got it working, FYI the entities are wrong in the examples provided here. I just replaced with correct entities and it worked. Here is a beginning config that worked for me:

- sensor:
    - name: "Munn Total Power"
      unit_of_measurement: "W"
      device_class: power
      state_class: measurement
      state: >
        {{ ([ states('sensor.totale_channel_a_power'),
              states('sensor.totale_channel_b_power'),
              states('sensor.totale_channel_c_power') ]
              | map('float') | sum) | round(2) }}
      availability: >
        {{ not 'unavailable' in
           [ states('sensor.totale_channel_a_power'),
             states('sensor.totale_channel_b_power'),
             states('sensor.totale_channel_c_power') ] }}

Here is my final config: hopefully someone can tell if I am in error for simple Kwh/Total power.

- sensor:
    - name: "Munn Total Power"
      unit_of_measurement: "W"
      device_class: power
      state_class: measurement
      state: >
        {{ ([ states('sensor.totale_channel_a_power'),
              states('sensor.totale_channel_b_power'),
              states('sensor.totale_channel_c_power') ]
              | map('float') | sum) | round(2) }}
      availability: >
        {{ not 'unavailable' in
           [ states('sensor.totale_channel_a_power'),
             states('sensor.totale_channel_b_power'),
             states('sensor.totale_channel_c_power') ] }}
- sensor:
    - name: "Munn Total Kwh"
      unit_of_measurement: "kWh"
      device_class: energy
      state_class: total_increasing
      state: >
        {{ ([ states('sensor.totale_channel_a_energy'),
              states('sensor.totale_channel_b_energy'),
              states('sensor.totale_channel_c_energy') ]
              | map('float') | sum) | round(2) }}
      availability: >
        {{ not 'unavailable' in
           [ states('sensor.totale_channel_a_energy'),
             states('sensor.totale_channel_b_energy'),
             states('sensor.totale_channel_c_energy') ] }}

This has my total Kwh and Power. If the math is wrong, I would welcome help. At least they look right.

Hello, I have this and it works fine, sometimes it reports state_class: measurement warning but it has no effect on operation

[image]

- sensor:
    - name: "Sh Energy Total"
      unique_id: sh_energy_total
      state: >-
        {{ states('sensor.shellyfaza1_red_energy')|float + states('sensor.shellyfaza2_white_energy')|float + states('sensor.shellyfaza3_blue_energy')|float }}
      unit_of_measurement: kWh
      device_class: energy
      state_class: measurement
      attributes:
        last_reset: "1970-01-01T00:00:00+00:00"
    - name: "Sh Power Total"
      unique_id: sh_power_total
      state: >-
        {{ (states('sensor.shellyfaza1_red_power')|float + 
            states('sensor.shellyfaza2_white_power')|float + 
            states('sensor.shellyfaza3_blue_power')|float) }}
      unit_of_measurement: W
      device_class: power
      state_class: measurement
    - name: "Sh Power Factor Total"
      unique_id: sh_power_factor_total
      state: >-
        {{ (((states('sensor.shellyfaza1_red_power')|float) * (states('sensor.shellyfaza1_red_power_factor')|float) +
              (states('sensor.shellyfaza2_white_power')|float) * (states('sensor.shellyfaza2_white_power_factor')|float) +
              (states('sensor.shellyfaza3_blue_power')|float) * (states('sensor.shellyfaza3_blue_power_factor')|float)) /
            (states('sensor.shellyfaza1_red_power')|float + states('sensor.shellyfaza2_white_power')|float + states('sensor.shellyfaza3_blue_power')|float))
            |int }}
      unit_of_measurement: "%"
      device_class: power_factor
      state_class: measurement
    - name: "Sh Current Total"
      unique_id: sh_current_total
      state: >-
        {{ states('sensor.shellyfaza1_red_current')|float + states('sensor.shellyfaza2_white_current')|float + states('sensor.shellyfaza3_blue_current')|float }}
      unit_of_measurement: A
      device_class: current
      state_class: measurement
    - name: "Sh Energy Returned Total"
      unique_id: sh_energy_returned_total
      state: >-
        {{ states('sensor.shellyfaza1_red_energy_returned')|float + states('sensor.shellyfaza2_white_energy_returned')|float + states('sensor.shellyfaza3_blue_energy_returned')|float }}
      unit_of_measurement: kWh
      device_class: energy
      state_class: measurement
      attributes:
        last_reset: "1970-01-01T00:00:00+00:00"