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

Thanks, the Shelly 3EM is correctly installed and its working fine out of script.
the template is checked again, but there are no errors from my point of view.

but, I’ve one specialised situation at home: my shelly 3em monitors all power consumption without my washing machine and the dryer, they are not covered by the 3EM.
So to include the dryer and the washing machine, I use 2 additional shelly plug s for each of them.
these two plugS were also included with their power sensors in the import template, but not in the export template (because I thought the only export which can be happened will be monitored by the Shelly 3em).

should I add the two plugS into the export template also?

This is my whole adoption of the script:

 # Template sensor for values of power import (active_power > 0)
      power_import:
        friendly_name: "Netzbezug"
        unit_of_measurement: 'W'
        unique_id: power_import
        device_class: power
        value_template: >-
          {% if (states('sensor.waschmaschine_power')|float + states('sensor.trockner_power')|float + states('sensor.shellyem3_channel_a_power')|float + states('sensor.shellyem3_channel_b_power')|float + states('sensor.shellyem3_channel_c_power')|float) > 0 %}
            {{ states('sensor.waschmaschine_power')|float + states('sensor.trockner_power')|float + states('sensor.shellyem3_channel_a_power')|float + states('sensor.shellyem3_channel_b_power')|float + states('sensor.shellyem3_channel_c_power')|float  }}
          {% else %}
            {{ 0 }}
          {% endif %}
        availability_template: "{{
            [ states('sensor.shellyem3_channel_a_power'),
              states('sensor.shellyem3_channel_b_power'),
              states('sensor.shellyem3_channel_c_power'),
              states('sensor.waschmaschine_power'),
              states('sensor.trockner_power')
            ] | map('is_number') | min
          }}"

      # Template sensor for values of power export (active_power < 0)
      power_export:
        friendly_name: "Einspeisung"
        unit_of_measurement: 'W'
        unique_id: power_export
        device_class: power
        value_template: >-
          {% if (states('sensor.shellyem3_channel_a_power')|float + states('sensor.shellyem3_channel_b_power')|float + states('sensor.shellyem3_channel_c_power')|float) < 0 %}
            {{ (states('sensor.shellyem3_channel_a_power')|float + states('sensor.shellyem3_channel_b_power')|float + states('sensor.shellyem3_channel_c_power')|float) * -1 }}
          {% else %}
            {{ 0 }}
          {% endif %}
        availability_template: "{{
            [ 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 consumption
      power_consumption:
        friendly_name: "Realverbrauch"
        unit_of_measurement: 'W'
        unique_id: power_consumption
        device_class: power
        value_template: >-
          {% if (states('sensor.power_export')|float(0)) > 0 and (states('sensor.hm_700_p_ac')|float(0) - states('sensor.power_export')|float(0)) < 0 %}
          {% elif (states('sensor.power_export')|float(0)) > 0 and (states('sensor.hm_700_p_ac')|float(0) - states('sensor.power_export')|float(0)) > 0 %}
            {{ (states('sensor.hm_700_p_ac')|float(0)) - states('sensor.power_export')|float(0) }}    
          {% else %}
            {{ states('sensor.power_import')|float(0) + states('sensor.hm_700_p_ac')|float(0) }}
          {% endif %}

Additional Info:
last couple days I was away due to holiday and there were no power consumption in my home except the base load.
what I noticed is, that every time the solar production rise (see turquoise line peaks) the power_import (called “Netzbezug”) also rises.

Yes it needs to be included in the export also, The import and export will not calculate correctly unless you include all the energy the house is using.

Why isn’t shelly clamps around the mains coming in from the street?
Using additional sensors will add more error.

Okay, I added the two shelly plugS into the export template - check that today (or tomorrow, because it will be cloudy day :frowning: )

you’re right, if I can catch all power consumption with the 3 EM, that would be the best, but its not possible. My power sub-distribution is in my ground floor und the dryer and washing machine are in the basement. the electricity meter (the only position to catch all is too far away from my network).

Just be aware that it may not be accurate all the time, as there will be measurement error and also timing issues as the sensors will update at different times, if there is a change in the loads one sensor will update before the other and so the NET import/export will not be accurate till all sensors update.

I had to install another WiFi AP in my network just for my Shelly in the meter box.

1 Like

This seems to be also true for the energy dashboard graph. Everything is rounded to 1 decimal, eg. 0,8kWh. For me this leads to the problem, that low import or export values (I guess <0.05kWh) are “disappearing” in the graph, as shown in the screenshot.
Any way to fix this behavior ?

Try changing to round: 2 in the following Sensors:

Sensor for Riemann sum of energy import (W → Wh)
Sensor for Riemann sum of energy export (W → Wh)
Sensor for Riemann sum of energy consumption (W → Wh)

Hi again man,

don´t know why is aksed this earlier but:

Is is possible that your Data will be controlled by the home assistant Date controller? I mean, i would like to see all the correct data form the past. Like the stats form 1st may or so.

Thank you!
Stephan

Hello togehter,

thanks for the templates, they work pretty well for me so far.
The only issue I have is that several times per hour the value for the sensor “power comsumption” shows “NaNw”.

To the same time there are warnings in the homeassistant.log (

2022-09-08 12:41:55 WARNING (MainThread) [homeassistant.components.integration.sensor] Invalid state ( > 139.61): [<class 'decimal.ConversionSyntax'>]

2022-09-08 12:42:42 WARNING (MainThread) [homeassistant.components.integration.sensor] Invalid state ( > 81.57): [<class 'decimal.ConversionSyntax'>]

2022-09-08 12:47:59 WARNING (MainThread) [homeassistant.components.integration.sensor] Invalid state ( > 19.84): [<class 'decimal.ConversionSyntax'>]

2022-09-08 12:48:42 WARNING (MainThread) [homeassistant.components.integration.sensor] Invalid state ( > 183.5): [<class 'decimal.ConversionSyntax'>]

2022-09-08 12:52:42 WARNING (MainThread) [homeassistant.components.integration.sensor] Invalid state ( > 339.05): [<class 'decimal.ConversionSyntax'>]

2022-09-08 13:59:12 WARNING (MainThread) [homeassistant.components.integration.sensor] Invalid state ( > 119.48): [<class 'decimal.ConversionSyntax'>]

2022-09-08 13:59:35 WARNING (MainThread) [homeassistant.components.integration.sensor] Invalid state ( > 74.38): [<class 'decimal.ConversionSyntax'>]

2022-09-08 13:59:53 WARNING (MainThread) [homeassistant.components.integration.sensor] Invalid state ( > 104.38): [<class 'decimal.ConversionSyntax'>]

Home Assitant works on a RasPi 4 2GB.

Is there any idea what is the matter for this behavior?
Thanks a lot.

The energy dashboard records over time.

How are you getting the solar power value used for consumption, that solar sensor may not be returning correct values when this issue occurs?

Thanks James for your reply.

The value is provided, every two minutes, by a “FRITZ!DECT 210” .

It also seems, that the warnings are gaining with intensity of sunshine.
So I got yesterday till 12:00 no warning

  • 12:00 - 13:00 → 5
  • 13:00 - 14:00 → 3
  • 14:00 - 15:00 → 18
  • 15:00 - 16:00 → 19
  • 16:00 - 17:00 → 11
  • 17:00 - 18:00 → 8

and till now there are no warnings anymore, but its pretty cloudy today and no solar production.

The calculation I took from here and adapted it:

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

Check sensor.solar_01_power_consumption is returning a positive number or 0 at the time the errors are occuring, it would seem that it may not be, this would be cause of your errors.

Also updating every two minutes it not really fast enough, can it update any faster?

Thank you so much !!! I´ve searched a very long time to make it right.

could some check my setting ? i’m close to jumping through a window.
i’m trying for a couple of day now but to no success

the energy dashboard seems to make it its job not to display the Energy Import Daily sensor, the Energy Export Daily is working fine.

#Shelly3EM

  - platform: template

    sensors:

      # Template sensor for values of power import (active_power > 0)

      power_import:

        device_class: power

        friendly_name: "Power Import"

        unit_of_measurement: 'w'

        value_template: >-

          {% if (states('sensor.shellyem3_485519c9a76a_channel_a_power')|float + states('sensor.shellyem3_485519c9a76a_channel_b_power')|float + states('sensor.shellyem3_485519c9a76a_channel_c_power')|float) > 0 %}

            {{ states('sensor.shellyem3_485519c9a76a_channel_a_power')|float + states('sensor.shellyem3_485519c9a76a_channel_b_power')|float + states('sensor.shellyem3_485519c9a76a_channel_c_power')|float }}

          {% else %}

            {{ 0 }}

          {% endif %}

        availability_template: "{{

            [ states('sensor.shellyem3_485519c9a76a_channel_a_power'),

              states('sensor.shellyem3_485519c9a76a_channel_b_power'),

              states('sensor.shellyem3_485519c9a76a_channel_c_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'

        value_template: >-

          {% if (states('sensor.shellyem3_485519c9a76a_channel_a_power')|float + states('sensor.shellyem3_485519c9a76a_channel_b_power')|float + states('sensor.shellyem3_485519c9a76a_channel_c_power')|float) < 0 %}

            {{ (states('sensor.shellyem3_485519c9a76a_channel_a_power')|float + states('sensor.shellyem3_485519c9a76a_channel_b_power')|float + states('sensor.shellyem3_485519c9a76a_channel_c_power')|float) * -1 }}

          {% else %}

            {{ 0 }}

          {% endif %}

        availability_template: "{{

            [ states('sensor.shellyem3_485519c9a76a_channel_a_power'),

              states('sensor.shellyem3_485519c9a76a_channel_b_power'),

              states('sensor.shellyem3_485519c9a76a_channel_c_power')

            ] | map('is_number') | min

          }}"

      # Template sensor for values of power consumption

      power_consumption:

        friendly_name: "Power Consumption"

        unit_of_measurement: 'w'

        value_template: >-

          {% if (states('sensor.power_export')|float(0)) > 0 and (states('sensor.today_s_pv_generation')|float(0) - states('sensor.power_export')|float(0)) < 0 %}

          {% elif (states('sensor.power_export')|float(0)) > 0 and (states('sensor.today_s_pv_generation')|float(0) - states('sensor.power_export')|float(0)) > 0 %}

            {{ (states('sensor.today_s_pv_generation')|float(0)) - states('sensor.power_export')|float(0) }}    

          {% else %}

            {{ states('sensor.power_import')|float(0) + states('sensor.today_s_pv_generation')|float(0) }}

          {% endif %}

  # Sensor for Riemann sum of energy import (W -> Wh)

  - platform: integration

    source: sensor.power_import

    name: energy_import_sum

    unit_prefix: k

    round: 1

    method: left

  # Sensor for Riemann sum of energy export (W -> Wh)

  - platform: integration

    source: sensor.power_export

    name: energy_export_sum

    unit_prefix: k

    round: 1

    method: left

  #Sensor for Riemann sum of energy consumption (W -> Wh)

  - platform: integration

    source: sensor.power_consumption

    name: energy_consumption_sum

    unit_prefix: k

    round: 1

    method: left

utility_meter:

  energy_import_daily:

    source: sensor.energy_import_sum

    name: Energy Import Daily

    cycle: daily

  energy_import_monthly:

    source: sensor.energy_import_sum

    name: Energy Import Monthly

    cycle: monthly

  energy_export_daily:

    source: sensor.energy_export_sum

    name: Energy Export Daily

    cycle: daily

  energy_export_monthly:

    source: sensor.energy_export_sum

    name: Energy Export Monthly

    cycle: monthly

  energy_consumption_daily:

    source: sensor.energy_consumption_sum

    name: Energy Consumption Daily

    cycle: daily

  energy_consumption_monthly:

    source: sensor.energy_consumption_sum

    name: Energy Consumption Monthly

    cycle: monthly

Do you have less than 0.1kW import, if so it may report as 0, if so you can try change round to 2.
source: sensor.power_import
round: 2

Check the following are reporting correct values:
sensor.power_import
sensor.energy_import_sum
sensor.energy_import_daily

thanks for checking :sunglasses:

That was because i was generating solar so i didn’t import power at that moment.

sensor.power_import now 1.7 kWh
sensor.energy_import_sum 10.1 kWh
sensor.energy_import_daily 1.7 kWh

the main problem is i can’t set the power import in the energy dashboard, this problem occurs only in the energy dashboard menu for some reason i can’t select power import, it doesn’t even display it. but outside of the dash no problem

You need to use Energy Import Daily and Energy Export Daily in the dashboard.

I know but there is no option to select the import daily. Only under tariffs.

Have you tried restarting home assistant?

Multiple times,

Maybe this clears some up the stats/sensors work , but not in the energy dash