Issues on Shelly 3EM and HA Riemann Integration / Utility Meter

Hi guys,

I’m using Home Assistant for about 5 months now and I’m really overwhelmed about the possiblities it gives to me. First I want to describe what my project is about and later I will point out my problem.

I’ve installed a solar system (Inverter is a Hoymiles HM 1500) and a shelly 3EM and I’m trying to use HA to check all the values.

I’ve integrated the shelly into HA using the official integration and it works good and delivers values.

As you know the shelly only gives you the values of how much energy is consumption or feed in kWh for each phase or the actual power in W for each phase.
In my configuration positive values of Watt are consumption and negative values are feed.

In order to differentiate how much of my solar energy I used by myself and how much is going into the net I created some templates.

First I sum up all 3 Phases in Watt like this:

      shelly_gesamt_power: 
        friendly_name: "Gesamtleistung_Shelly" 
        device_class: power
        unit_of_measurement: 'W' 
        value_template: "{{ (states('sensor.phase_1_power') | float + states('sensor.phase_2_power') | float + states('sensor.phase_3_power') | float) |round(3) }}"

and also sum up the energy like this:
for feed:

      shelly_einspeisung_geraet: 
        friendly_name: "Netzbezug_Shelly_Gerät" #Anzeigename für den Sensor
        device_class: energy #die Messgröße des Sensors
        unit_of_measurement: 'kWh' #Einheit des Sensors
        # Value Template ist die Berechnungvorschrift des Sensors
        value_template: "{{ (states('sensor.phase_1_energy_returned') | float + states('sensor.phase_2_energy_returned') | float + states('sensor.phase_3_energy_returned') | float) |round(3) }}"

for consumption:

      shelly_netzbezug_geraet: 
        friendly_name: "Einspeisung_Shelly_Gerät" #Anzeigename für den Sensor
        device_class: energy #die Messgröße des Sensors
        unit_of_measurement: 'kWh' #Einheit des Sensors
        # Value Template ist die Berechnungvorschrift des Sensors
        value_template: "{{ (states('sensor.phase_1_energy') | float + states('sensor.phase_2_energy') | float + states('sensor.phase_3_energy') | float) |round(3) }}"

In my opinion when the sum of Watt is positive there is a comsumption if the value is negative there is a feed.

In a second step I created two template sensor two differiated between positive and negative values or consumption and feed.

for consumption:

      netzbezug_shelly_cal:
        friendly_name: "Netzbezug_shelly_cal [W]"
        unit_of_measurement: 'W'
        device_class: power
        value_template:  >
          {% if (states('sensor.shelly_gesamt_power') | float) > 0 %}
            {{ (states('sensor.shelly_gesamt_power') | float ) | round(2) }}
          {% else %}
            {{ 0.0 }}
          {% endif %}

for feed:

      einspeisung_shelly_cal:
        friendly_name: "Einspeisung_shelly_cal [W]"
        unit_of_measurement: 'W'
        device_class: power
        value_template:  >
          {% if (states('sensor.shelly_gesamt_power') | float) *-1 > 0 %}
            {{ (states('sensor.shelly_gesamt_power') | float )*-1 | round(2) }}
          {% else %}
            {{ 0.0 }}
          {% endif %}

As you know the riemann integration got problems if the source kepps the value “0” for longer time, so I created a substitute sensor which randomly add a vaule between 0.1…1 (sensor.zufall) to the source if the value is “0”.

for consumption:

      netzbezug_shelly_cal_ersatz:
        friendly_name: "Netzbezug_shelly_cal_ersatz [W]"
        unit_of_measurement: 'W'
        device_class: power
        value_template:  >
          {% if states('sensor.netzbezug_shelly_cal') | float > 0 %}
            {{ states('sensor.netzbezug_shelly_cal') | float | round(2) }}
          {% else %}
            {{ states('sensor.zufall') | float / 10 | round (2) }}
          {% endif %}

for feed:

      einspeisung_shelly_cal_ersatz:
        friendly_name: "Einspeisung_shelly_cal_ersatz [W]"
        unit_of_measurement: 'W'
        device_class: power
        value_template:  >
          {% if states('sensor.einspeisung_shelly_cal') | float > 0 %}
            {{ states('sensor.einspeisung_shelly_cal') | float | round(2) }}
          {% else %}
            {{ states('sensor.zufall') | float / 10 | round (2) }}
          {% endif %}

After that I created riemann sum integrations of the consumption and the feed:

  - platform: integration
    source: sensor.netzbezug_shelly_cal_ersatz
    name: Netzbezug_Shelly_cal_int
    unit_prefix: k
    round: 3
    unit_time: h
    method: trapezoidal

  - platform: integration
    source: sensor.einspeisung_shelly_cal_ersatz
    name: Einspeisung_Shelly_cal_int
    unit_prefix: k
    round: 3
    unit_time: h
    method: trapezoidal

After all I created some utility meters for my using the GUI with a daily reset interval. I’ve created the utility meters for the energy sensors given by the 3EM and also for my self created templates. In my opinion the results should be the same but they are not.

I’ve seen that there maybe is a relation to the power the solar system is producing.

The picture is just for the power consumption but the feed looks similar.
The values given by the shelly are always higher than my calculated values. I am observing this for some days now. Even if there is no power consumption the utility meter for the shelly energy got a little positive gradient. The utility meter for the calculated values is exactly horizontal, as it should be.

Has anybody of you made similar experiences with the shelly 3em or the riemann sums / utility meters?
Can you see mistakes I made in my sensors?

I apologize for my bad english respectively not finding the right words to describe my problem due to I’m not a native english speaker.

Due to that I am a new user I can upload only one picture. Sorry.

What have I tried:

  • changed riemann sum integrations from kWh to Wh – > same result
  • tried different methods of integration left, right, trapez → same result, trapez is exact the mean value of left and right as it should be
  • using MQTT to get the values from the Shelly 3EM → same result
  • set the MQTT update interval to 1s → same result

Thanks for your help.