You can support me by reading this article on Medium

In the previous article we looked at how to rationally charge EVs if you have the option to install your home own charger (EVSE). But if you can install your home charger, chances are you can install a rooftop solar system too. And once you have that, a home storage battery is the logical next step.

These two investments finally let you take matters into your own hands and, combined with a heat-pump, effectively decarbonize the majority of your energy use. You will still be dependent on the grid, but instead of being dependent all the time, your dependence shrinks to 3-4 months a year, depending on the sizing and where you live. 🄳

But let’s stick to EV charging for now:

The Basic Concept

The best way to explain this is to pretend you are a farmer 😃 Your rooftop solar is a bit like a farm in that it’s dependent on the weather. When the sun is shining, your solar plant is producing kilowatts of power. When it’s heavily overcast, rainy, or at night, you get next to no production. So when you see the sun shining brightly, charge your car!

This brings all kinds of advantages:

  • The solar energy is effectively free, except for installation costs. You should recover the installation costs in 6-7 years.
  • Any energy you charge from your solar system is carbon neutral, expect for the carbon debt of the installation (I don’t like greenwashing, but this happens only once as well and with rational use, it is negligible in comparison to the avoided emissions).
  • You are actively contributing to your grid stability. Not only will you be a smaller burden on your grid in general, but with some automation, you can actively act as a good citizen and act to reduce grid load in peak times. Why push power into the grid if you can charge your car instead? Why charge your car with grid power at night if you could have taken advantage of the sun during the day?
Solar EV charging station with storage
A simple picture of the concept, credit: Electrek

The Setup

The base for this article are a solar system with storage and a controllable car charger (EVSE), connected to a Home Assistant system. I’m going to describe a solution with a hybrid inverter, but solutions with a separate power-wall will be similar, depending on the way your power-wall is controlled.

The charging logic will implemented using a Python script in AppDaemon, the rest are normal integrations, automations and entities in Home Assistant.

The Charging Strategy

There are several things to take care of here:

  • We need to supervise the total current of the installation to prevent overloads. If you have a 3x 20A home installation, you need to take care not to exceed 20A on any of the phases at any time, regardless of the resulting grid current including solar.

  • We need to prepare separate options at least for green charging and low-cost charging.

    • Green charging will only turn on when you have an excess of energy available and only charge the excess energy without taking any from the grid.
    • Low-cost charging will charge the car when the energy prices are low using all the available power.
    • You could also add an “express” mode which will give you all available power at any time while still ensuring you don’t blow any fuses.
  • We want to monitor the following at all times:

    • The phase currents. If any one of them is above the installation limit, we need to reduce charging current of the EVSE.
    • The storage battery charge level. Our strategy will depend on it.
    • The power coming from/going into the storage battery. This will help us set the EVSE charging power in the green mode.
    • The power coming from/going into the grid. This will help us avoid the grid utilization penalties.
    • Status of car connection to the EVSE. This is one of the inputs when deciding whether to turn on charging.
    • The grid utilization time-slots and limits. This is again helping us to avoid the grid utilization penalties.
    • Information about consumed energy at the EVSE. This is just to know how much charge was delivered to the car.

Limiting Currents

This is the simplest part of it all.

  1. We set a current limit, 15A in my case due to limits of the inverter. I could actually go up to 20A, but there are other things to think about outside this scope…

  2. We check the phase currents of the installation every minute.

    The interval is important: Too fast and the EVSE does not have the time to respond to current changes. Too slow and we run the risk of blowing a fuse. Fuses work relatively slowly, so you should have about 10 minutes before anything bad happens. But it’s better to do it as fast as your car can follow. You see, when the car charger sets the current, it basically tells your car what the maximum current it can take from the EVSE. The on-board charger of the car (OBC) will then honor this limit with a certain delay depending on the manufacturer. But one minute should be ok for most cars.

  3. If any of the phase currents is above the limit, we:

    1. Calculate the excess current. E.g. if we set the limit to 15A and the biggest phase current is 18A, the excess is 3A.
    2. Then we check the EVSE set curent (e.g. 10A) and subtract the excess (3A) to get the needed current setting (7A).
    3. Most cars can only charge with a minimum of 6A. So if the new current setting is lower than that, we need to turn off the EVSE for a while (e.g. 5 minutes). This will give the fuses some time to cool down.
    4. If limit is over minimum, we set the new current limit (7A) on the EVSE.
  4. Conversely, if all the phase currents are well below the limit (more than a tolerance we set, normally just the setting resolution, i.e. 1A), we do the above steps in the other direction, increasing the EVSE current limit as needed or turning it on if we had to turn it off previously.

  5. Rinse and repeat forever.

This process needs to have an absolute top priority, because it is the only part that can cause problems with our installation. Any strategy we implement will be using it.

Example Code

def currentControl(self, limit):    
    headroom = limit + CURRENT_TOLERANCE - INSTALLATION_MAXIMUM
    if headroom < 0:
      #Current too high, calculate wanted charging current to compensate
      wanted_current = int(self.set_current + int(headroom))
      if wanted_current >= MIN_CHARGE_CURRENT:
        self.setEVSE(wanted_current)
      elif not self.powerOff:
        # Turn off to cool off if no headroom
        self.powerOff = True
    elif headroom > CURRENT_TOLERANCE:
      #Current too low, calculate wanted charging current to compensate
      wanted_current = int(self.set_current + headroom)
      self.setEVSE(wanted_current)

This function will be called every minute, regardless of the chosen charging mode, as long as charging is active.

Green Charging

Now that the current limiting is out of the way, we can start thinking about green charging.

The first thing to consider is whether the storage battery is at least partially charged. It will act as our buffer when setting the charging power, taking or receiving any power we will not be able to fully compensate by setting the charger current. The storage battery voltage is not an exact measurement of charge level, but close enough. So I simply decided on the lower storage battery voltage limit to enable green charging, corresponding to about 20% of it’s capacity. I also set a hysteresis so the charging does not turn on/off repeatedly when we are near the limit. And the charging power near the limit is minimal to avoid spikes.

Next we must take care of proper turning on and off of the car charger. We do this by looking at the car connection status of the charger (in my case 1 means nothing is connected, 2 means connected but not charging, 3 means connected and charging). If the car is connected, but not charging, we should turn on the charger. If the car is not corrected, we should turn it off.

Most importantly, we need to look at the power being charged to the storage battery. This is effectively the excess solar power available and we should charge the car with it. The approach I took is simple, but works well: If the excess power exceeds a certain value (1A of charging current, i.e. 690W + losses), increase the charging current by 1A. If the excess power drops below zero (i.e. we are draining the storage battery), decrease the charging current by 1A. This approach is really simple, but it also helps to smooth out spikes that happen in the solar system due to e.g. clouds shifting quickly.

If you were careful, you might wonder how this works together with the current limiting. For simplicity I decided to adapt the current limiting function to take two values, the current control maximum and the charging logic maximum resulting from excess power.

Finally, I think it’s useful to try to speed up the charging when the sun is plentiful. So if the storage battery is more than half-full, I simply charge with full power until it gets down to half-full. This makes the charging faster.

Example Code

def doGreenCharge(self):
    # Set current to minimum at start of charge, only start if voltage high
    # enough, only start if enough headroom
    if self.battery_voltage > (LOW_BATTERY_LIMIT + VOLTAGE_HYSTERESIS):
      if (self.vehicle_state == 2) and ((MAX_INVERTER_CURRENT - self.highest_inverter_current) >= MIN_CHARGE_CURRENT):
        if (self.set_current == 0) or (self.set_current > MIN_CHARGE_CURRENT):
          self.setEVSE(MIN_CHARGE_CURRENT)
    # If the battery if full enough, charge
    if (self.battery_voltage > LOW_BATTERY_LIMIT) and (self.vehicle_state == 3):
      # Do the excess power control
      if (self.battery_power<-800) and (self.greenCurrent>MIN_CHARGE_CURRENT):
        if (self.battery_voltage < (LOW_BATTERY_LIMIT + VOLTAGE_HYSTERESIS)):
          self.greenCurrent=MIN_CHARGE_CURRENT
        else:
          self.greenCurrent=self.greenCurrent-1
      elif (self.battery_power>800) and (self.greenCurrent<MAX_INVERTER_CURRENT):
        if (self.battery_voltage < (LOW_BATTERY_LIMIT + VOLTAGE_HYSTERESIS)):
          self.greenCurrent=MIN_CHARGE_CURRENT
        else:
          self.greenCurrent=self.greenCurrent+1
      # Do full power with full storage battery
      elif (self.battery_voltage > HIGH_BATTERY_LIMIT):
        self.greenCurrent=MAX_CHARGE_CURRENT
      self.currentControl(MAX_INVERTER_CURRENT, self.greenCurrent)
def currentControl(self, limit, preference):    
    headroom = limit + CURRENT_TOLERANCE - MAX_INVERTER_CURRENT
    if headroom < 0:
      #Current too high, calculate wanted charging current to compensate
      wanted_current = int(self.set_current + int(headroom))
      if wanted_current >= MIN_CHARGE_CURRENT:
        if wanted_current > preference:
          self.setEVSE(preference)
        else:
          self.setEVSE(wanted_current)
      elif not self.powerOff:
        # Turn off to cool off if no headroom
        self.powerOff = True
    elif headroom > CURRENT_TOLERANCE:
      #Current too low, calculate wanted charging current to compensate
      wanted_current = int(self.set_current + headroom)
      if wanted_current > preference:
        self.setEVSE(preference)
      else:
        self.setEVSE(wanted_current)

Called every minute, calls adapted currentControl function to control the charge current

Results

Let’s look at a typical mostly sunny day using green charging:

The curves when the system is working
The EVSE Charging Power, the Maximum Phase Current and the Storage Battery Level When Charging on a Sunny Day

It was a sunny day and I decided to charge up the car in green mode at around 12:20:

  • You can see how the charging power ramps up quickly due to there being so much solar power available at around 12:30, around 8kW.
  • The power stays almost constant until around 13:20, when another large load (the oven) turns on. This in turn causes the charging power to drop to about 3.5kW due to current limiting until the oven is done. During this time, the system is unable to take all available power (the oven only works on one phase), so the storage battery gets charged up a bit.
  • At around 13:40 the oven thermostat turns off and the charging power ramps up again. There is another cycle of the oven thermostat with power reducing again due to current control. The oven is finally turned off at about 13:50.
  • After 14:00 the solar power begins to slowly drop as the afternoon begins. It is clear to see how the system reduces charging power accordingly until about 16:00. There are some short current spikes, but current control takes quick care of them.
  • At 16:30 the solar power finally drops to a value below minimal charging power and we can finally see a clear trend in the storage battery voltage. The minimal charging power is maintained until the shutoff limit is reached at about 18:15 when the charging shuts off for the day.

This was in late October, so I was able to charge only 11.6kWh that day (my personal best was 39,6kWh, almost 2/3 of my Kona), but this is still enough charge to do two errands. It was free in all important aspects—I didn’t have to pay anything, it caused no carbon emissions and the power control prevented unnecessary cycling of the storage battery. It is a win-win-win šŸ˜€ If I needed more charge that day, I could still simply turn on the cheap charging option and charge to full at night. But it turns out that with my driving, the green option is all I need from about March to about October.

What If You Don’t Have Storage?

This same approach would still work if you didn’t have a storage battery in your solar system. Instead of looking at the storage battery charging power you would have to look at grid power, but all the rest would be the same.

The only difference is that you would be using the grid as your battery. This means you are not worried about wear on the battery, but you can still be a good citizen and reduce your usage during the maximum grid load times. Sadly, the old-style net-metering with constant energy price does not give you any financial incentive to do that, but I’m assuming this will change soon.

Conclusion

This was a short description of the principles of EV charging control in a smart home installation with solar, storage battery, and a car charger. As you can see, home automation is an interesting way to reduce your carbon footprint, save energy (costs), and reduce grid load.

To keep thing simple, I did not discuss the grid utilization limit handling here. If there is interest, I’ll write a separate article about that.

The code examples are only snippets and intended as an illustration of principle. If you are interested in details, add a comment below and let’s discuss!

If you found this article useful, you can buy me a beer here.