This project provides an API for optimizing battery schedules using MQTT-based forecasts and a linear optimization algorithm.
- Method:
POST - Content-Type:
application/json - Description: Optimizes the battery schedule based on the provided current state of charge and forecast data fetched via MQTT.
{
"current_soc_percent": 50, // Current state of charge in percentage
"current_time_index": 12, // Current time index (e.g., hour of the day)
"battery_params": { // Optional: Custom battery parameters
"capacity_kwh": 7.4,
"max_rate_kw": 0.8,
"min_soc_percent": 10,
"efficiency_roundtrip": 0.90
}
}- 200 OK: Optimization successful, returns the next action and estimated savings.
- 400 Bad Request: Missing or invalid input fields.
- 503 Service Unavailable: Failed to fetch forecast data.
- 500 Internal Server Error: Optimization or MQTT publishing error.
Example Response:
{
"solver_status": "Optimal",
"action_next_hour": 0.5,
"estimated_total_savings": 12.34,
"mqtt_publish_status": "Success"
}The application uses the following environment variables for configuration:
| Variable | Default Value | Description |
|---|---|---|
MQTT_BROKER |
localhost |
MQTT broker hostname or IP address. |
MQTT_PORT |
1883 |
MQTT broker port. |
MQTT_TOPIC_FORECAST |
forecast/topic |
MQTT topic for receiving forecast data. |
MQTT_TOPIC |
battery/schedule/optimal |
MQTT topic for publishing optimized schedules. |
MQTT_USERNAME |
None | MQTT username for authentication (optional). |
MQTT_PASSWORD |
None | MQTT password for authentication (optional). |
MQTT_BROKER=192.168.1.100
MQTT_PORT=1883
MQTT_TOPIC_FORECAST=forecast/topic
MQTT_TOPIC=battery/schedule/optimal
MQTT_USERNAME=user
MQTT_PASSWORD=passTo build the Docker image, use the following command:
docker buildx build --platform linux/amd64 -t registry.example.com/energy-ai:latest .To push the image to a Docker registry:
docker push registry.example.com/energy-ai:latestReplace registry.example.com with your Docker registry URL.
- Ensure the required environment variables are set (e.g., via
.envfile). - Run the application using Docker:
docker run --env-file .env -p 5001:5001 registry.example.com/energy-ai:latest
- Access the API at
http://localhost:5001/optimize.
Logs are output to the console in the following format:
[Timestamp] [LogLevel] Message
Adjust the logging level by modifying the logging.basicConfig configuration in app.py.
This project can be integrated into smart home systems to optimize energy usage and visualize data. In my setup, I integrated the system using ioBroker, Tibber, PVForecast, and a Zendure battery. The data is processed and pushed into MQTT, which is then consumed by Home Assistant to display charts.
- ioBroker: For managing states and integrating with Tibber and PVForecast.
- Tibber: Provides electricity price data.
- PVForecast: Supplies solar production forecasts.
- Zendure Battery: Used for energy storage and control.
- MQTT: Acts as a communication layer for optimized schedules.
- Home Assistant: Displays the data using custom charts.
The provided code is heavily adapted for my specific use case. You may need to modify it to suit your own setup and requirements.
tibber-prices.js: Processes Tibber prices and solar forecasts to calculate adjusted prices.zendure-control-ai.js: Controls the Zendure battery based on AI-optimized schedules.hass-apexcharts-pricegraph.yml: Displays Tibber prices and adjusted prices in Home Assistant.hass-apexcharts-actionschart.yml: Visualizes battery actions in Home Assistant.
The hass-apexcharts-pricegraph.yml file configures a chart to display Tibber prices and adjusted prices based on solar production.
The hass-apexcharts-actionschart.yml file configures a chart to show battery charge and discharge actions.


