Calculate return to grid from consumption and solar production

Hi all,

Very new to home assistant and while I found most answers to my setup I’m a bit stuck on a “detail”.

(Hopefully) relevant information:

I have a solar installation with a SolarEdge inverter, through the integration I get a lifetime energy production entity.
I also installed a watt-meter on my electrical circuit, AFTER the inverter but before everything else, hooked to HA giving me my total consumption (grid + solar then)

The watt-meter is configured as such:

binary_sensor:
  - platform: rpi_gpio
    invert_logic: false
    pull_mode: DOWN
    ports:
      18: ORNO521S0
      
counter:
  kwhcounter:
    initial: 0
    step: 1
    icon: mdi:home-lightning-bolt

utility_meter:
  quarter_hourly_electricty:
    source: sensor.electricity_used
    name: Quarter-Hourly Electricity
    cycle: quarter-hourly
  hourly_electricty:
    source: sensor.electricity_used
    name: Hourly Electricity
    cycle: hourly
  daily_electricty:
    source: sensor.electricity_used
    name: Daily Electricity
    cycle: daily
  monthly_electricty:
    source: sensor.electricity_used
    name: Monthly Electricity
    cycle: monthly

sensor: 
  - platform: template
    sensors:
      electricity_used:
        unit_of_measurement: kWh
        value_template: "{{ (states('counter.kwhcounter') | float / 1000) }}"
        device_class: energy

Both separately work well, I can see my consumption in the energy panel (bound to daily_electricity) and my solar production. Now given it’s winter here I could let it as-is as there’s very little chance I’ll return anything but I’d like to complete my setup correctly.

Since I don’t have anything that can provide me with the returned energy I thought about calculating the difference between my consumption and production but I don’t know how to proceed as I can’t really remove the lifetime production from the total consumption (the kwhcounter) as they were not setup as the same time and while I could reset the kwhcounter, the production value comes from SolarEdge API and I can’t reset it (nor would I want to).

So here’s where I am now and I would really appreciate any help I can get.

Thanks!

(edit: I forgot to include it but there’s a trigger on the binary sensor from off to on to increment the counter:

- id: '1641759811738'
  alias: KWH Pulse Counter
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.orno521s0
    from: 'off'
    to: 'on'
    id: kwh_pulse
  condition: []
  action:
  - service: counter.increment
    target:
      entity_id: counter.kwhcounter
  mode: queued
  max: 100

)

Aand it seems I miss-configured something as it seems HA actually sums the solar production and the electricity used:

image

The total should be 15.2kwh and the grid usage should be 9kwh :disappointed:

@hlidotbe I have the same issue.
Did you find what it was / how to correct it?

It appears the developers energy monitoring setups provide the needed inputs and therefore there is no issue for them. For my solar installation, simply putting energy measuring devices on available wires (as suggested) does not provide the needed “Return to Grid” information. My power company meter does provide this info but I have no way of extracting it. I suppose it’s may be possible to detect when my grid kwh begin to decrease ( excess solar power to grid) and do some math but wouldn’t is be far more practical to have an option for correct display with when this information isn’t readily available. Seem this is an issue for a number of users. Perhaps someone has long ago provide a solution but I haven’t run across it yet.

1 Like

I have the same issue and I am currently going down the create a Return to Grid template route. Currently need to work out how to create it so it is positive values only, i.e.
if Solar - House > 0
then Return = Solar - House
else 0
Working through the syntax… anybody know?

      value_template: >
        {% if is_state('sensor.solar_power' | float - 'sensor.grid_power' | float , '>0' ) %}
          ( states('sensor.solar_power' | float(default=0)) - states('sensor.grid_power') | float(default=0) )
        {% else %}
          0
        {% endif %}

Doesn’t seem to work…
I’ll post my similar question on another thread.
Cheers

@JollyJames Did you get this working in the end ?

Just got my soloar panels installled, and i’m seeing the same behaviour, anyone found a solution for this yet?

Following, I’m in the same situation.

I’m trying with this Integration - Riemann sum integral - Home Assistant will keep you posted.
EDIT:
there’s a section where maybe… Integration - Riemann sum integral - Home Assistant

1 Like

SECOND EDIT (CAN’T POST IT ON THE SAME COMMENT):
It’s showing promising graphs, let’s see how this works on the energy dashboard in a few hours:

have u got any solutions

The syntax should be:

value_template: >
  {% set grid_out = states('sensor.solar_power') | float(0) - states('sensor.electricity_used') | float(0) %}
  {% if  grid_out > 0  %}
    {{ grid_out }}
  {% else %}
    0
  {% endif %}
1 Like

I have the same problem and tried to get the value of ReturnToGrid with a formula from PowerIn at the electricity meter and SolarPower from the solar panel. But you can’t.

Imagine the electricity meter shows 0 W and the solar panel generates 400 W. If all consumers are switched off, the solar power goes completely into the grid (ReturnToGrid = SolarPower = 400 W). If you have a 100 W lamp, the electricity meter also shows 0 W, the solar panel still has 400 W, but ReturnToGrid = SolarPower-100 = 300 W.

As long as you cannot measure your total power consumption, you cannot calculate ReturnToGrid. Because the electricity meter no longer measures your total consumption as soon as you bring other generators into your home grid. It only measures how much electricity is taken from the electricity provider.

Yes of course, if you got 4 variables you need the values of 3 of them to calculate the 4th :slightly_smiling_face:
If you are using a smartmeter and a P1 integration, you should be able to measure grid_out.
Ans with grid_in, grid_out and solar you can calculate the home consumption.

Consider calculating return_to_grid from your two smart meter sensors power and energy, as outlined here.

Picking up an old thread.
There are 3 variables here:

1- Solar Production
2- Home Load/ consumption
3- Grid consumption

For the energy tab, HA seems to assume that 1 and 3 are the variables available and computes 2 by summing up 1 and 3.
However it is quite common to have a model where 1 and 2 are available while 3 is not easy to retrieve.

It should be possible to net off the two to compute the net grid consumption / net returned to grid i.e. 2 -1 = 3
but there does not appear to be any way to modify it

So no solve yet it sounds like :confused: