Benjamin Schieder


2023 March 08 | Comments

Another entry in my HomeAssistant posts. I now have a photovoltaic installation on my roof and as it is getting sunnier I have finally started delivering surplus energy to the grid. Unfortunately, the Kostal inverter does not provide that information, and I do not have a smart energy meter with it that I could query. So in absence of that I monitor the “surplus” energy I produce instead, with the template below. You can then add that to HomeAssistant’s energy dashboard as energy returned/delivered to the grid. Note that it may not be accurate, but “close enough”.

The template is here, the explanation below:

  - sensor:
    - name: "PV energy to grid"
      unit_of_measurement: "kWh"
      device_class: energy
      state_class: total
      state: >
        {% set yieldTotal = states('sensor.scb_energy_yield_total') | float %}
        {% set homeTotal = states('sensor.scb_home_consumption_total') | float %}
        {% set homeFromGridTotal = states('sensor.scb_home_consumption_from_grid_total') | float %}
        {% set batteryFromGridTotal = states('sensor.scb_battery_charge_from_grid_total') | float %}
        {% set surplus = homeFromGridTotal + yieldTotal - homeTotal - batteryFromGridTotal %}
        {% set surplus = ((surplus * 100) | int) / 100 %}
        {% set old = states('sensor.pv_energy_to_grid') %}
        {% if is_state('sensor.pv_energy_to_grid', "unavailable")%}{%set old=0|float%}{%else%}{%set old=old|float%}{%endif%}
        {% if surplus > old %}{{surplus}}{% else %}{{old}}{% endif %}

  • yieldTotal
    This is the total energy generated by your PV cells.
  • homeTotal
    This is the total energy consumed by your home.
  • homeFromGridTotal
    This is the total energy you have drawn FROM the grid.
  • batteryFromGridTotal
    Sometimes the inverter needs to charge the battery from the grid, we capture that value here.
  • surplus
    This is the calculated surplus energy that you generated, but have neither consumed nor charged your battery with it.

The surplus is calculated as homeFromGridTotal (what you pulled from the grid) plus yieldTotal (what your PV cells generated) minus homeTotal (what you consumed) minus batteryFromGridTotal (what you charged into your battery. The result is then cut off at .01 kWh precision.
Still, this may flop around a bit, giving weird displays in the energy dashboard, so lastly I compare the result to the previous value, and return whichever is higher. That way the value will not decrease.
No, you can not use state_class: total_increasing for that, as every time the value decreases HomeAssistant would treat that as a new cycle, ADDING the difference from 0 to the new value to the current value.


Category: blog

Tags: HomeAssistant SCB Kostal Photovoltaic


You can use a Mastodon account to comment on this article by replying to the associated Mastodon toot.