How I built a HEMS with solar, a battery and a charge station (in Python)

The setup

The setup consists of an Alfen NG9xx charge station, a BYD Battery-Box Premium LVL 15.4 kWh (bought at Time Shift), a SMA Sunny Island 8.0H-13 inverter, an ABB B21–312–100 kWh meter and a few Raspberry Pi’s.

Inside the inverter
Overview of the setup with 2 PV inverters, the battery and the inverter for the battery (on the right)


Why a home battery?

The first question people usually ask when you talk about solar panels, a static battery or any other more sustainable solution is what the time you hit the point of (financial) break even.

Voltage at my grid connection

What I noticed at my grid connection, which I measure with 2Hz, is that my voltage is very volatile. It jumps all around the place when I add a load, or when I feed back in. It also just randomly jumps a few volts, maybe due to my neighbors switching on heavy loads? Who knows…

In 1 hour my voltage goes from almost 240 to almost 210 volts

Yearly zero net cost in the Netherlands

I have two PV installations, together they have a peak power production of 7000 watts (7kW). Although that is hardly ever reached.

Use cases

In this topic I’ll discuss the different use cases, or control mechanisms I’ve built in my controller. The controller runs as a Python script on a Raspberry Pi, I will go into technical details further in this blog.

Storing excessive solar energy

As mentioned in the text above, the way we can use solar energy will change rapidly in the Netherlands. Storing the energy produced during the afternoon, and using it later in the evening is the easiest way of using the battery. I just measured at my grid connection if I was feeding back into the grid, or taking from grid. And then respectively I store energy in the battery, or take energy from the battery. It’s very straight on and easy to implement. Of course this only works on sunny days, because at some point the battery is empty and the sun not shining. So you have to take energy from the grid again.

Keeping the power at grid connection at zero

Storing energy from solar into the battery means, that at some other point you’ll have to use it or your battery remains full. The way I designed the system is to have a maximum self consumption.

The battery is charged during the day and discharged during the evening to keep the grid connection at around 0 watts.

Charge (cheap) during the night

Another use case would be to charge the battery during the night, when you energy prices are usually lower. Or even a better solution, charge your battery exactly at the moment the energy prices are the lowest. In the Netherlands one of the companies that offers that solution is easyEnergy. They offer energy prices which are directly connected to the day-ahead hourly spot prices on EPEX (trading platform). The prices are the without tax and other costs (11,822 ct/kWh + 3,303 ct/kWh at the time of writing August 2020), keep in mind that taxes still make up most of the energy price.

The prices drop to 0 in the night
Negative prices during the afternoon

(Only) charge during the night when the next day will not be sunny

When I built the first two use cases, I quickly came to the conclusion that I only need to charge during the night when the next day is not going to be sunny, and thus I’ll not produce enough solar energy myself.

  • Maximum state of charge (85%, always keep room for not predicted solar energy)
  • Predicted solar energy for the next day
  • Energy need during the day (10kWh)
The (not complete) formula. batteryChargePercentage is limited between 25% and 85%.
World Solar irradiance data from Solcast
The battery starts charging at 4am, to reach a minimum of 45% SoC (which the system has predicted as the needed SoC to get through the day)

Charge with solar, extra discharge in the evening

During building the case with ‘charge during the cheapest hours’, I also thought about ‘discharge during the most expensive hours’. Although with the current energy prices (and the taxes involved), it’s only about a few cents that can be gained (and I didn’t take into account the energy losses with charging and discharging), I still built the case to show that it can be done.

In this case 19:00h seems to be the most expensive hour

Grid balancing / power quality

Using the battery for grid balance and/or power quality related challenges can be a real use case too. Changing the power from the battery from charging with 5kW to discharge with 5kW (thus a delta of 10kW) means in my case a change in voltage of more than 10 volts! That means I can control my voltage by 1V per kW.

The full setup with solar, battery and charge station

The end goal was to combine everything, so it can work together to really act as a smart home with a HEMS. The battery should first be charged with solar energy, and if the battery is fully charged (which is 95% in my case) it’ll start charging my electric vehicle. The grid should be kept at 0 watt during the day, using the energy from the battery. At the most expensive hours the battery can discharge (a bit), but that use case is only really useful when the battery is big enough. In my case, 15.4 kWh, that’s not really the case.

  • The battery will pre-charge at night when there is not enough predicted solar for the next day. It’ll charge at the cheapest hours.
  • The charge station will charge the electric vehicle at night at the cheapest hours.
  • The battery will store all excessive solar energy produced during the day in the battery.
  • The battery will keep the grid connection at around 0 watt, to be as self sufficient as possible.
  • The charge station will only charge the car with excessive solar if, and only if, the battery is 95% full and not discharging.
  • Discharging of the battery at the most expensive hours.
The SoC of the stationary battery (left) and the power on my grid connection, battery and charge station

Next steps or future use cases

Next thing I might build is, that if the car is fully charged I might enable my airconditioning to start cooling when the temperature in the room is > 22 degrees Celsius for example.

The code

I still have to clean up my code, so you’ll find my code here later on.

In-home protocols and modbus magic

ElaadNL did research the different in-home energy flexibility protocols, I suggest you to read it if you’re into in-home systems.

Did I inspire you?

If I inspired you and want to know more, work together, or have some devices you want me to integrated in my home as a pilot or proof of concept, please contact me. I’m always in for new technology and new appliances I can add and control. The easiest way is probably via LinkedIn: Harm van den Brink



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Harm van den Brink

Harm van den Brink


Cyber security, smart grids, electric vehicles, distributed ledger technology, hardware. Owner of Innoshift B.V. Articles on personal title.