Gateway for ShellyEM3 to SMA Speedwire protocol. Enables SMA inverters to read data from Shelly EM3.
1.5K
A Python-based gateway that enables Shelly EM3 three-phase energy meters to communicate with SMA inverters by emulating the SMA Energy Meter protocol via Speedwire.
invert_values option for backwards-mounted current transformersversion: '3.8'
services:
shelly-speedwire-gateway:
image: dzikus99/shelly-speedwire-gateway:latest
container_name: shelly-speedwire
restart: unless-stopped
network_mode: host # REQUIRED for multicast/broadcast
environment:
- MQTT_BROKER_HOST=192.168.1.123
- MQTT_BASE_TOPIC=shellies/shellyem3-XXXXXXXXXXXXX
- SPEEDWIRE_SERIAL=1234567890
- MQTT_INVERT_VALUES=false # Set to true if CT clamps are backwards
docker-compose up -d
docker run -d \
--name shelly-speedwire \
--network host \
--restart unless-stopped \
-e MQTT_BROKER_HOST=192.168.1.123 \
-e MQTT_BASE_TOPIC=shellies/shellyem3-XXXXXXXXXXXXX \
-e SPEEDWIRE_SERIAL=1234567890 \
-e LOG_LEVEL=INFO \
dzikus99/shelly-speedwire-gateway:latest
Pull the image from Docker Hub:
docker pull dzikus99/shelly-speedwire-gateway:latest
Or build locally:
docker build -t shelly-speedwire-gateway .
git clone https://github.com/dzikus/shelly-speedwire-gateway
cd shelly-speedwire-gateway
pip3 install -r requirements.txt
sudo mkdir -p /opt/shelly-speedwire-gateway
sudo cp shelly_speedwire_gateway.py /opt/shelly-speedwire-gateway/
sudo cp shelly_speedwire_gateway_config.yaml /opt/shelly-speedwire-gateway/
sudo cp shelly-speedwire-gateway.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable shelly-speedwire-gateway
sudo systemctl start shelly-speedwire-gateway
sudo systemctl status shelly-speedwire-gateway
sudo journalctl -u shelly-speedwire-gateway -f
pip3 install paho-mqtt pyyaml
Configure the gateway (see Configuration section)
Run the gateway:
python3 shelly_speedwire_gateway.py
The gateway can be configured using either a YAML configuration file or environment variables (Docker only).
Create or edit shelly_speedwire_gateway_config.yaml:
mqtt:
broker_host: 192.168.1.123 # Your MQTT broker IP
broker_port: 1883
base_topic: shellies/shellyem3-XXXXXXXXXXXXX # Your Shelly EM3 topic
keepalive: 60
invert_values: false # Set to true if CT clamps are mounted backwards
# username: mqtt_user # Optional MQTT auth
# password: mqtt_pass
speedwire:
interval: 1.0 # Send interval in seconds
use_broadcast: false # Use broadcast instead of multicast
dualcast: false # Send both multicast and broadcast
push_on_update: true # Send immediately on MQTT update
min_send_interval: 0.2 # Minimum seconds between packets
heartbeat_interval: 10.0 # Maximum seconds without sending
serial: 1234567890 # Unique serial number
susy_id: 349 # SUSy ID (349 = standard EMETER-20)
include_voltage_current: true # Include V, I, PF, Hz data
include_sw_version: true # Include software version
# Optional: Direct unicast to specific devices
# unicast_targets:
# - 192.168.1.50 # SMA inverter IP
# - 192.168.1.51
logging:
level: INFO # DEBUG, INFO, WARNING, ERROR
When using Docker, you can configure the gateway using environment variables instead of a configuration file.
| Variable | Default | Description | Required |
|---|---|---|---|
MQTT_BROKER_HOST | localhost | MQTT broker IP address or hostname | No |
MQTT_BROKER_PORT | 1883 | MQTT broker port | No |
MQTT_BASE_TOPIC | shellies/shellyem3-XXXXXXXXXXXXX | Your Shelly EM3 MQTT topic. Format: shellies/shellyem3-[DEVICE_ID] | Yes |
MQTT_KEEPALIVE | 60 | MQTT connection keepalive interval in seconds | No |
MQTT_INVERT_VALUES | false | Set to true if CT clamps are mounted backwards (reverses power sign and swaps total/total_returned) | No |
MQTT_USERNAME | - | MQTT broker username for authentication | No |
MQTT_PASSWORD | - | MQTT broker password for authentication | No |
| Variable | Default | Description | Required |
|---|---|---|---|
SPEEDWIRE_INTERVAL | 1.0 | How often to send data packets in seconds. Lower values = more frequent updates | No |
SPEEDWIRE_USE_BROADCAST | false | Use UDP broadcast (255.255.255.255) instead of multicast. Enable if multicast doesn't work in your network | No |
SPEEDWIRE_DUALCAST | false | Send both multicast and broadcast packets. Useful for mixed network environments | No |
SPEEDWIRE_PUSH_ON_UPDATE | true | Send packet immediately when new MQTT data arrives (reduces latency) | No |
SPEEDWIRE_MIN_SEND_INTERVAL | 0.2 | Minimum seconds between packets when push_on_update is enabled | No |
SPEEDWIRE_HEARTBEAT_INTERVAL | 10.0 | Maximum seconds without sending a packet (ensures SMA device knows gateway is alive) | No |
SPEEDWIRE_SERIAL | 1234567890 | Unique serial number for the emulated energy meter. Change this to a unique value! | Yes |
SPEEDWIRE_SUSY_ID | 349 | SUSy ID for device identification. 349 (0x015D) = standard SMA EMETER-20 | No |
SPEEDWIRE_INCLUDE_VOLTAGE_CURRENT | true | Include voltage, current, power factor, and frequency in packets | No |
SPEEDWIRE_INCLUDE_SW_VERSION | true | Include emulated software version (2.3.4.R) in packets | No |
| Variable | Default | Description | Required |
|---|---|---|---|
LOG_LEVEL | INFO | Logging verbosity: DEBUG, INFO, WARNING, ERROR | No |
To use a configuration file instead of environment variables:
version: '3.8'
services:
shelly-speedwire-gateway:
image: dzikus99/shelly-speedwire-gateway:latest
container_name: shelly-speedwire
restart: unless-stopped
network_mode: host
volumes:
- ./shelly_speedwire_gateway_config.yaml:/app/shelly_speedwire_gateway_config.yaml:ro
Note: When mounting a config file, environment variables are ignored.
network_mode: hostMulticast (Default)
Broadcast
SPEEDWIRE_USE_BROADCAST=trueDualcast
SPEEDWIRE_DUALCAST=trueUnicast
unicast_targets in config fileThe Shelly EM3 uses CT clamps to measure current flow. The direction these clamps are installed affects the power readings:
invert_values ParameterIf your CT clamps are mounted backwards, set invert_values: true in your configuration:
mqtt:
invert_values: true # Fixes backwards CT clamp installation
Or with Docker:
-e MQTT_INVERT_VALUES=true
What invert_values does:
total and total_returned energy countersThe gateway translates Shelly EM3 MQTT data to SMA Speedwire protocol using OBIS codes with reactive power, apparent power, and per-phase power factor.
| Shelly EM3 Data | SMA Speedwire Field | OBIS Code | Unit | Description |
|---|---|---|---|---|
| Active Power | ||||
| Total Power | Total Active Power | 1.4.0 / 2.4.0 | W | Positive = Import, Negative = Export |
| Phase A Power | L1 Active Power | 21.4.0 / 22.4.0 | W | Phase 1 instantaneous power |
| Phase B Power | L2 Active Power | 41.4.0 / 42.4.0 | W | Phase 2 instantaneous power |
| Phase C Power | L3 Active Power | 61.4.0 / 62.4.0 | W | Phase 3 instantaneous power |
| Reactive Power | ||||
| Calculated Total | Total Reactive Power | 3.4.0 / 4.4.0 | VAr | Calculated from V, I, PF |
| Calculated L1 | L1 Reactive Power | 23.4.0 / 24.4.0 | VAr | Calculated per phase |
| Calculated L2 | L2 Reactive Power | 43.4.0 / 44.4.0 | VAr | Calculated per phase |
| Calculated L3 | L3 Reactive Power | 63.4.0 / 64.4.0 | VAr | Calculated per phase |
| Apparent Power | ||||
| Calculated Total | Total Apparent Power | 9.4.0 / 10.4.0 | VA | Calculated from V, I |
| Calculated L1 | L1 Apparent Power | 29.4.0 / 30.4.0 | VA | Calculated per phase |
| Calculated L2 | L2 Apparent Power | 49.4.0 / 50.4.0 | VA | Calculated per phase |
| Calculated L3 | L3 Apparent Power | 69.4.0 / 70.4.0 | VA | Calculated per phase |
| Electrical Parameters | ||||
| Phase A Voltage | L1 Voltage | 32.4.0 | V | Phase 1 voltage |
| Phase B Voltage | L2 Voltage | 52.4.0 | V | Phase 2 voltage |
| Phase C Voltage | L3 Voltage | 72.4.0 | V | Phase 3 voltage |
| Phase A Current | L1 Current | 31.4.0 | A | Phase 1 current |
| Phase B Current | L2 Current | 51.4.0 | A | Phase 2 current |
| Phase C Current | L3 Current | 71.4.0 | A | Phase 3 current |
| Power Factor | ||||
| Phase A PF | L1 Power Factor | 33.4.0 | - | Phase 1 power factor (supports negative values) |
| Phase B PF | L2 Power Factor | 53.4.0 | - | Phase 2 power factor (supports negative values) |
| Phase C PF | L3 Power Factor | 73.4.0 | - | Phase 3 power factor (supports negative values) |
| Worst PF | Total Power Factor | 13.4.0 | - | Worst (closest to zero) phase PF |
| Grid Frequency | Frequency | 14.4.0 | Hz | Grid frequency |
| Energy Counters | ||||
| Energy Consumed | Import Energy | 1.8.0 / 21.8.0 / 41.8.0 / 61.8.0 | Wh | Cumulative energy from grid |
| Energy Exported | Export Energy | 2.8.0 / 22.8.0 / 42.8.0 / 62.8.0 | Wh | Cumulative energy to grid |
By default, the gateway assumes:
If your CT clamps are installed backwards, use invert_values: true to correct the readings.
shellies/shellyem3-shellyem3-mosquitto_sub -h YOUR_BROKER_IP -t "shellies/#" -v
Check MQTT connectivity:
ping YOUR_BROKER_IP
mosquitto_sub -h YOUR_BROKER_IP -t "shellies/shellyem3-XXXXXXXXXXXXX/#" -v
Enable debug logging to see detailed error messages:
# Docker
docker run -e LOG_LEVEL=DEBUG dzikus99/shelly-speedwire-gateway:latest
# Systemd
# Edit /opt/shelly-speedwire-gateway/shelly_speedwire_gateway_config.yaml
# Set logging.level to DEBUG, then restart:
sudo systemctl restart shelly-speedwire-gateway
# Standalone
# Set LOG_LEVEL=DEBUG in config file or run directly
Check logs:
# Docker
docker logs shelly-speedwire
# Systemd
journalctl -u shelly-speedwire-gateway -f
# Standalone
python3 shelly_speedwire_gateway.py
Try different network modes:
SPEEDWIRE_USE_BROADCAST=trueSPEEDWIRE_DUALCAST=trueCheck these settings in order:
invert_values - Set invert_values: true if CT clamps are backwards# Monitor all Shelly MQTT messages
mosquitto_sub -h BROKER_IP -t "shellies/#" -v
# Check if gateway is sending packets
tcpdump -i any -n udp port 9522
# Check Docker network mode
docker inspect shelly-speedwire | grep NetworkMode
# Monitor specific power values with invert_values
mosquitto_sub -h BROKER_IP -t "shellies/shellyem3-XXXXXXXXXXXXX/emeter/+/power" -v
The gateway implements the SMA Speedwire EMETER protocol v1.0:
# Clone repository
git clone https://github.com/dzikus/shelly-speedwire-gateway
cd shelly-speedwire-gateway
# Create virtual environment
python3 -m venv venv
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt
# Run gateway
python3 shelly_speedwire_gateway.py
# Single architecture
docker build -t shelly-speedwire-gateway .
# Multi-architecture
docker buildx create --use
docker buildx build --platform linux/amd64,linux/arm64,linux/riscv64,linux/s390x,linux/386,linux/arm/v7,linux/arm/v6 \
--tag your-registry/shelly-speedwire-gateway:latest --push .
Contributions are welcome! Please feel free to submit issues and pull requests.
MIT License - See LICENSE file for details.
invert_values parameter for backwards CT clamp installationsflip_import_export parameter (replaced by invert_values)Note: This is an unofficial gateway not affiliated with Shelly or SMA. Use at your own risk.
Content type
Image
Digest
sha256:bdfffbd1c…
Size
38.2 MB
Last updated
8 months ago
docker pull dzikus99/shelly-speedwire-gateway:2.0.0