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

I think there is a bracket missing in the calculation of pv_power_total. You only multiply phase 3 with -1 and not the whole term.
{{ states(‘sensor.pv_faza_1_power’)|float + states(‘sensor.pv_faza_2_power’)|float + states(‘sensor.pv_faza_3_power’)|float * -1}}

{{ (states(‘sensor.pv_faza_1_power’)|float + states(‘sensor.pv_faza_2_power’)|float + states(‘sensor.pv_faza_3_power’)|float) * -1}}

Yes your solar production is very low, as @Upsi said looks like the calculation is missing a set of brackets.

Thank you for the solution, I added missing brackets and pv_power_total looks like that:

      pv_power_total:
        friendly_name: "PV Moc Total"
        unit_of_measurement: 'W'
        device_class: power
        value_template: >-
          {% if (states('sensor.pv_faza_1_power')|float + states('sensor.pv_faza_2_power')|float + states('sensor.pv_faza_3_power')|float) < 0 %}
            {{ (states('sensor.pv_faza_1_power')|float + states('sensor.pv_faza_2_power')|float + states('sensor.pv_faza_3_power')|float) * -1 }}
          {% else %}
            {{ 0 }}
          {% endif %} 
        availability_template: "{{
            [ states('sensor.pv_faza_1_power'),
              states('sensor.pv_faza_2_power'),
              states('sensor.pv_faza_3_power')
            ] | map('is_number') | min
          }}"

but on Energy dashboard solars show me 0 kWh each day after the change. Here’s pics of entities on certain day:



What am I missing here? What should I correct or change?
Thank You in advance for any info in this case.

Hi at all, and realy thanks a lot to uksa007, great job. :slightly_smiling_face:
Perhaps you can help me with a hint how i can build/calculate additional sensors.

I have 2 Shelly 3em, one for grid and one for all my pv, both works fine. I have sensors for generating(solargen), grid import/export, and house consumption. I use your template for both shellys.

I want additional calculated sensors for “used solar power” in W, kWh and percentage and i´m a litle bit stucked in the logic how to build them. Like the data in the energy dashbaord, but as senors that i can use in external programms.

You need to start by figuring out mathematically how you get the “used solar power” figure.
Maybe just like consumption without import ike Solar gen - export

I haven’t tested this but it should give you just the Solar consumption

      power_used_solar:
        friendly_name: "Use"
        unit_of_measurement: 'W'
        device_class: power
        value_template: >-
          {% if (states('sensor.power_export')|float(0)) > 0 and (states('sensor.pv_power_total')|float(0) - states('sensor.power_export')|float(0)) < 0 %}
          {% elif (states('sensor.power_export')|float(0)) > 0 and (states('sensor.pv_power_total')|float(0) - states('sensor.power_export')|float(0)) > 0 %}
            {{ (states('sensor.pv_power_total')|float(0)) - states('sensor.power_export')|float(0) }}    
          {% else %}
            {{ states('sensor.pv_power_total')|float(0) }}
          {% endif %}
1 Like

Thanks, i will try and give you feedback :slight_smile:

works great for now, thx a lot :slight_smile:

Thanks for putting this together, took a little bit to get it to work with my configuration but now its within 0.4kwh of what AGL is reporting. Solar usage is off but my ducted uses it all anyway =D

Hi all,

really excited having found this post. I posted as instructed by “uksa007/James” into the Configuration.Yaml but when trying to configure the energy dashboard, I can’t chose “Energy Import Daily”.

What am I doing wrong?

Thanks a million for a hint!!

Not sure if you’ve solved your issue yet but I had a similar thing happen with mine, it ended up being that somewhere I was sending a string across which wasn’t able to become a value usable to the utility.

I did have to modify the code to match my shelly setup and solar values, but it does work in the end =)

I am about to install the Shelly 4PM Pro. Will I need to update firmware once I install the device? Do I need this specific firmware or will an update update work?

It’s a different device, I have never seen one, assuming the data get to HA using the Shelly integration my scripts should work the same.

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;