Shelly 3EM 3-phase Net Metering templates for Import, export and consumption

So ok now the sensors are working. I created the Utility Meter (image_1) and Riemann Sum (image_2). I attached two images to check with you if they are set correctly.

  1. Let me know it they are set correctly
  2. Just for my info…Can you describe me why we are creating these sensors and helpers?
  3. I’m ready for the next steps :slight_smile:

[image_1]

[image_2]

Not sure what you are doing the instruction are pretty clear in the first post:

Copy and paste the templates into your configuration.yaml, restart homeassistant
Configure the energy dashboard as per the photo in post 9 Energy dashboard config

You have to set the value for “kilo” in the second one. Apart from that it looks fine…
The first one I am not sure…my Italian is not particularly good, but I almost think you chose the wrong kind of helper…but I might be mistaken.

If you have that all set, then you should see the Sensors you created and be able to add them to the energy dashboard.

Edit: it looks like you chose “counter” for the first one, but it should be “consumption counter”. In my list it is the choice directly above “counter” but my UI is set to German…
Also…your should choose “Left Riemann” and jot the Trapezoidal thingy!
Also…you should give the Helpers a nice name…and you need to create one for Input and Output separately of course…:wink:

If only it was that easy. I did exactly what was suggested in the first post, but it didn’t work at all. Only after changing the syntax and creating the “helpers” via the UI did I manage to get it working in the energy dashboard…:man_shrugging:

Shouldn’t all 3 phases be calculated individually? If there is now reference to one phase and feed-in to the other phase at the same time, the sensor would only add it up and not recognize the reference, which in turn would lead to incorrect measurements, or am I seeing something completely wrong?

Net metering is sum of all phases power at every moment in time:

L1+L2+L3 = Positive you are importing(buying), Negative you are exporting(selling)

Yes but at the same time I could buy on L1 und sell und L2 but the sensor says only which I do more.

I suggest you educate yourself on how net metering works.

http://www.sonnensupportaustralia.com.au/uploads/2/9/8/5/29857561/an018_-sonnen%E2%80%93_3-ph_net_metering_overview.pdf

@uksa007 ive got my yaml configured… but im getting this error… any idea? statistics not defined.

Have you restarted homeassistant and are they reporting any data?

yes tried restarting still the same… i can see the graph in grafana… but this still getting errors on energy

Have you tried removing them from the energy dashboard, restarting home assistant and then add them?

Yeah i did that so many times, funny thing is even if i removed the grid, solar source,… somehow the solar still retains in my energy dashboard after removing all the sources. im using the latest version of HA running in OVA.

This resolved similar issues:

Remove all of the config, reboot HA, start again.
Copy and paste the scripts from the top of the thread, only modify the values for your shelly.

Hi!
Love this template and use it for about a month now.
Now my question: Is ist possible to get long-term statistics for this template?

I’ve tried to find something to do it by myself, but I think statistics for template sensors ist not possible.
Or do I just need to give the entities an ID?

Thanks in advance

The energy dashboard keeps long term stats.

hi, yes, but I have them in my dashboards and they load very slow in comparison to other sensors which use the long-term-stats now

Hello, i only want say Thank You - great work. I have look a so long time for this! :clap:

thanks suddenly it works. :slight_smile: Now how can i make this another graph but this time a realtime graph of grid export, grid import and Solar production just like in this fronius photo?

Are you able to create a sample yaml config files? thanks!

Hi @uksa007,

I just would like to thank you very much for your contribution / code.
I came across this post by chance yesterday and can only say: Awesome! It works wonderful!
This is the first time the energy panel works like it should :wink:

My devices:
HA: Home Assistant 2022.11.2
Shelly 3EM FW: 20221027-110030/v1.12.1-ga9117d3
Shelly Plug S FW: 20221027-101131/v1.12.1-ga9117d3

My setup:
Feeding a mini solar system via L2 (named “phase 2” in the further course). The measured power of the solar system is measured by the Shelly Plug S.

The code looks like this (configuration.yaml):

  - platform: template
    sensors:
    
      # Template sensor for values of power import (active_power > 0)
      power_import:
        friendly_name: "Power Import"
        unit_of_measurement: 'W'
        device_class: power
        value_template: >-
          {% if (states('sensor.phase_1_power')|float + states('sensor.phase_2_power')|float + states('sensor.phase_3_power')|float) > 0 %}
            {{ states('sensor.phase_1_power')|float + states('sensor.phase_2_power')|float + states('sensor.phase_3_power')|float }}
          {% else %}
            {{ 0 }}
          {% endif %}
        availability_template: "{{
            [ states('sensor.phase_1_power'),
              states('sensor.phase_2_power'),
              states('sensor.phase_3_power')
            ] | map('is_number') | min
          }}"

      # Template sensor for values of power export (active_power < 0)
      power_export:
        friendly_name: "Power Export"
        unit_of_measurement: 'W'
        device_class: power
        value_template: >-
          {% if (states('sensor.phase_1_power')|float + states('sensor.phase_2_power')|float + states('sensor.phase_3_power')|float) < 0 %}
            {{ (states('sensor.phase_1_power')|float + states('sensor.phase_2_power')|float + states('sensor.phase_3_power')|float) * -1 }}
          {% else %}
            {{ 0 }}
          {% endif %}
        availability_template: "{{
            [ states('sensor.phase_1_power'),
              states('sensor.phase_2_power'),
              states('sensor.phase_3_power')
            ] | map('is_number') | min
          }}"

      # Template sensor for values of power consumption
      power_consumption:
        friendly_name: "Power Consumption"
        unit_of_measurement: 'W'
        device_class: power
        value_template: >-
          {% if (states('sensor.power_export')|float(0)) > 0 and (states('sensor.shellyplug_s_b4c105_power')|float(0) - states('sensor.power_export')|float(0)) < 0 %}
          {% elif (states('sensor.power_export')|float(0)) > 0 and (states('sensor.shellyplug_s_b4c105_power')|float(0) - states('sensor.power_export')|float(0)) > 0 %}
            {{ (states('sensor.shellyplug_s_b4c105_power')|float(0)) - states('sensor.power_export')|float(0) }}    
          {% else %}
            {{ states('sensor.power_import')|float(0) + states('sensor.shellyplug_s_b4c105_power')|float(0) }}
          {% endif %}

Previously I had my configuration (unsatisfactory) as follows:

- name: "Energy Total"
      unique_id: energy_total
      state: >-
        {{ states('sensor.phase_1_energy')|float(0) + 
           states('sensor.phase_2_energy')|float(0) +
           states('sensor.phase_3_energy')|float(0) }}
      unit_of_measurement: kWh
      device_class: energy
      state_class: total_increasing
      attributes:
        last_reset: "1970-01-01T00:00:00+00:00"
      availability: >
          {{ not 'unavailable' in 
             [ states('sensor.phase_1_energy'), 
               states('sensor.phase_2_energy'),
               states('sensor.phase_3_energy') ] }}
    - name: "Power Total"
      unique_id: power_total
      state: >-
        {{ (states('sensor.phase_1_power')|float(0) + 
            states('sensor.phase_2_power')|float(0) + 
            states('sensor.phase_3_power')|float(0) |round(1)) }}
      unit_of_measurement: W
      device_class: power
      state_class: measurement
      availability: >
          {{ not 'unavailable' in 
             [ states('sensor.phase_1_power'), 
               states('sensor.phase_2_power'),
               states('sensor.phase_3_power') ] }}

The only question I have on this and maybe you can help me on this.
My Wi-Fi has a night shut-off. That means it is switched off (on weekdays) from 11pm to 5am.

With the previous code, the consumption from the Shelly 3EM was still fed into HA when the WLAN was activated. I then had a total consumption of the whole night in the morning. This is also completely correct.

With your code, the consumption is only displayed from the period in which the WLAN is active again. I think this is because the saved data is not taken over by the Shelly 3EM.

Do you have an idea for this?