Home Assistant: Hourly Imported/Exported Energy Balance in Solar Automations

In Finland we have this so called energy balance (“tuntinetotus“) meaning that sold and bought energy are being summed up every hour (or 15 minutes, depending of the electrical company) before adding up to the electricity bill. So, if you are consuming much less energy than producing in a one hour, the rest of the energy will be sold to the energy company. However, to get most use of the self produced energy, as much as possible ‘should‘ be consumed yourself.*

An example: you are producing 1kWh of energy in the first 30 minutes and not consuming any. Then using 1kWh in the next 30min within the same hour and not producing any, total cost of the energy will be 0€. Without energy balance, you would first selling 1kWh and then buying back 1kWh and thus paying the transfer costs of the bought back energy.

This got me thinking that I would like to get realtime information of the energy balance of the current hour so I could run automations or manually trigger devices with excess balanced energy. E.g. every half an hour I check the current balance and run water heater the rest of the hour, if the balance is negative enough. Target is always to have 0 kWh balance at the end of each hour.

* Of course sometimes it’s better to sell product electricity instead of using yourself for a better profit, but this automation does not take that into consideration


For this helper a local energy production is required (obviously) and a Home Assistant integrated device that can read your consumed and sold energy in real time.

In my house I got 8.6kWp Solar system with Growatt Inverter and EM340 ModBus energy meter* where I can get realtime data of imported and exported energy.

* Guide coming later up, how to integrate WallBox Pulsar Plus + EM340 PowerBoost to Home Assistant for realtime energy information.

Setting up the Home Assistant utility meters

So, for starters we are going to create two utility meters that cycles every hour: one for import and one for export.

    name: Hourly Energy In
    source: sensor.energy_total_in
    cycle: hourly
    name: Hourly Energy Out
    source: sensor.energy_total_out
    cycle: hourly

Those two entities can now be used to check how much energy were imported and exported every hour.

Creating template energy balance entity

Next step is just to sum those up so we get the total balance that is updated realtime.

  - sensor:
    - name: "Energy balance current hour"
      unique_id: energy_balance_current_hour
      device_class: energy
      unit_of_measurement: kWh
      state: >
        {{ ((states('sensor.hourly_energy_in') | float) - (states('sensor.hourly_energy_out') | float)) | round(2) }}

That’s it! The energy balance is now visible!

The automation

Here’s an example automation how I turn on my water heater for 30 minutes, if I have enough excess solar energy from the first half an hour. My water heater is using 3000 watts of power while heating, so 30 minute heating requires 1500kWh of energy. So when ever I have 1.5 kWh of excess energy on 30 minute mark, I can turn on water heater for next 30 minutes for ‘free’. Here’s an example automation how I’m currently using it:

alias: "Energy: Water Boiler on excess solar balance"
description: >-
  Turns on water boiler for 30 minutes when solar balance exceeds 1.5kwh on 30th
  - platform: time_pattern
    minutes: "30"
  - condition: numeric_state
    entity_id: sensor.energy_balance_current_hour
    above: 1.5
  - service: switch.turn_on
    data: {}
      entity_id: switch.water_boiler
  - delay:
      hours: 0
      minutes: 30
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
    data: {}
      entity_id: switch.water_boiler
mode: single


Of course this automation is not perfect, since I’m still getting more and more energy if the sun is shining so I still end up more energy sold than used within an hour hour. Once I manage to automate my water heater system fully I’m going to change this automation to use something else..

Anyhow, simple combination of utility meters and a template sensor to do the full job. Now we are able to get realtime information of current energy balance and run automations regarding it. Should improve my total energy management by an inch at least if nothing else 🙂

Did you find this guide helpful? You can keep the blog going by bidding me a coffee!

2 Replies to “Home Assistant: Hourly Imported/Exported Energy Balance in Solar Automations”

  1. Excellent idea! I have been doing similar thing by calculating last 5 minutes rolling average power continuously. If power is negative power is fed to grid and if average is negative enough then automation kicks in one radiator in garage (1000 Watts) to use the power and balance the average.
    But this actual hourly calculation could be added quite easily to support the functionality.

    Now if I use a lot power (cook for example) for half an hour and balance is for example -1,5kWh my average thingy will still run the heating for the later half hour to prevent selling too much.

    My idea now would probably combine the both, use the rolling average for beginning of the hour and then later part of the cycle try to balance everything. Needs some thinking. Usually on sunny days heating is running all the time during day, it’s so low on power compared to garage size that thermostat only will stop the heating after midsummer. Maybe need to add also nordpool to the mix and cause some brain injury when trying to figure out the best strategy for each day. Like you wrote, some days it is worth selling the excess (expensive 8-18 and cheap night time) but on some days it’s not worth selling anything. I already use one relay to switch on the soft 0W export limit on Fronius Symo if price is negative, so it tries to match the production to house consumption and not produce any excess.

    1. Indeed it’s hurting brains when trying to think all the possible scenarios
      I’m in progress of automating all the energy usage, but it has to be done in small steps to ensure everything goes smoothly.

Leave a Reply

Your email address will not be published. Required fields are marked *