Warning: This project is in alpha status. Expect inconsistencies in the code and naming conventions, bugs and missing docstrings. I am a hobby programmer Use at your own risk! There are inconsistensies in the naming conventions of the variables a mix of German and English. Also this code was created with the help of AI and therefore should not be used for AI traning.
- π¨ Disclaimer
- SmartCharge πβ‘
- π Features
- Prerequisites
- π οΈ Installation
- π Usage
- π§ How It Works
- π€ Contributing
- π License
Welcome to SmartCharge, a smart charging solution for electric vehicles (EVs) that integrates multiple load points and home battery systems. This program optimizes your EV charging schedule based on solar production forecasts, weather conditions, electricity prices, and home energy consumption. ππ§οΈπ‘
Using evcc's api it sets up your car trips (using a schedule) and loads the car to the minimum amount possible using the cheapest energy price possible taking into consideration future charges from PV, energy consumption of the car considering the trip lenght and the temperatures. PV charge is estimated with solcast and added to the EV. Remaining energy is used to "cache" this in the home battery. Also the energy consumption of the house is estimated by multiple factors. This energy is precharged into the battery if this is economically resonable - of course at cheapest cost possible.
- create a web interface using websockets to have the data available in real time and also to make setup of trips without danger of error
I highly depend on your participation now. Before creating pull requests please open an issue and let me assign the issue to you to make sure that not multiple people are working on the same function. There is a lot to do:
- testing
- looking at the TODO: / FIXME: / BUG: comments here and in the code
- Multiple Load Point Support: Manage charging for multiple EVs simultaneously.
- Home Battery Integration: Optimize charging based on home battery status and capacity.
- Solar Forecasting: Utilize solar production forecasts to prioritize charging when solar energy is abundant. π
- Weather Integration: Adjust charging plans based on weather conditions. β
- Electricity Price Optimization: Schedule charging during off-peak hours to save on electricity costs. π°
- evcc Integration: Seamlessly integrate with evcc (Electric Vehicle Charge Controller) GitHub Link / Non GitHub link
- Webserver: Edit trips using the web interface
- PV installation
- home battery
- Python
- evcc
- InfluxDB (also set up in evcc)
- A Solcast account with your photovoltaic (PV) system set up. You can create an account and set up your PV system here.
- An OpenWeather account to retrieve weather data. You can create an account and get your API key here.
- a contract with tibber and your [acces token] (https://developer.tibber.com/settings/accesstoken), alternatively: integrate another source for energy prices such as Fraunhofer or Awattar - see - Contributing
- a fake loadpoint and charger to be able to lock the home battery with a "quick and dirty" trick: https://github.com/evcc-io/evcc/wiki/aaa-Lifehacks#entladung-eines-steuerbaren-hausspeicher-preisgesteuert-oder-manuell-sperren
- heatpump set up with SG Ready (https://docs.evcc.io/docs/faq#heizstab--w%C3%A4rmepumpe)
- if not set up: you will just get an error message - the program keeps operable
- furthermore the relays need to react to different conditions. For the Smart Grid operations 1/0 is "boost light", it must react to the evcc MQTT api publish on
- 0/1 for block modes
- TODO: [high prio] - detailed description, link to my shelly scripts
If these instructions say sudo
do so. If not, do not!
Follow these steps to set up SmartCharge on your system:
You may not be able to use git
and pip
. If you encounter this problem: sudo apt-get install -y git pip
git clone https://github.com/Coernel82/smartCharge4evcc.git
cd smartCharge4evcc
To update to new versions: git pull origin main
It's recommended to use a Python virtual environment to manage dependencies:
sudo apt update
sudo apt install python3-venv
python3 -m venv myenv
source myenv/bin/activate
You may replace myenv
to your liking.
To deactivate / leave the virtual environment simply use deactivate
Don't do that now!
pip install -r requirements.txt
mv settings_example.json settings.json
- edit the settings via Webserver
<your-ip>:5000
after your webserver is running
The webserver is a Flask-Server which should only be run in your private network as it is not safe to open it to the internet. The server is included in /www/server.py
Create a bash
Activate your virtual environment and run the smartCharge.py
script:
source myenv/bin/activate
python smartCharge.py
To keep SmartCharge running continuously, restart it automatically if it crashes, and start it on boot, you can set it up as a systemd service. The script loops itself. Every ten minutes the SoC of the home battery is checked, every hour the calculations are done.
Create a new service file to run SmartCharge and its server in the virtual environment:
nano run_smartcharge.sh
and paste this:
#!/bin/bash
# switching to the working directory
cd /home/evcc-admin/smartCharge4evcc
# Activate virtual environment
source /home/evcc-admin/myenv/bin/activate
# run both scripts simultaniously by using the &-sign
python /home/evcc-admin/smartcharge4evcc/backend/smartCharge.py &
python /home/evcc-admin/smartCharge4evcc/www/server.py &
# wait till the scripts finish (they never should)
wait
# Deaktivieren der virtuellen Umgebung
deactivate
Make it executable: chmod +x /home/evcc/run_smartcharge.sh
Then make this a system service:
sudo nano /etc/systemd/system/smartcharge.service
Paste the following content into the file:
[Unit]
Description=SmartCharge Service
After=network.target
[Service]
User=evcc-admin
WorkingDirectory=/home/evcc-admin/smartCharge4evcc
ExecStart=/home/evcc-admin/run_smartcharge.sh
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
Note: Replace /home/evcc-admin/smartCharge4evcc
and evcc-admin
with your actual installation path and username if they are different.
Reload systemd to recognize the new service:
sudo systemctl daemon-reload
Enable the service to start on boot:
sudo systemctl enable smartcharge.service
Start the SmartCharge service:
sudo systemctl start smartcharge.service
Check the status of the service to ensure it's running:
sudo systemctl status smartcharge.service
You should see that the service is active and running.
#### 5. View Service Logs
To view the logs for the SmartCharge service, use:
```bash
sudo journalctl -u smartcharge.service
- The
Restart=always
option ensures that the service restarts automatically if it stops or crashes. - The
RestartSec=5
option sets a 5-second delay before the service restarts. - Ensure that your Python virtual environment and paths are correctly specified in the
ExecStart
directive.
SmartCharge intelligently schedules your EV charging by considering several factors:
- Get many pieces of information from APIs π»
- energy forcast from Solcast
- weather forcast from Openweather
- settings from evcc
- Calculate the energy consumption of your house in hourly increments
- using the value of the energy certificate of the house the energy consumption is calculated:
x kWh / ΞK / mΒ² / year
. Break it down to an hour/365/24
- apply a correction factor: heating energy comes for free through your windows when the sun is shining. I estimate the energy by a correction factor: Normalize the prognosed yield of the pv by dividing through the kWP value of your pv. So the incoming radiation through the windows is somehow proportional to your PV yield. In another function the real energy used for heating and the calculated are compared and the correction factor gets adapted to make this prognosis more precise. For this I write the real and the calculated values to InfluxDB
- using the value of the energy certificate of the house the energy consumption is calculated:
- Substract baseload and heating energy:
- the baseload also comes from InfluxDB after it has run for some weeks. It is calculated over 4 weeks per day of the weeks and in hourly increments. So for every hour
(Monday1 + Monday2 + Monday3 + Monday4) / 4 = baseload
- we have a value containing the remaining energy per hour
- the baseload also comes from InfluxDB after it has run for some weeks. It is calculated over 4 weeks per day of the weeks and in hourly increments. So for every hour
- Calculate energy needed for ev
- we have trip data in a json for recurring and non recurring trips.
- (delete old non recurring trips takes place somewhere in the program as well)
- we have a total degradated battery capacity which we calculate by mileage
- get weather data for departure and return
- calculate energy consumption for return and departure trip and take into consideration departure and return temperature (complicated gauss formula derived from a graph - link to graph in source code)
- "load" energy to the ev with the remaining pv energy (i.e. reserve this for the vehicle)
- Calculate loading plan for ev
- the energy which can not be loaded till departure by solar energy has to be charged at cheapest cost:
- calculate the charging time at the loadpoint for this amount of energy
amount / speed = time
- filter energy prices from
now till departure
- sort energy prices from
low to high
- iterate through them till
time (in hours) = number of iteration
. Return the price at that hour and post it to evcc
- calculate the charging time at the loadpoint for this amount of energy
- the energy which can not be loaded till departure by solar energy has to be charged at cheapest cost:
- Store remaining energy in home battery (= reserve it)
- Now we have a thorough energy profile which also has energy deficits for the home battery but also might have grid feedin (what we cannot do anything about as we have used the energy to the maximum possible)
- Calculate charging costs of home battery
- consider efficiency:
charging cost = charing costs * (1/efficiency)
- consider wear and tear: break down purchase price to Wh for battery and inverter:
charging cost = charging cost + wear and tear
- consider efficiency:
- Charge battery when charging and using charged energy is still cheaper then grid energy
- for every hour compare: how much energy is needed?
- is charging beforehand (with losses, see above) cheaper:
- sum up the energy need for all the times where charging beforehand is cheaper
- calculate charging time
amount / speed = time
- iterate as above with the loading plan for the ev
- set cheapest price via evcc api
- this can charge a bit more than needed as evcc does not support a "stop soc"
- utils.py: Helper functions for calculations and data handling.
- initialize_smartcharge.py: Loads settings and initializes the application.
- smartCharge.py: The main script that orchestrates the charging schedule.
- vehicle.py: Handles vehicle-specific calculations like energy consumption and SOC (State of Charge).
- home.py: Manages home energy consumption, battery status, and interactions with home devices.
- solarweather.py: Fetches and processes weather and solar data.
- evcc.py: Interfaces with the EVCC API to set charging parameters.
- settings.json: Configuration file containing API keys and user settings.
- usage_plan.json: User-defined schedule for vehicle usage.
- www: the webserver directory
Contributions are welcome! Please fork the repository and create a pull request. For major changes, please open an issue first to discuss what you would like to change. π οΈ
MIT
Enjoy smart charging! If you encounter any issues or have suggestions, feel free to open an issue on GitHub. π