Help setting up energy to measure solar, house usage and grid feed out with a single shellyEM

Hi, I’m trying to set up the new energy thing in Home Assistant. I have attached a CT Clamp to the solar wire coming into my utility box and another CT Clamp on the wire that goes into my meter. this way I can technically measure how much energy I’m pumping into the grid by seeing if the CT Clamp measures negative current right? The issue is I don’t think home assistant knows to treat negative energy values as grid feed-back as it’s not really the best way to do this. Does anyone know of a proper way to set this up with a single ShellyEM so I can see house energy usage, solar production, and how much energy I’m pumping into the grid?

I was thinking it would be possible to make 4 separate entities from the data from the shelly, 2 for KWh and 2 for W using the equations:

house=CT Clamp on wire from smart meter (feed-in/out)
solar=CT Clamp from solar inverter

house+solar=Actual House usage
Truncate positive value from house=Actual Grid feed-in

Have you considered making two sensors one for Grid_In and one for Grid_Out. If the value is below zero it goes into Grid_in by using abs function otherwise it goes to Grid_out. You can make a third sensor to sum the two to determine net value.

I would make a change on your diagram. I would put one clamp onto HOUSE side and one on the METER side. Difference in value will be your solar production regardless of + or - values. However I am getting great help from @ardysusilo in this thread when it comes to flipping values and having them work in your favor.

I don’t believe its possible to put a CT Clamp on the house side as the solar output goes into the same breaker that’s feeding the whole house, so there are multiple cables coming out of that single breaker going to different places in the house.


from my limited knowledge of how to use the value templates I have come up with the configuration:

sensor:
  - platform: template
    sensors:
      # power used by the whole house, including solar and grid input
      house_power:
        value_template: >
          {{ (states('sensor.actual_grid_power')|float) + (states('sensor.solar_power')|float) }}
        unit_of_measurement: 'W'
        friendly_name: House Power
      # power sent to the grid from solar
      grid_power_return:
        value_template: >
          {% if (states('sensor.actual_grid_power')|float) > 0 %}
            0
          {% else %}
            {{ (states('sensor.actual_grid_power')|float) * -1 }}
          {% endif %}
        unit_of_measurement: 'W'
        friendly_name: Grid Power Return
      # power supplied/bought from the grid to the whole house
      grid_power:
        value_template: >
          {% if (states('sensor.actual_grid_power') | float) < 0 %}
            0
          {% else %}
            {{ states('sensor.actual_grid_power') }}
          {% endif %}
        unit_of_measurement: 'W'
        friendly_name: Grid Power

  - platform: integration
    source: sensor.house_power
    name: Total Energy Consumed
    unit_prefix: k
    round: 4
  - platform: integration
    source: sensor.grid_power_return
    name: Total Energy Returned
    unit_prefix: k
    round: 4
  - platform: integration
    source: sensor.grid_power
    name: Total Grid Power
    unit_prefix: k
    round: 4
  - platform: integration
    source: sensor.solar_power
    name: Total Solar Power
    unit_prefix: k
    round: 4

utility_meter:
  daily_solar_energy_generated:
    name: Daily Solar Energy Generated
    source: sensor.total_solar_power
    cycle: daily
  daily_house_energy_consumed:
    name: Daily House Energy Consumed
    source: sensor.total_energy_consumed
    cycle: daily
  daily_energy_returned:
    name: Daily Energy Returned
    source: sensor.total_energy_returned
    cycle: daily
  daily_grid_energy_consumed:
    name: Daily Grid Energy Consumed
    source: sensor.total_grid_power
    cycle: daily
    tariffs:
      - peak
      - shoulder
      - offpeak

I have no idea if this is giving me the right numbers though, I have no way to clarify the numbers I’m getting. Perhaps someone with more knowledge can look over this config and see if there are any issues with it.

Edit:
somehow grid feed-back is higher than generated solar which is not possible so something is going on

From my understanding of how the energy dashboard works I think you are close. First thing though, you are using power and not the rate of power. You need to be collecting and using either watt-hours or kilo watt- hours, consumption. The second item is, I think the dashboard is looking for values that are continuously counting up or down. Then once a hour it does some math to subtract the last value from the current value.

My formulas are similar to yours above, though I think you need to have the last collected value instead of zero in your if statements.

If someone has a better idea please let us know. Here is what I have

template:
  - sensor:
      - name: Solar Export
        state: >
        #grabs data from influxdb sensor current and subtracts previous influxdb value
         {% set export = (states('sensor.main_wh_influxdb_now') | float - states('sensor.main_wh_influxdb') | float | round(2) )%}
         #if value less than zeron then must be exporting energy
         {% if export < 0 %}
         #if exporting energy subtract influxdb value now from previous and add previous solar export sensor value ( this creates a always counting up value for the energy dashboard)
           {{ ((states('sensor.main_wh_influxdb') | float - states('sensor.main_wh_influxdb_now') | float | round(2) ) + states('sensor.solar_export') | float | round(2) )}}
         {% else %}
         #if greater than zero then solar export is equal to prevous value becuase no exportingis going on.
           {{states('sensor.solar_export') | float | round(2) }}
         {% endif %}
        unit_of_measurement: Wh
        state_class: measurement
        device_class: energy
        attributes:         
          last_reset: '1970-01-01T00:00:00+00:00'
      - name: Consumption Grid
        state: >
        #grabs data from influxdb sensor current and subtracts previous influxdb value
         {% set export = (states('sensor.main_wh_influxdb_now') | float - states('sensor.main_wh_influxdb') | float | round(2) )%}
         #if value greater than zeron then must be using grid energy
         {% if export > 0 %}
         #if using grid energy then subtract influxdb value previous from now and add previous grid consumption sensor value ( this creates a always counting up value for the energy dashboard)
           {{ ((states('sensor.main_wh_influxdb_now') | float - states('sensor.main_wh_influxdb') | float | round(2) ) + states('sensor.consumption_grid') | float | round(2) )}}
         {% else %}
         #if less than zero then grid consumption is equal to prevous value becuase no use is going on.
           {{states('sensor.consumption_grid') | float | round(2) }}
         {% endif %}
        unit_of_measurement: Wh
        state_class: measurement
        device_class: energy
        attributes:         
          last_reset: '1970-01-01T00:00:00+00:00'
      - name: solar pv energy
        state: "{{ states('sensor.solar_wh_influxdb') | float | abs | round(2) }}"
        unit_of_measurement: Wh
        state_class: measurement
        device_class: energy
        attributes:         
          last_reset: '1970-01-01T00:00:00+00:00'

i am using the integration integration to convert the W I’m getting from the sensors to kWh:

  - platform: integration
    source: sensor.house_power
    name: Total Energy Consumed
    unit_prefix: k
    round: 4
  - platform: integration
    source: sensor.grid_power_return
    name: Total Energy Returned
    unit_prefix: k
    round: 4
  - platform: integration
    source: sensor.grid_power
    name: Total Grid Power
    unit_prefix: k
    round: 4
  - platform: integration
    source: sensor.solar_power
    name: Total Solar Power
    unit_prefix: k
    round: 4

and then I’m using a utility meter to pump that data into the energy UI

utility_meter:
  daily_solar_energy_generated:
    name: Daily Solar Energy Generated
    source: sensor.total_solar_power
    cycle: daily
  daily_house_energy_consumed:
    name: Daily House Energy Consumed
    source: sensor.total_energy_consumed
    cycle: daily
  daily_energy_returned:
    name: Daily Energy Returned
    source: sensor.total_energy_returned
    cycle: daily
  daily_grid_energy_consumed:
    name: Daily Grid Energy Consumed
    source: sensor.total_grid_power
    cycle: daily
    tariffs:
      - peak
      - shoulder
      - offpeak

this probably isn’t the best way to do it, I’m no home assistant expert

Somehow I missed the “returned” entity that the shelly makes specifically to record the backflow of current through the wire.