All apps · 0 apps
ocpp2mqtt
Docker app from Nodiaque's Repository
Overview
Readme
View on GitHubocpp2mqtt
ocpp2mqtt is a gateway software that converts OCPP (Open Charge Point Protocol) requests to MQTT (Message Queuing Telemetry Transport) and vice versa. This enables seamless integration of EV charging stations with any home automation system.
✨ Features
- 🔌 Converts OCPP 1.6 requests to MQTT and vice versa
- 🏠 Easy integration with home automation systems (Home Assistant, OpenHAB, etc.)
- 🐳 Docker and Kubernetes support for flexible deployment
- 📝 Configurable logging with file rotation support
- 🔄 Automatic MQTT reconnection with exponential backoff
- 🔐 MQTT authentication support
- 🌐 WebSocket transport support for MQTT
- 📡 Real-time charger connection state monitoring via MQTT
- ⚡ Automatic disconnection detection with reason tracking
📋 Table of Contents
- Prerequisites
- Installation
- Configuration
- Usage
- MQTT Topics
- Logging
- Integration Guides
- Contributing
- License
📦 Prerequisites
- Python 3.8 or higher
- MQTT Broker (Mosquitto, HiveMQ, etc.)
- Docker/Kubernetes (optional)
🚀 Installation
Option 1: Python (Direct)
# Clone the repository
git clone https://github.com/gyzod/ocpp2mqtt.git
cd ocpp2mqtt
# Install dependencies
pip install -r requirements.txt
# Run the application
python central_system.py
Option 2: Docker
# Build the image
docker build -t ocpp2mqtt .
# Run the container
docker run -d \
--name ocpp2mqtt \
-p 3000:3000 \
-e MQTT_HOSTNAME=your-mqtt-broker \
-e MQTT_BASEPATH=ocpp/ \
-e MQTT_USESTATIONNAME=true \
ocpp2mqtt
Option 3: Docker Compose
version: '3.8'
services:
ocpp2mqtt:
build: .
ports:
- "3000:3000"
environment:
- MQTT_HOSTNAME=mqtt-broker
- MQTT_BASEPATH=ocpp/
- MQTT_USESTATIONNAME=true
- AUTHORIZED_TAG_ID_LIST=["tag1","tag2"]
restart: unless-stopped
Option 4: Kubernetes
See Kubernetes Deployment Guide for detailed instructions.
⚙️ Configuration
Create a .env file or set environment variables:
MQTT Configuration
| Variable | Default | Description |
|---|---|---|
MQTT_HOSTNAME |
localhost |
MQTT broker IP address or hostname |
MQTT_PORT |
1883 |
MQTT broker port |
MQTT_BASEPATH |
ocpp/test |
Base path for MQTT topics |
MQTT_USERNAME |
(empty) | MQTT username (if authentication required) |
MQTT_PASSWORD |
(empty) | MQTT password |
MQTT_TRANSPORT |
tcp |
Transport protocol: tcp, websockets, or unix |
MQTT_KEEPALIVE |
60 |
MQTT keepalive interval in seconds |
MQTT_TIMEOUT |
30 |
MQTT connection timeout in seconds |
MQTT_RECONNECT_BASE_DELAY |
5 |
Initial reconnection delay in seconds |
MQTT_RECONNECT_MAX_DELAY |
60 |
Maximum reconnection delay in seconds |
MQTT_CLIENT_ID |
(auto) | Custom MQTT client ID (auto-generated if not set) |
MQTT_USESTATIONNAME |
(empty) | Set to true to append station name to base path |
MQTT_WEBSOCKET_PATH |
(empty) | WebSocket path (for WebSocket transport) |
MQTT_WEBSOCKET_HEADERS |
(empty) | JSON string with WebSocket headers |
Server Configuration
| Variable | Default | Description |
|---|---|---|
LISTEN_ADDR |
0.0.0.0 |
Address to bind the OCPP WebSocket server |
LISTEN_PORT |
3000 |
Port to listen for OCPP connections |
AUTHORIZED_TAG_ID_LIST |
[] |
JSON array of authorized RFID tags |
EXPECTED_CHARGE_POINTS |
[] |
JSON array of expected charge point IDs (publishes DISCONNECTED on startup) |
OCPP Command Retry Configuration
| Variable | Default | Description |
|---|---|---|
OCPP_COMMAND_RETRY_ATTEMPTS |
5 |
Number of retry attempts when WebSocket is temporarily disconnected |
OCPP_COMMAND_RETRY_BASE_DELAY |
0.3 |
Base delay in seconds between retries (exponential backoff) |
Logging Configuration
| Variable | Default | Description |
|---|---|---|
LOG_LEVEL |
INFO |
Log level: DEBUG, INFO, WARNING, ERROR, CRITICAL |
LOG_FILE |
(empty) | Path to log file (logs only to console if not set) |
LOG_MAX_SIZE |
10485760 |
Maximum log file size in bytes (10MB default) |
LOG_BACKUP_COUNT |
5 |
Number of backup log files to keep |
LOG_FORMAT |
(default) | Custom log format string |
LOG_DATE_FORMAT |
%Y-%m-%d %H:%M:%S |
Log date format |
Example .env file
# MQTT Configuration
MQTT_HOSTNAME=192.168.1.100
MQTT_PORT=1883
MQTT_BASEPATH=ocpp/
MQTT_USESTATIONNAME=true
MQTT_USERNAME=
MQTT_PASSWORD=
# Server Configuration
LISTEN_PORT=3000
LISTEN_ADDR=0.0.0.0
AUTHORIZED_TAG_ID_LIST=["johnny-car","other-car"]
EXPECTED_CHARGE_POINTS=["charger1","charger2"]
# Logging Configuration
LOG_LEVEL=INFO
LOG_FILE=/var/log/ocpp2mqtt/app.log
LOG_MAX_SIZE=10485760
LOG_BACKUP_COUNT=5
🔧 Usage
Starting the Server
python central_system.py
The server will start listening for OCPP connections on the configured address and port. When a charge point connects, it will automatically bridge communications to MQTT.
Charge Point Connection
Configure your OCPP charge point to connect to:
ws://<server-ip>:<port>/<station-id>
Example: ws://192.168.1.10:3000/charger1
📡 MQTT Topics
State Topics (Charger → MQTT)
All charger data is published to: <MQTT_BASEPATH>/<station-id>/state/<parameter>
Connection State Topics
These topics track the WebSocket connection between the charger and ocpp2mqtt:
| Topic | Description | Values |
|---|---|---|
.../state/connection_state |
Current WebSocket connection state | CONNECTED, DISCONNECTED |
.../state/last_connected |
Timestamp of last successful connection | ISO 8601 datetime |
.../state/last_disconnected |
Timestamp of last disconnection | ISO 8601 datetime |
.../state/disconnect_reason |
Reason for the last disconnection | See Disconnect Reasons |
.../state/service_started |
Timestamp when ocpp2mqtt service started | ISO 8601 datetime |
Note: If
EXPECTED_CHARGE_POINTSis configured, the service publishesDISCONNECTEDstate for each expected charger on startup, before any charger connects.
Charger Data Topics
| Topic | Description |
|---|---|
.../state/heartbeat |
Connection heartbeat |
.../state/last_seen |
Last communication timestamp |
.../state/status |
Current charger status |
.../state/error_code |
Current error code |
.../state/charge_point_vendor |
Charger vendor |
.../state/charge_point_model |
Charger model |
.../state/firmware_version |
Firmware version |
.../state/current_import |
Current (Amperes) |
.../state/voltage |
Voltage (Volts) |
.../state/power_active_import |
Active power (Watts) |
.../state/energy_active_import_register |
Total energy (Wh) |
.../state/meter_start |
Transaction start meter |
.../state/meter_stop |
Transaction stop meter |
Disconnect Reasons
When a charger disconnects, the disconnect_reason topic indicates why:
| Reason | Description |
|---|---|
normal_closure |
Clean disconnection (charger initiated) |
session_cancelled |
Session was cancelled (e.g., charger reconnected) |
connection_closed_<code> |
WebSocket closed with specific code |
connection_error_<code> |
WebSocket error with specific code |
unexpected_error |
Unexpected error occurred |
Note: When a charger disconnects,
power_active_importandcurrent_importare automatically reset to0.
Command Topics (MQTT → Charger)
Send commands to: <MQTT_BASEPATH>/<station-id>/cmd
Message Schema
{
"action": "<operation_name>",
"args": { <ocpp_payload> }
}
Available Commands
Change Availability
{
"action": "change_availability",
"args": {
"connector_id": 1,
"type": "Operative"
}
}
Remote Start Transaction
{
"action": "remote_start_transaction",
"args": {
"connector_id": 1,
"id_tag": "your-rfid-tag"
}
}
Remote Stop Transaction
{
"action": "remote_stop_transaction",
"args": {
"transaction_id": 1
}
}
Reset
{
"action": "reset",
"args": {
"type": "Soft"
}
}
Unlock Connector
{
"action": "unlock_connector",
"args": {
"connector_id": 1
}
}
Command Result
Command results are published to: <MQTT_BASEPATH>/<station-id>/cmd_result/status
Connection Monitoring
ocpp2mqtt automatically monitors the WebSocket connection with each charger and publishes state changes to MQTT. This enables your home automation system to:
- Detect offline chargers - Trigger alerts when a charger goes offline
- Track availability - Know when chargers are available for use
- Monitor network issues - Identify connection problems with specific chargers
- Automate recovery - Trigger actions when a charger reconnects
Example: Home Assistant Binary Sensor
mqtt:
binary_sensor:
- name: "Charger Connection"
state_topic: "ocpp/charger1/state/connection_state"
payload_on: "CONNECTED"
payload_off: "DISCONNECTED"
device_class: connectivity
Example: Home Assistant Automation (Alert on Disconnect)
automation:
- alias: "Charger Disconnected Alert"
trigger:
- platform: mqtt
topic: "ocpp/charger1/state/connection_state"
payload: "DISCONNECTED"
action:
- service: notify.mobile_app
data:
title: "⚠️ Charger Offline"
message: "Your EV charger has disconnected"
📝 Logging
ocpp2mqtt supports flexible logging configuration with both console and file output.
Console Only (Default)
By default, logs are only output to the console:
LOG_LEVEL=INFO python central_system.py
File Logging
Enable file logging by setting LOG_FILE:
LOG_FILE=/var/log/ocpp2mqtt/app.log python central_system.py
Log Rotation
When file logging is enabled, logs are automatically rotated:
- Maximum file size:
LOG_MAX_SIZE(default: 10MB) - Backup files kept:
LOG_BACKUP_COUNT(default: 5)
Docker with File Logging
docker run -d \
--name ocpp2mqtt \
-p 3000:3000 \
-e MQTT_HOSTNAME=broker \
-e LOG_FILE=/var/log/ocpp2mqtt/app.log \
-v /path/to/logs:/var/log/ocpp2mqtt \
ocpp2mqtt
📚 Integration Guides
Detailed integration guides are available for popular home automation platforms:
- Home Assistant - MQTT sensors, automations, and energy dashboard integration
- OpenHAB - Things, Items, Rules, and Sitemap configuration
- Kubernetes - Deployment, ConfigMaps, Secrets, and Ingress setup
🤝 Contributing
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Running Tests
pytest tests/ -v
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgements
Install Ocpp2mqtt on Unraid in a few clicks.
Find Ocpp2mqtt in Community Apps on your Unraid server, review the template, and click Install. Unraid handles the Docker app or plugin setup from the published template.
Categories
Download Statistics
Related apps
Explore more like this
Explore allDetails
gyzod/ocpp2mqttRuntime arguments
- Network
bridge- Shell
sh- Privileged
- false
Template configuration
Port mapping for OCPP connection. The host side must match LISTEN_PORT value
- Target
- 3000
- Default
- 3000
- Value
- 3000
Port to listen for incoming OCPP connection. If this is change, you must update the OCPP PORT port forwarding
- Default
- 3000
- Value
- 3000
If using multiple NIC, used to bind to a specific address
- Default
- 0.0.0.0
- Value
- 0.0.0.0
Port used for MQTT
- Default
- 1883
- Value
- 1883
MQTT server ip address
Basepath to use for mqtt. State and command will be after that
- Default
- ocpp/charger1
- Value
- ocpp/charger1
Username to connect to MQTT if required
Password for MQTT connection
Set to true (all lower) to append the station name to the MQTT_BASEPATH. Be sure the basepath end with /
List of station ID that are authorized to charge. You can get this value in your station apps.
- Value
- [""]