This repository now includes a small helper that pushes Kamstrup meter data to a Home
Assistant MQTT broker using the pykmp-tool equivalent API calls.
-
Create a virtualenv on the Pi (or reuse your existing one) and install the extras needed for the CLI and MQTT publishing:
$ python3 -m venv /opt/pykmp/.venv $ source /opt/pykmp/.venv/bin/activate $ pip install --upgrade pip $ pip install ".[tool]" paho-mqtt
-
Copy the example environment file and fill in the MQTT and serial settings:
$ sudo cp contrib/systemd/pykmp-ha.env.example /etc/pykmp-ha.env $ sudoedit /etc/pykmp-ha.env
The defaults request registers
60 68 80 74 86 87 266every 300 seconds (5 minutes) from/dev/ttyUSB0. SetMQTT_HOSTto your broker address; leaveMQTT_DISCOVERY=trueto let Home Assistant auto-discover the sensors. -
Install the systemd unit and point it at your checkout/venv:
$ sudo cp contrib/systemd/pykmp-ha-mqtt.service /etc/systemd/system/ $ sudoedit /etc/systemd/system/pykmp-ha-mqtt.service
Update
User/Group,WorkingDirectoryandExecStartif your paths differ, for example:WorkingDirectory=/opt/PyKMP ExecStart=/opt/pykmp/.venv/bin/python /opt/PyKMP/examples/home_assistant_mqtt.py -
Enable and start the service:
$ sudo systemctl daemon-reload $ sudo systemctl enable --now pykmp-ha-mqtt.service $ sudo systemctl status pykmp-ha-mqtt.service
Make sure the chosen User is allowed to talk to the meter (e.g. a member of the
dialout group when using /dev/ttyUSB0).
- Serial number once on startup:
<MQTT_BASE_TOPIC>/serial - All registers together:
<MQTT_BASE_TOPIC>/state- Per-register states (JSON, retained):
<MQTT_BASE_TOPIC>/register/<id> - MQTT discovery (when
MQTT_DISCOVERY=true):homeassistant/sensor/pykmp_<serial>_<id>/config
- Per-register states (JSON, retained):
The state payload now includes both a compact map and a register_data list that mirrors
pykmp-tool get-register --json, for example:
{
"serial": "123456",
"timestamp": "2024-04-12T12:00:00Z",
"register_data": [
{
"id_int": 60,
"id_hex": "0x003C",
"name": "Heat Energy (E1)",
"device_class": "energy",
"state_class": "total_increasing",
"unit_of_measurement": "kWh",
"unit_int": 2,
"unit_hex": "0x02",
"unit_str": "kWh",
"value_float": 135152.0,
"value_str": "135152"
}
]
}MQTT discovery payloads now also include device_class and state_class when the unit
is recognized (e.g., energy, power, temperature, volume, flow). For flow the script
now converts l/h to m³/h so Home Assistant accepts the unit; raw values and units
are still included alongside the converted values.
Home Assistant will create one sensor per register via the discovery payloads. If you prefer manual sensors, point them at the per-register topics above.