Shelly EM + Utility Meter, sum the amount exported to the grid?

I have a Shelly EM with the two CT clamps, one connected to the house mains, and the other connected to the solar-pv Inverter.

when my solar array is generating more power than my house consumes, i export that exess to the grid. This i can see by the amount of “negative” kW, kWh, on the CT clamp on the house mains.

I have setup the Utility Meter in HA, which is tracking both daily and monthly usage/generation.
Is it possible to setup another utility meter to track the amount of power I export back to the grid, i.e. only track the negative kWh?

or would i have to create a custom sensor to calculate (House kWh)-(Solar kWh)= Export…
I dont know how to create a custom sensor

1 Like

Yes a template sensor would be appropriate.

- platform: template
  sensors:
    exported_enegy:
      friendly_name: 'Exported Energy'
      value_template: "{{ (states('sensor.energy_house')|float - states('sensor.energy_solar')|float)|round(3) }}"
      unit_of_measurement: "kWh"

Change the sensors in the template to match your entity ids. If you want this sort of help it is useful if you include the entity id information in future.

1 Like

Thanks, that’s a lot simpler than I expected!
I purposely kept the entity_id out to keep the question generic, but i’ll include them in the future.

1 Like

How did you track daily power generation?

the Shelly EM has two Current Transformer inputs I can use to keep track of power used and generated (Watts), as well as the energy used and generated (Watt-Hours).

One Clamp is on my Solar Panel Inverter to track how much power and energy im generating.
The other Clamp is on my mains power to track how much power and energy im importing from the grid

The energy information from the Shelly is sent to the Utility Meter which can break it down to daily energy used from the grid and daily energy generated from the solar panels.

note: I’m still working on a Appdaemon script to log the amount of energy im exporting to the grid, which is essentally just a counter of the amount of “negative-watts” on the mains current transformer intergrated over time. But things take time when im waiting for the sun to shine… currently in the middle of winter here.

After some more digging around, I found out that the Shelly EM also keeps track of the energy exported to the grid, but this is only accessible via MQTT messages.
To get this showing in HA:

  1. Set up a MQTT Broker in a docker
  2. Enable MQTT via the Shelly EM’s IP address webpage
  3. Create the MQTT Sensor in config.yaml
  - platform: mqtt
    state_topic: "shellies/shellyem-D3AD1A/emeter/0/total_returned"
    name: "Export Solar"

this sensor reports the “total returned” watt-hours.
For all the calculations I’m interested in Kilo-Watt-Hours, so I created a template sensor to divide this value by 1000

  - platform: template
    sensors:
      solar_export_corrected:
        friendly_name: Solar Export kWh
        unit_of_measurement: kWh
        value_template: "{{ (states('sensor.export_solar')|float / 1000) | round(2)}}"

now I can feed the kWh values of the newly corrected sensor into the Utility Meter functions.

utility_meter:
  daily_solar_export:
    source: sensor.solar_export_corrected
    cycle: daily
  monthly_solar_export:
    source: sensor.solar_export_corrected
    cycle: monthly

Then I use the utility meters to calculate how much credit I receive from the exported solar each day, and by the month.

  - platform: template
    sensors:
      solar_daily_earnings:
        friendly_name: Daily Solar Sold
        unit_of_measurement: $
        value_template: "{{ (states('sensor.daily_solar_export')|float * 0.102) | round(2)}}"
      solar_monthly_earnings:
        friendly_name: Monthly Solar Sold
        unit_of_measurement: $
        value_template: "{{ (states('sensor.monthly_solar_export')|float * 0.102) | round(2)}}"  

I’m sure there’s a way to combine all the above into a single entry, but it works as it is :slight_smile:

FYI: there’s a power wheel card if you are looking for a way to display this data in the frontend:

Hi Tom. I was trying to implement your solution in my HA, but I have a doubt. Let’s say that I have an energy consumption per hour of 500 Wh and my solar production’s peak is at 1.4 KWh. Since the solar production value changes during the day and the Electrical company power’s meter reads values every 15 minutes, I can’t just sum all the daily solar production and subtract the daily total energy consumption, both calculated by the HA, because the result of exported energy won’t be correct. In fact I believe that I should have something (a sensor configuration) that would analyze the difference between consumption and production at 15 minutes interval and add all the values calculated in one day. Am I right or wrong? Example: at 08:00 I have 500 Wh consumption and zero production. At 13:00 I have again 500 Wh consumption but 1.4 KWh production. The exported energy to the grid isn’t 400Wh, but 900 Wh (because the only excess of production happened only at 13:00 and not at 08:00). I hope you understand my idea/concept. I would appreciate your help to clarify it and/or come with a solution. Thanks.

Hey jfoliver13, firstly welcome to the forums!

From what you have written it seems that you might be confusing the difference between Watts (How much power is currently being used/generated ) and Watt-Hours ( how much energy has been consumed/generated over a time ), don’t worry too much i’ve done it before 1000 times too.

if you look at this image below from my solar system, i start to generate power at about 8am and there is a peak solar generation of ~1.67kW at 1:11pm

To work out the the total amount of energy (kWh) my solar system produced for the day, i need to know how much area is shaded in green under the graph, which was around 12.5kWh for the day (see graph below).

But rather than wait for the day to be completed then do a lot of very complicated maths to work out the area on such a complex curve, we can take the power value and time stamp at each measurment of the solar power generation and sum it all together and multiply it by the time interval (which is what the utility card in HA does), the shorter the time between each measurment the more accurate the kWh value will be.

Your house meter should be sampling your house usage / solar generation continously ( or at least 100’s of times per second ), it might be that your utility provider only gathers the infomation from your house meter every 15 minutes, after all they have 1000’s of homes to gather the usage/generation from.

Hi Culloden_spectre. Thank you for your quick reply.
I know the difference between power and energy, but I believe that I understand your explanation. Apparently my “mistake” was related to my lack of knowledge of what the Utility Card really does.
I was looking at your lines of code and I assumed that it would take the total of daily production energy minus the total of daily consumption and the result was the total of energy exported.
That in my understanding is incorrect, because we shouldn’t take in consideration the day average, but instead the sum of every reading (production minus consumption, every 15 minutes or every hour) in one day (if we want the energy exported in one day). So, what the green area in your beautiful graphic shows (proofs) is that the Utility Card (with your configuration) takes that in consideration and the nominal value presented is exactly the amount of energy exported to the grid. Correct?
Thanks, somehow I had the feeling that the value of energy exported in my HA Card was increasing during the first part of the day and then decreasing towards the end of the day. Optical illusion for sure.
Your explanation and support was well appreciated. Thanks.

I treat the Utility Meter sensor as a very simple calculator (SISO) which just increases its value on each cycle (thanks for the clarification tom).

The original problem I had was I didn’t know how to create a custom sensor which i could use to calculate the power I was exporting within HA’s yaml…

With Tom’s help I was able to create custom template sensor, one for power (w) and one for energy (kWh)

So for example:

From my previous graph at 1:11pm
Solar Export (w) = Solar Generation (w) - |House Consumption (w)|
Solar Export = 1678 - |-1097|
Solar Export = 581 watts

That is not how the utility meter works. It performs no integration. It simply follows the increase of the input sensor above the state at the beginning of each cycle.

If you (incorrectly) feed the utility meter a power sensor (kW) it will not output energy, the integral of power, (kWh). It needs to be fed a total energy consumed/produced sensor that always increases.

1 Like

Ah yes, my bad. I’m getting my kWh from the MQTT messages on the shelly and sending that into a utility meter.

it’s been a while since ive gone through this code :slight_smile:

1 Like

Is this still the best option to monitor exported energy into the grid with Shelly EM?

I am still using the template sensors to split out the relevant information I need, solar consumption, solar export, house consumption, grid consumption.

But with the latest firmware updates to Shelly you don’t need to enable MQTT to get the kWh of exported/consumed.

Is it still the best, no idea, but it works for me for the last number of years.

1 Like

Hi Tom,

I have a similar question.

In Shelly I have too the solar and grid. But the grid is showing the amount I spend and give to grid, not the real power consumption.

Do you know how to get it so I show in HA? I searched almost every where and I can’t figure out…

Thank you

Could you please provide your conf of your sensors? I have a shelly EM, with one clamp monitoring the production, and one clamp monitoring the house’s consumption. I am trying to create a sensors to monitor the Return_to_Grid & Consumed_from_Grid energy values. The Return_to_Grid should be a deduction of Production-Consumption provided that this number is positive. If it’s negative it should be zero. Here is my conf so far:

A sensor to monitor exported power:

template:
  - sensor:
    - name: "Exported_Power"
      unique_id: "Exported_Power"
      unit_of_measurement: "W"
      device_class: power
      state_class: measurement
      state: >-
        {% if states('sensor.shellyem_244cab419c34_channel_1_power')|float(0) - states('sensor.shellyem_244cab419c34_channel_2_power')|float(0) > 0 %}
            {{ (states('sensor.shellyem_244cab419c34_channel_1_power')|float(0) - states('sensor.shellyem_244cab419c34_channel_2_power')|float(0))*-1 }}
          {% else %}
            {{ (states('0.0') | float(0)) |round(3) }}
          {% endif %}

then a sensor to create average:

sensor:
  - platform: average  
    name: 'Avg_Exported_Energy'
    unique_id: "Avg_Exported_Energy"
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'
    entities: sensor.Exported_Power

and then a sensor to calculate the returned energy:

  - sensor:
      - name: "Grid_Exported_Energy1"
        unique_id: "Grid_Exported_Energy1"
        state: "{{ '%0.2f'|format(states('sensor.Avg_Exported_Energy')|float(0) / 1000 * (now().hour + now().minute/60)) }} "
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total

I was wondering how you created your sensors.

With a recent (since this thread started anyway) firmware update, the Shelly would keep track of positive and negative current on both clamp meters,

See screenshot from my shelly integration below

then i just needed to create a couple of tempate sensors to seperate out what i wanted

      grid_consumption:
        friendly_name: Grid Consumption 
        unit_of_measurement: W
        value_template:  >-
          {% if states('sensor.mains_power') | int > 0 %}
           {{ states('sensor.mains_power') }}
          {% else %}
            0
          {% endif %}
      grid_feed_in:
        friendly_name: Grid Feed-in
        unit_of_measurement: W
        value_template:  >-
          {% if states('sensor.mains_power') | int < 0 %}
            {{ states('sensor.mains_power') | int * -1 }}
          {% else %}
            0
          {% endif %}     
      house_consumption:
        friendly_name: House Consumption
        unit_of_measurement: W
        value_template:  "{{ states('sensor.mains_power') | int + states('sensor.solar_power') | int }}"
      solar_consumption:
        friendly_name: Solar Consumption
        unit_of_measurement: W
        value_template:  "{{ states('sensor.solar_power') | int -  states('sensor.grid_feed_in') | int }}"    

Hope this helps

I have the Shelly EM, and came across your post Zero direct usage from solar panels? All returned to grid - #8 by culloden_spectre.

It’s different in the one here in that it’s missing the first line: grid_consumption:

Regardless, where do I enter the code, in configuration.yaml? If I do I get;

Configuration warnings
Integration error: grid_consumption - Integration ‘grid_consumption’ not found.

Looking forward to assistance here, writing YAML is not for the faint hearted :wink:

Gordon,