How I built a HEMS with solar, a battery and a charge station (in Python)
A Home Energy Management System (HEMS) is a system that optimizes the efficiency of a home with respect to energy. It can control the energy usage of smart appliances, the charging of stationary batteries and much more. Also, it can be used in small scale systems like microgrids.
Home storage, or storing energy at home in a static battery, is a way to store the abundance of solar energy or to charge the battery (in the night) with cheaper energy.
The storage can be grid tied (connected to the main grid of the distribution system operator) or off grid, which means it is working entirely on itself — creating its own grid with the right voltage and frequency needed — and charge by solar or/and a generator.
This blog post will focus on the grid tied, AC coupled, solution to store excessive solar energy produced on my roof and to charge during the night at cheap hours. I will show the impact of a battery at my grid connection from a power quality perspective. I’ll also show how I integrated the electric vehicle into the system, to store even more energy directly from the PV panels.
The setup is being discussed, the metering needed, the controller , the results achieved and the open source code. Keep in mind that this is a private, experimental project. This setup allows me to learn more about batteries, how to use them, what the impact is on the grid (from a power quality perspective) and the possible use cases that may arise in the (near) future.
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.
The battery is connected with 70mm2 DC cables to the inverter via a 200Amp circuit breaker, the inverter itself is directly connected to the AC grid. The battery is read out via Modbus TCP, using pyModbus. More on modbus-magic later in this blog.
The solar panels are connected with their own inverters to the AC grid, so the system setup is an AC coupled solution. This means that we use the AC grid to share power between devices, not directly DC from the solar panels into the battery.
The charge station is just a normal charge station that’s connected to my AC grid, with its own circuit breaker. Since the charging station supports load balancing via Modbus, it is integrated into the system that way.
The ABB kWh meter is placed directly at the grid connection, directly behind the (DSO) smart meter. (Why I didn’t use the smart meter? Mine only gives every 10 seconds a value at the P1 without voltage.) The ABB meter is read out using an USB-RS485-WE-1800-BT cable, connected to a Raspberry Pi and with a Python script the data is stored in an InfluxDB.
My grid connection is a one phase, 32A connection.
All devices are connected to a switch with an ethernet cable, which is connected to the internet so we can directly communicate with them.
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.
But how I look at it, is how the battery could also help from a grid perspective (to learn, and how it could help me in the future of course!) and to see what the potential business cases are. Since, if I store my own solar energy or charge the battery at night, my energy usage is less of a burden to the grid (and also cheaper for me) because I don’t take energy from the grid at peak times. Because during the night the grid usually has an abundance of capacity available and the wind also produces energy. While during the day solar panels are feeding back the unused energy at home, and the grid is kind of used like a big battery (in the Netherlands, because you can use that energy at a later moment with zero sum, but more on that later).
When your solar panels produce energy, that energy is directly used in your home but if you produce more than you use, that energy is fed into the grid. The only way to feed it into the grid is by having a higher voltage than the grid. This means that at your point of connection, your voltage will become slightly higher (I’m not taking into account smart inverters with Volt-VAR control here). If there are multiple homes with solar panels in your neighborhood and they are all connected to the same phase, chances are that at peak production time the solar inverter will switch off because of reaching the maximum voltage (~253).
On the other hand when you connect a load to the grid (let’s say in the evening), the voltage (in my case measured at the grid connection) will slightly drop. So if you connect an electric car, which adds 6kW load to the grid, this will affect your voltage. One factor for the change in voltage is the impedance of the grid, so the cable running from the transformer to your house. This can be the length of the cable and/or the size of the cable, the longer and smaller the size the more you’ll notice the change.
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…
From over 240V to less than 210V in an hour, or a jump of more than 2.5V in 1 second. I even saw sub 207V a few times, which is outside the range of allowed voltages for my grid connection (230 +- 10%, 207–253V). One way of dealing with this course could be to call my distribution system operator, another option would be to use the battery to create zero usage on my grid connection point. This would mean that the battery absorbs all energy produced by my solar panels, or feeds back into the grid (my house) where it is directly used. That’s more fun! So let’s play with it!
With making sure there is as little current as possible, with a cos(phi) close to 1 or -1, I make sure that my grid connection isn’t used. Would that make a difference in voltage? Yes, it definitely does. When I look at the impact of a load to my grid, the voltage doesn’t change that much anymore because the battery supplies that energy.
However, even when I use zero energy from the grid, and the current is 2A or less (with a really bad cos(phi)), the voltage still drops with 2 volts in just a second. That means something elsewhere in the grid is changing? Maybe my neighbors are switching on a heavy load, or on the medium voltage there is a change which leads to another conversion at low voltage. I’ll not dig into this right now, but it is something interesting to follow up in the future.
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.
I do reach peak production of 6kW quite easily, that means that at a sunny day I push back around 6kW into the grid. That’s in my opinion, also from a grid perspective, not sustainable. Because we all need it in the evening again!
In the Netherlands you can feed back into the grid and use that same amount of energy later that day (or year). Which means if you produce enough in the summer, you can have a net cost of zero in that year. This also means that batteries are not really, looking at the cost perspective, a good business case for the customer. You can just use the grid as a big battery.
But, that’s about to change. From 2023 on (the plan is to) each year 9% of the energy fed back into the grid will not be counted in the total net cost (sum of production and consumption). Your individual netto sum is lowered with 9% per year, and at 2030 the amount goes straight from 28% to 0%. Batteries (stationary or vehicle to grid) might become an interesting option for people with solar panels quite fast. Are you interested in what you can do with the battery? Keep on reading my next posts, I hope I’ll inspire you!
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.
Excessive energy production is stored, and when energy is needed from the grid the battery supplies it.
The reason you see negative power on the grid connection at 19:00h is because the use case ‘extra discharging in the evening’ is active.
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.
So during the night you pay roughly 15.1 ct per kWh, which is way cheaper than the average of 21 ct per kWh in the Netherlands. And it can be ever better, prices can also be negative!
(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.
My system has a few variables I can tune, to make sure that I only charge when there won’t be enough solar energy the next day.
- Minimum state of charge (25%, at least to make it through the morning)
- 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)
I use Solcast (Solar resource assessment and forecasting data for irradiance and PV power. Created using a global fleet of weather satellite.) to fetch the predictive solar energy for the next day. With a roughly energy need of 10kWh per day, the predictive amount is subtracted from the energy need. The rest will be stored in the battery, and thus the battery has to have room for that amount.
When the minimum state of charge is determined, the system checks at which timeslot between 1:00 and 6:00 in the night it can best charge the battery, taking into account the energy prices.
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.
The system checks the energy prices between 19:00h and 0:00h. It finds the most expensive hour, and start discharging from that time on. I’m still looking for how much energy there should be left in the battery after discharging. Currently I have this at 50%, in the image above I discharged till 35% (at around 20:30), the discharging after that is just ‘normal usage’.
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.
One case I only tried for a few days is to start charging when the voltage is above 235, and to start discharging when the voltage is below 225. This way I was able to keep the voltages between certain thresholds. It really worked out quite well for me, and at my point of grid connection. However, it’d be even better to also measure at my neighbors’ connection to see if my actions are also keeping the voltage at the right point for them.
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 system is aware of hourly kWh rates.
- 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.
In the graph above you can see that as soon as the battery hits 95% state of charge, the system starts charging the electric vehicle. The grid connection is kept at around 0 watts at all times. Yellow is the battery, a negative number means it’s charging and a positive number means it’s discharging. Blue is the charge station (it can only be positive at the moment), and positive means it’s charging.
The challenge here is that all actors must be somehow aware of each other, so that the electric vehicle is not using the energy from the battery. That could happen, because the battery looks at the grid connection and wants it to be 0 and the electric vehicle also wants the grid to be 0. Thus, if the battery is feeding energy back into my home, it could be that the grid connection shows 0 but the car is actually being charging with energy from the battery. So in my case I made sure that all devices are aware of all states, and know what each device is doing (charging, discharging or nothing). In my case I used a database and realtime read-outs to know all this, in a future design I would make this via a databus like MQTT. So every device can publish it’s state and current action (and all details needed), and others can subscribe on the devices they need to know to act accordingly.
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.
And including a heat pump for heating my home and for hot water would be the final thing to add, that would be ******* awesome! But… that’s a bit too expensive at the moment, so I’ll have to postpone that. But using a heat pump I can store excessive electricity as heat.
A vehicle to grid (V2G) enable car and charger, would also be great. I could use the combination of a stationary battery, which can charge and discharge when the car is not home, and the V2G car to do the same. The challenge here would also probably be the algorithm. When to charge and discharge what? How much SoC does every battery need to have as a minimum? All interesting future use cases!
I still have to clean up my code, so you’ll find my code here later on.
- Python script for reading out an ABB kWh meter and storing it into InfluxDB
- Python script for reading out and controlling an Alfen charge station.
- Python script for reading out and controlling a SMA Sunny Island (8.0H-13)
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.
In my case I mainly used Modbus. I connected to inverter of the battery using Modbus TCP, to the charge station using Modbus TCP and to the ABB kWh meter via Modbus Serial.
I’m partly using the SunSpec protocol to the SMA inverter, I use it to read out values like power, energy, temperature, state of charge etc. I also wanted to use it to control the inverter, but somehow I never managed to get it to work. After days and endless hours of trying, I just switched back to basic Modbus to control the inverter. And like always with Modbus, I needed some magic and a few tries to really found out how it should be controlled. Although Modbus is kinda straight forward, somehow it always requires some magic and testing to really get it working. The specifications are usually not that great, so it’s just trial & error.
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
“The more RCDs, the merrier” — Mupps