Protocol gateways

KNX to MQTT Gateway

Bridge a KNX group address to an MQTT topic with SPX on Docker. Follow the KNX/IP occupancy example and test it on a virtual knxd bus.

Publish a KNX group address to MQTT

A monitoring service can use an MQTT topic to track whether a room is occupied. SPX listens for an occupancy telegram through a KNX/IP tunnelling gateway, decodes the one-bit value and publishes a JSON boolean to an MQTT broker. The model maps one KNX group address to one topic. The broker can deliver it to multiple subscribers.

You need a reachable KNX/IP tunnelling gateway, an MQTT broker and a client for each protocol. A virtual knxd gateway with a dummy bus lets you run this exercise before connecting to a building. On Linux, run SPX Server with Docker Engine and Compose v2 and give its container access to the KNX/IP gateway and MQTT broker. SPX installation on Linux

Set up the gateway

  1. Import the KNX to MQTT gateway model in Models and create an instance. Set knx_router_host to your KNX/IP gateway, knx_router_port to its tunnelling port, and mqtt_broker_host and mqtt_broker_port to the broker address reachable from SPX. The bundled defaults use knx_gateway:3671 and mosquitto-server:1883 on the same Docker network.
  2. For the dummy-bus exercise, keep the example group address 29/6/7. For a real installation, use the occupancy group address and data type from your ETS project.
  3. Subscribe to the output topic, then start the gateway instance. Use the broker address and published port reachable from your terminal.
mosquitto_sub -h localhost -p 1883 -q 1 -v -t spx/examples/knx-mqtt-gateway/telemetry/occupied

Build the gateway with an AI agent

Paste this prompt into an agent connected to your SPX workspace.

Prompt for your AI agent

Build a KNX to MQTT gateway in SPX. Run the test on a virtual knxd dummy bus. 1. Write knx_to_mqtt.yaml with name: knx_to_mqtt. Under attributes, declare occupied: false as a scalar. Use knx_ip with the gateway's tunnelling address, normally knx_gateway:3671 on the Docker network. Listen at group address 29/6/7, DPT 1.018, with the bool codec. Store the result in occupied. 2. Publish occupied as a JSON boolean at QoS 1 to spx/examples/knx-mqtt-gateway/telemetry/occupied, with retain disabled. 3. Subscribe with mosquitto_sub -v before starting the SPX instance. On the dummy bus, send a GroupValueWrite of 1 and then 0 with knxtool. Confirm the subscriber receives true followed by false. Ignore any initial false when checking the transition. Use an isolated SPX test workspace. Save the new YAML under custom_models/ and validate it before starting an instance. Use existing reachable services and free ports. Preserve existing models, instances and network settings. Return the YAML paths and the actual client output. If a prerequisite is missing, report which check you could not run.

Tested with GPT-6 Luna · Medium · Est. total 0.0033 USD

In a local Docker lab, Luna generated the YAML, ran the gateway in SPX, and checked the result with protocol clients. On the virtual KNX bus, telegrams carrying 1 and then 0 produced the JSON booleans true and false on MQTT.

The estimate uses token usage for the successful build and test and Standard API rates.

Connect and observe

On the dummy bus, send KNX GroupValueWrite telegrams with values 1 and 0 to 29/6/7. If knxtool runs inside the knxd container, run these commands there. Wait for the subscriber output after each write.

knxtool groupswrite ip:localhost 29/6/7 1
knxtool groupswrite ip:localhost 29/6/7 0

Check occupied in the SPX instance and compare it with the MQTT subscriber. This example can publish the initial false before any KNX telegram arrives, so use the true followed by false transition to confirm that both telegrams crossed the bridge.

In our Docker test, knxtool sent both telegrams through a dummy knxd bus. The SPX KNX/IP adapter received them through its tunnel, and mosquitto_sub observed these JSON booleans on the configured topic:

KNX GroupValueWrite 29/6/7 = 1 | MQTT JSON true
KNX GroupValueWrite 29/6/7 = 0 | MQTT JSON false

Stop the instance when you finish.

Protocol communication

KNX input
KNXnet/IP tunnelling, UDP 3671 by default, group address 29/6/7
KNX value
One-bit occupancy, DPT 1.018, bool codec
MQTT output
spx/examples/knx-mqtt-gateway/telemetry/occupied
MQTT payload
JSON true or false, QoS 1, retain disabled

When to use MQTT to KNX

Use MQTT to KNX when an application needs to send a command, such as switching a light. Map an MQTT subscription to a command attribute and bind it to an outbound KNX group address. Use a separate KNX feedback address and status topic to observe the actuator state. The example above covers monitoring with an inbound KNX binding. SPX KNX/IP adapter