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

Hi @uksa007;

I still use your configuration and it works great.
I would now like to add a second sensor (Shelly Plug S) to the configuration for feeding.

A simple insertion of: "and states(‘sensor.sensorname_power’)|float(0) did not work. Can you possibly help me with this?

sensor:
  - 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 %}

Thanks in advance. :innocent:

As an update I just deleted the plug, I don´t use anymore and updated it to the new version:


- sensor:
      # Template sensor for values of power import (active_power > 0)
    - name: "power_import"
      unique_id: Power Import
      unit_of_measurement: 'W'
      state_class: measurement
      device_class: power
      state: "{{ max(0,states('sensor.phase_1_power')|float + states('sensor.phase_2_power')|float + states('sensor.phase_3_power')|float )}}"
      availability: "{{
        [ states('sensor.phase_1_power'),
          states('sensor.phase_2_power'),
          states('sensor.phase_1_power')
        ] | map('is_number') | min
      }}"
      
      # Template sensor for values of power export (active_power < 0)
    - name: "power_export"
      unique_id: Power Export to grid
      unit_of_measurement: 'W'
      state_class: measurement
      device_class: power
      state: "{{ min(0,states('sensor.phase_1_power')|float + states('sensor.phase_2_power')|float + states('sensor.phase_3_power')|float)|abs}}"
      availability: "{{
        [ 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
    - name: "power_consumption"
      unique_id: Power Consumption
      unit_of_measurement: 'W'
      state_class: measurement
      device_class: power
      state: "{{ states('sensor.phases_sum')|float + states('sensor.shelly_plug_s_soyo_inverter_power')|float}}"
      availability: "{{
        [ states('sensor.phases_sum'),
          states('sensor.shelly_plug_s_soyo_inverter_power')
        ] | map('is_number') | min
      }}"

2 Likes

Hi guys,

I´m also quite new to home assistant and struggle with the Shelly import.
I´ve used below configuration, but it doesn´t seem to work.

- sensor:
         # Template sensor for values of power import (active_power > 0)
       - name: "Power NET Import"
         unit_of_measurement: 'W'
         state_class: measurement
         device_class: power
         unique_id: "Shelly Import"
         state: "{{ max(0,states('sensor.shellyem3_channel_a_power')|float + states('sensor.shellyem3_channel_b_power')|float + states('sensor.shellyem3_channel_c_power')|float )}}"
         availability: > 
          "{{
             [ states('sensor.shellyem3_channel_a_power'),
               states('sensor.shellyem3_channel_b_power'),
               states('sensor.shellyem3_channel_c_power')
             ] | map('is_number') | min
           }}"
         # Template sensor for values of power export (active_power < 0)
       - name: "Shelly NET Export"
         unit_of_measurement: 'W'
         state_class: measurement
         device_class: power
         unique_id: "Shelly Export"
         state: "{{ min(0,states('sensor.shellyem3_channel_a_power')|float + states('sensor.shellyem3_channel_b_power')|float + states('sensor.shellyem3_channel_c_power')|float)|abs}}"
         availability: > 
          "{{
             [ states('sensor.shellyem3_channel_a_power'),
               states('sensor.shellyem3_channel_b_power'),
               states('sensor.shellyem3_channel_c_power')
             ] | map('is_number') | min
           }}"
       - name: "Consumption Calculated"
         unit_of_measurement: 'W'
         state_class: measurement
         device_class: power
         unique_id: "Shelly calc"
         state: "{{ states('sensor.shelly_all_channel_power')|float + states('sensor.sma_ac_power')|float}}"
         availability: >
          "{{
             [ states('sensor.shelly_all_channel_power'),
               states('sensor.sma_ac_power')
             ] | map('is_number') | min
           }}"

If I create an entity card and add the Shelly NET Export, it shows unavailable.
Maybe you could give me some hints in the new yaml format.

Thanks,

romtin

I´ve used a workaround and added the shellyem3 energy returned channels to the enegry dashboard. Do you see any issue with this, or should it be accurate?

Thanks,

romtin

Hello,
I use the version 1.3. for a long time and it.
works quite well in conjunction with the Shelly 3EM.
However, I have large deviations when I
feed solar energy.
I have unfortunately the problem that the values in the HA take a long time to update.
The values in the cloud change every 1-2 seconds.
When I connect to the Shelly in the home network, there are changes every 5-6 seconds.
In HA it sometimes takes 30 seconds to a change.
Maybe someone has a tip and can help?

Greetings from Jürgen

So i created also my yaml in the “new” HA format… i hope this works. i´ve got many errors lately with the old config…

  1. createt a new template.yaml and included it to configuration.yaml
  2. written new yaml file with try and erroe :sweat_smile:
  3. works for now… if any one finds an error or something else what could i´ve change to get a better config please give me a hint… :slight_smile:

template.yaml:

- sensor:
#IMPORT SENSOR
  - name: aktuellerimport
    unit_of_measurement: W
    state_class: measurement
    device_class: power
    unique_id: aktuellerimport
    state:
      "{{ max(0,states('sensor.shelly3em_channel_a_power')|float +
      states('sensor.shelly3em_channel_b_power')|float +
      states('sensor.shelly3em_channel_c_power')|float )}}"
    availability:
      "{{ [ states('sensor.shelly3em_channel_a_power'),
      states('sensor.shelly3em_channel_b_power'),
      states('sensor.shelly3em_channel_c_power') ] | map('is_number') | min }}"
#EXPORT SENSOR
  - name: aktuellerexport
    unit_of_measurement: W
    state_class: measurement
    device_class: power
    unique_id: aktuellerexport
    state:
      "{{ min(0,states('sensor.shelly3em_channel_a_power')|float +
      states('sensor.shelly3em_channel_b_power')|float +
      states('sensor.shelly3em_channel_c_power')|float)|abs}}"
    availability:
      "{{ [ states('sensor.shelly3em_channel_a_power'),
      states('sensor.shelly3em_channel_b_power'),
      states('sensor.shelly3em_channel_c_power') ] | map('is_number') | min }}"
#VERBRAUCH SENSOR
  - name: aktuellerverbrauch
    unit_of_measurement: W
    state_class: measurement
    device_class: power
    unique_id: aktuellerverbrauch
    state: "{{ states('sensor.sum_channel_shelly')|float + states('sensor.shellypv_power')|float}}"
    availability:
      "{{ [ states('sensor.sum_channel_shelly'), states('sensor.shellypv_power') ]
       | map('is_number') | min }}"

configuration.yaml:

# Loads default set of integrations. Do not remove.
default_config:

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
sensor: !include sensors.yaml
template: !include template.yaml

utility_meter:
  energy_import_daily:
    source: sensor.Import
    name: Tagesimport
    cycle: daily
  energy_export_daily:
    source: sensor.Export
    name: Tagesexport
    cycle: daily
  energy_consumption_daily:
    source: sensor.Verbrauch
    name: Tagesverbrauch
    cycle: daily

sensors.yaml:

# Sensor for Riemann sum of energy import (W -> Wh)
 - platform: integration
   source: sensor.aktuellerimport
   name: Import
   unit_prefix: k
   round: 2
   method: left
# Sensor for Riemann sum of energy export (W -> Wh)
 - platform: integration
   source: sensor.aktuellerexport
   name: Export
   unit_prefix: k
   round: 2
   method: left
# Sensor for Riemann sum of energy consumption (W -> Wh)
 - platform: integration
   source: sensor.aktuellerverbrauch
   name: Verbrauch
   unit_prefix: k
   round: 2
   method: left
3 Likes

Hi,
balanced energy measuring seems to be something for specialist. ! Great work, as far as I can understand this discussion as a HASS and yaml newbie.
Is this solution also usable for using a Shelly Pro3EM, with a PV-panel pluged to one phase? With the straightforward way of integration the balancing is unaccounted, so the energy not used on the same phase is counted as ‘returned’ (in purple colour).

Good morning (o;

Just to clarify all those hundreds of posts (o;
Which version is the actual one for the 3EM?

The original from the first post seems to work, well, at least HA didn’t complain (o;

Anyone have any experience with the new Pro 3EM? Apparently with the Pro this shouldn’t be necessary anymore as far as I understood.

Filed a feature request this morning at Shelly for adding net energy support…but probably many people have done that before in the past year (o;

I have been using my original scripts with the all the versions including the latest version of Home assistant and it still works great.

I logged a feature request a long time ago, they don’t seem to go anywhere,

The Pro 3EM doesn’t seem to mention NET metering, at least not on the product page?

I just read in a product rating in an online shop here for the Pro 3EM yesterday that the net energy calculation is natively supported…guess I order one just to find out (o;

Rating translated with google (o;

“Works great with Home Assistant and the total energy entity of all three phases no longer has to be entered manually via YAML code, it is even native to this model”

Hmm…can I use the Wh returned from the PV DTU instead the wattage sensor sensor.power_solargen?
The energy generated is constantly counted up and reset at midnight.

I’m using AhoyDTU where the HM-600 not always answers, but at least I get correct Wh figures the next time it answers…

No, the sensor.power_solargen is used to calculate the live consumption in W not Wh.

But actually only import daily and export daily is used for the energy dashboard, the solar energy is already set in the dashboard from the DTU data.

And what are those monthly sensors for? The energy dashboard is already calculating weekly/monthly usage as well.

If you don’t want to calculate near instantaneous consumption then you don’t need solar_gen.
I find it incredibly useful to see what is happening with your power demand real time.
Eg did I forget to turn off the AC, or did the kids leave a hair straightener turned on.

The data in the energy dashboard was not easily accessible, for other sensors.

Ah I see…I use a second 3EM for that which measures consuimption for all three floors (each floor connected to a different phase).

Can I somehow correct the net neutrality gauge in the dashboard?

It always shows “daily import - daily export” as grid consumption, which of course is wrong.

It shows correctly what it says on the label “Net returned to the grid”

Maybe returned, but not used.
I have to pay for the energy used from the import daily sensor, not “daily import - daily export” (o;

Otherwise it would mean that the grid smart meter is counting backwards as soon I return energy…which it isn’t…

The calculation in the energy list is right though…as I can supply tariffs per daytime and per used/returned individually. Just the wording is misleading with “net usage” (o;

It is the NET or sum of the import and export.
It has nothing to do with how you are billed by the meter.

Your issues seems to be with the translation to german.

Nothing with the translation…just don’t see any advantage for a regular user displaying the difference of usage and return in the gauge…the needle in the gauge is fin3 though, telling you how efficient your pv is, even if it is that small as mine :wink:

But let’s stay on-topic (o;
My Pro 3EM is on the way to verify if that is true what a rating said…arriving hopefully tomorrow morning :wink:

Anyway…great work your script :slight_smile:
Looks more real life than before.

Morning (o;

These are the entities discovered from a Shelly Pro 3EM:


Gonna put it into my workplace electric distribution board and hook up one clamp in reverse to see how total active/returned energy is handled…and when it’s fine…it will replace one of my 3EM in the main distribution board.