API MQTT
Publishes OpenEMS Edge data to a MQTT broker and subscribes to control topics. Integrates OpenEMS with IoT platforms, cloud dashboards, and home automation systems via MQTT protocol.
Overview
The MQTT API Controller publishes OpenEMS channel values to a MQTT broker in real-time and exposes the Edge configuration. This enables:
-
IoT Integration: Connect to platforms like Home Assistant, Node-RED, Mosquitto dashboards
-
Cloud Connectivity: Publish to Azure IoT Hub, AWS IoT Core, Alibaba Cloud, etc.
-
Flexible Topic Filtering: Selective channel publication via MQTT topic subscriptions
-
Real-Time Monitoring: Changes published immediately; periodic updates every 5 minutes
-
Configuration Export: Full Edge config published as JSON for remote discovery
-
TLS/SSL Support: Encrypted connections with certificate authentication
-
Automatic reconnection with exponential backoff
-
Change-based and periodic publishing
-
Topic filtering with MQTT subscription patterns
-
Persistence priority filtering
-
Debug logging mode
-
Certificate-based authentication
Components
Controller.Api.MQTT
MQTT client controller that maintains connection to MQTT broker and publishes/subscribes to OpenEMS channels.
Controller.Api.MQTT
Implements:
-
io.openems.edge.controller.api.mqtt.ControllerApiMqtt -
io.openems.edge.controller.api.Controller -
io.openems.edge.common.component.OpenemsComponent -
org.osgi.service.event.EventHandler
| Constant | Value |
|---|---|
|
|
|
|
|
|
Configuration
Basic Configuration
| Parameter | Description | Type | Default |
|---|---|---|---|
|
Unique component identifier |
String |
|
|
Human-readable component name |
String |
(component-id) |
|
Enable/disable the controller |
Boolean |
|
|
MQTT client ID for broker identification |
String |
|
|
Optional prefix for all topics (format: |
String |
(empty) |
|
MQTT broker URI (tcp://, ssl://, ws://, wss://) |
String |
|
|
MQTT broker username (required) |
String |
(required) |
|
MQTT broker password (required) |
Password |
(required) |
|
Minimum persistence priority to publish |
Enum |
|
|
Enable verbose debug logging |
Boolean |
|
Security Configuration
| Parameter | Description | Type |
|---|---|---|
|
Client certificate in PEM format for TLS authentication |
Text |
|
Client private key in PEM format for TLS authentication |
Text |
|
Trust store (CA certificates) in PEM format |
Text |
Topic Filtering
| Parameter | Description |
|---|---|
|
Array of MQTT topic subscription patterns |
Uses standard MQTT wildcarding:
* ` matches single level: `channel/_sum/ matches channel/_sum/GridActivePower
* matches multiple levels: channel/_sum/ matches channel/_sum/GridActivePower, channel/_sum/EssSoc, etc.
If not set, all channels are published.
topicFilters = [
"channel/lastUpdate",
"channel/_sum/#",
"channel/ess0/+",
"edgeConfig"
]
Topic Structure
The MQTT topic hierarchy is:
[topicPrefix/]edge/{clientId}/channel/{componentId}/{channelId}
[topicPrefix/]edge/{clientId}/channel/lastUpdate
[topicPrefix/]edge/{clientId}/edgeConfig
Examples
| Topic | Content |
|---|---|
|
Sum grid power (W) |
|
ESS state of charge (%) |
|
Meter power reading (W) |
|
Unix timestamp of latest update |
|
Full Edge configuration as JSON |
If topicPrefix is set to factory1:
| Topic | Content |
|---|---|
|
Sum grid power |
|
Latest update timestamp |
|
Full Edge configuration |
Publishing Behavior
Channel Data Topics
-
Published when channel value changes
-
Also published periodically every 5 minutes (keep-alive)
-
Only channels matching
persistencePrioritythreshold are published -
JSON payload format: value or object depending on channel type
{
"GridActivePower": 2500,
"EssSoc": 85.5,
"GridFrequency": 50.0
}
Configuration Examples
{
"id": "ctrlMqtt0",
"alias": "Home Assistant Gateway",
"enabled": true,
"clientId": "openems_edge",
"topicPrefix": "home",
"uri": "mqtts://mqtt.example.com:8883",
"username": "openems_user",
"password": "secure_password",
"certPem": "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----",
"privateKeyPem": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----",
"trustStorePem": "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----",
"topicFilters": [
"channel/_sum/#",
"channel/ess0/#",
"channel/meter0/#",
"channel/lastUpdate"
],
"persistencePriority": "HIGH",
"debugMode": false
}
{
"id": "ctrlMqtt0",
"alias": "Azure IoT Hub",
"enabled": true,
"clientId": "edge-device-001",
"topicPrefix": "",
"uri": "mqtts://openems-hub.azure-devices.net:8883",
"username": "openems-hub.azure-devices.net/edge-device-001/?api-version=2021-04-12",
"password": "shared_access_key",
"topicFilters": ["channel/_sum/#", "edgeConfig"],
"persistencePriority": "MEDIUM"
}
{
"id": "ctrlMqtt0",
"alias": "Local MQTT Broker",
"enabled": true,
"clientId": "openems",
"uri": "tcp://mosquitto.local:1883",
"username": "openems",
"password": "password123",
"persistencePriority": "VERY_LOW",
"debugMode": false
}
Persistence Priority Levels
Controls which channels are published based on their configured priority:
| Priority | Channel Types |
|---|---|
|
All channels published (default) |
|
Excludes diagnostic channels |
|
Only primary control/monitoring channels |
|
Only critical channels (power, SOC) |
|
System-critical only |
Lower persistencePriority threshold = more data published.
Reconnection Strategy
Automatic reconnection with exponential backoff:
-
Initial reconnect delay: 5 seconds
-
Multiplier per attempt: 1.5x
-
Maximum delay: 5 minutes (300 seconds)
Connection failures are logged; Edge continues operating independently.
Error Handling
| Condition | Handling |
|---|---|
Connection failed |
Automatic reconnection with backoff |
TLS certificate invalid |
Connection rejected, error logged, retry scheduled |
Authentication failed |
Connection rejected, check credentials |
Topic subscription rejected |
Logged as warning; continues publishing |
Message publish timeout |
Logged; next cycle attempts retry |
Configuration error |
Component fails to activate, check syntax |