Coder Social home page Coder Social logo

matthijsvisser / kamstrup-402-mqtt Goto Github PK

View Code? Open in Web Editor NEW
43.0 4.0 11.0 1.1 MB

This project provides a Python library that enables communication with the Kamstrup Multical 402 heat meter. The configured parameters will be read from the meter at a certain interval and published in MQTT messages.

License: MIT License

Python 97.74% Dockerfile 2.26%
mqtt mqtt-client systemd-service systemd kamstrup multical 402 kamstrup-smart yaml yaml-configuration

kamstrup-402-mqtt's Introduction

GitHub GitHub GitHub

Kamstrup multical 402 MQTT library

This project provides a Python library that enables communication with the Kamstrup Multical 402 heat meter. The configured parameters will be read from the meter at a certain interval and published in MQTT messages. This guide uses Linux as a base operating system.

Contents

Requirements

The following software and packages are required to run the script via the commandline or as a service

Required hardware

Configuration file

The library can be configured to fit your needs using the config.yaml file. The parameters of this file are described below.

parameter name description
host MQTT broker host domain name or IP address
port MQTT broker port number
client Client name to identify this MQTT client e.g. Kamstrup
topic MQTT topic where the values are published on
retain If set to true, the message will be set as the "last known good"/retained message for the topic
qos The quality of service level to use for the message. Cane be any value between 0 and 2
authentication Set this to true if your MQTT broker requires authentication
username Username to connect to broker
password Password to connect to broker
com_port port of serial communication device
parameters List of parameters that are read and published to the configured MQTT topic. See Meter parameters table.
poll_interval Meter readout interval in minutes (value should be less than 30 to prevent the meter from going in standby mode

Kamstrup meter parameters

These parameters can be added to the config.yaml file. Atleast one parameter must be present in the configuration file.

parameter name description
energy consumed energy in GJ
power
temp1 incoming temperature in degrees
temp2 outgoing temperature in degrees
tempdiff difference between temp1 and temp2 in degrees
flow water flow in l/h
volume consumed water in m3
minflow_m minimum water flow
maxflow_m minimum water flow
minflowDate_m
maxflowDate_m
minpower_m
maxpower_m
avgtemp1_m
avgtemp2_m
minpowerdate_m
maxpowerdate_m
minflow_y
maxflow_y
minflowdate_y
maxflowdate_y
minpower_y
maxpower_y
avgtemp1_y
avgtemp2_y
minpowerdate_y
maxpowerdate_y
temp1xm3
temp2xm3
infoevent
hourcounter

Running the script

Running on the commandline

The script can be started by simply starting the daemon file with Python 3.

python3 daemon.py &

The & will start the Python script as a daemon process. The following output is a example of what to expect in the log file when the meter is receiving actual data from the meter.

mqtt_handler.py publish:  46 - INFO - Publishing 'kamstrup/values' '{"energy": 227.445, "volume": 2131.935, "temp1": 52.81, "temp2": 39.94}' to 10.0.0.210:1883]

By subscribing to the configured mqtt topic on the MQTT broker we can view these messages. You can use MQTT Explorer for debugging to test the MQTT part.

Running as a systemd service

Edit the kamstrup_meter.service file and adjust the path accordingly. The working directory in this example is /opt/kamstrup/.

[Unit]
Description=Kamstrup2mqtt Service
After=multi-user.target

[Service]
Type=simple
WorkingDirectory=/opt/kamstrup
ExecStart=/usr/bin/python3 /opt/kamstrup/daemon.py
StandardOutput=null
StandardError=journal
Restart=always

[Install]
WantedBy=multi-user.target

Check if the service has the required permissions after copying, if not, change it with chmod and chown.

cp /opt/kamstrup/kamstrup_meter.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable kamstrup_meter.service
sudo service kamstrup_meter start

Running as a Docker container

Requirements

Make sure the following is installed:

**Docker-compose is optional but useful and easy to configure.

Building the image

Build a docker image "kamstrup"

docker build -t kamstrup .

Edit the docker-compose.yml file and adjust the "devices" parameter accordingly. Syntax is:

<Path must match usb device --> /dev/ttyKamstrup:/dev/ttyKamstrup <-- Inside container, must match path of config.yml>

Initial start of container

Initial start of the container:

docker-compose up -d

The container should run now and produce output via Mqtt. If not, check the logging in logs.

Container management

After the initial start of the container it can be stopped and started again with the folowing commands:

docker start kamstrup_kamstrup_1
docker stop kamstrup_kamstrup_1
docker restart kamstrup_kamstrup_1

Check if the container is running with:

docker stats

Meter setup

It can be hard to find the correct position of the meter head. It might differ if you are using an other model. I positioned the infrared head as follows:

meter setup

Troubleshooting

This section includes some tips to solve some issues.

Read the log file

The log file will, in most cases, spoil what going on if something's not working.

tail -f debug.log

Reading values

If you have any troubles with retrieving the values from the meter, make sure that the meter is 'awake', you can do so by pressing any button on the meter. It is also important that you've positioned the meter head correctly. I may take a while to find the sweet spot. For some reason the position for the meter head that I've got is a little bit higher than what the distance keepers on the meter suggest.

Finding the correct com port

Unplug the usb connector from the computer and plug it back in. Use dmesg to find the com port.

dmesg

kamstrup-402-mqtt's People

Contributors

matthijsvisser avatar matthijsvssr avatar sebastiaankok avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

kamstrup-402-mqtt's Issues

Docker install

Hi Matthijs,

I've have this running perfectly under windows, but since a couple days I have a NAS (synology).
And I want to move the script to the NAS with docker. But somehow i'm not able to install this on a docker.

Any change you could help?

Greetings,
Jeroen

Multical 603

Hello!

I recently got my meter changed from a Kamstrup Multical 402 to a Kamstrup Multical 603. The same setup works like a charm!! One minor thing, the optical head needs to be moved down just a couple of millimeters.

Kindest, Magnus

I can't start the systemd service on my RasperryPI

Hello, I followed your steps for the setup following the blog of Pieter https://www.pieterbrinkman.com/2022/02/01/make-your-city-heating-stadsverwarming-smart-and-connect-it-home-assistant-energy-dashboard/#comment-20805

But whenever I run sudo systemctl start kamstrup_meter.service the system freeze and the service doesn't start.

I'm not sure what i'm doing wrong.

I have the project checkout in the /opt folder, with below permissions

pi@pi-mr:/opt/kamstrup $ ls -lah
total 76K
drwxr-xr-x 6 pi   pi   4.0K Apr 18 22:51 .
drwxr-xr-x 3 root root 4.0K Apr 18 22:34 ..
-rw-r--r-- 1 pi   pi    371 Apr 18 22:36 config.yaml
-rw-r--r-- 1 pi   pi   2.5K Apr 18 22:34 daemon.py
-rw-r--r-- 1 pi   pi    252 Apr 18 22:34 docker-compose.yml
-rw-r--r-- 1 pi   pi    228 Apr 18 22:34 Dockerfile
drwxr-xr-x 8 pi   pi   4.0K Apr 18 22:34 .git
-rw-r--r-- 1 pi   pi     30 Apr 18 22:34 .gitignore
drwxr-xr-x 2 pi   pi   4.0K Apr 18 22:34 images
-rw-r--r-- 1 pi   pi   5.1K Apr 18 22:34 kamstrup_meter.py
-rw-r--r-- 1 pi   pi    288 Apr 18 22:44 kamstrup_meter.service
-rw-r--r-- 1 pi   pi   1.1K Apr 18 22:34 LICENSE
drwxr-xr-x 2 pi   pi   4.0K Apr 18 22:41 logs
-rw-r--r-- 1 pi   pi   2.1K Apr 18 22:34 mqtt_handler.py
drwxr-xr-x 2 pi   pi   4.0K Apr 18 22:41 __pycache__
-rw-r--r-- 1 pi   pi   7.9K Apr 18 22:34 readme.md
-rw-r--r-- 1 pi   pi     27 Apr 18 22:34 requirements.txt

and I have configure the systemd service to run as follows

[Unit]
Description=Kamstrup2mqtt Service
After=multi-user.target

[Service]
User=pi
Type=simple
WorkingDirectory=/opt/kamstrup
ExecStart=/usr/bin/python3 /opt/kamstrup/daemon.py
StandardOutput=null
StandardError=journal
Restart=always

[Install]
WantedBy=multi-user.target

BTW, running the script via command line send the output correctly

Thank you in advance

Multical 403

I can confirm that the multical 403 can also be read with this library.

Docker error while installing

Hi, when I try to install the docker build I get this error:

sudo docker build -t kamstrup .
[+] Building 0.6s (2/2) FINISHED
=> [internal] load build definition from dockerfile 0.2s
=> => transferring dockerfile: 2B 0.1s
=> [internal] load .dockerignore 0.2s
=> => transferring context: 2B 0.1s
ERROR: failed to solve: failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount863944789/dockerfile: no such file or directory

Any idea how to fix it?

Questions about hardware

Hi Matthijs @matthijsvisser,

I find this project very interesting. I have a Kamstrup 402 and some questions regarding the hardware and other requirements. Can I ask you some questions?

Are you using this with district heating (stadsverwarming)?
How difficult is it to place the IR receiver?
Is IR the only option for communication?
Which home automation platform are you connecting to? Home assistant, domoticz, other?
What can you recommend to control the IR receiver? A raspberry pi?

Thanks!

Stuck on "AttributeError: 'kamstrup' object has no attribute 'serial'"

Hi Matthijs, i followed steps in your git and am trying to get everything but I am stuck now.
It seems there is something wrong with my serial connection. Hope you can direct me in the right way? I am not a python/linux expert...

I am quite sure my USB is connected to ttyUSB0 as I can see from dmesg :
FTDI USB Serial Device converter now attached to ttyUSB0

But when I add ttyUSB0 to config.yaml and I run the script with python3 daemon.py & I get this mesage:

File "/root/kamstrup-402-mqtt/daemon.py", line 92, in <module> main() File "/root/kamstrup-402-mqtt/daemon.py", line 89, in main daemon.run() File "/root/kamstrup-402-mqtt/daemon.py", line 81, in run values = self.heat_meter.run() File "/root/kamstrup-402-mqtt/kamstrup_meter.py", line 104, in run if self.serial.is_open: AttributeError: 'kamstrup' object has no attribute 'serial'

Multical 403 - No response from meter / Message is invalid

I am trying to get this script to save it's output to MQTT, but it throws 2 different error's when I start the script

  • No response from meter
  • Message is invalid

I get those error when the meterhead is flat on the surface, and when I hold it about 1 cm above the normal place.

Also I see that the script is publishing values to MQTT, but I don't see them with MQTT browser (other MQTT stuff is working).

Some information:
root@kamstrup-01:~# ls -l /dev/serial/by-id
total 0
lrwxrwxrwx 1 root root 13 Feb 12 15:41 usb-FTDI_FT230X_Basic_UART_D308BOXH-if00-port0 -> ../../ttyUSB0

cloned the git in /opt/kamstrup

root@kampstrup-01:~# cat /opt/kamstrup/config.yaml
mqtt:
    host: 192.168.8.254
    port: 1883
    client: kamstrup
    topic: kamstrup
    qos: 0
    retain: False
    authentication: False
    username: mqtt
    password: <PASSWORD>
serial_device:
    com_port: /dev/ttyUSB0
kamstrup:
    parameters:
    - energy
    - volume
    - temp1
    - temp2
    poll_interval: 11

kamstrup 402/3 on ESPhome

Hi Matthijs,

In my search on how to read my kamstrup 403 with an esp32/esphome, I keep on finding your page mentioned by other people..
Because of that, I'm want to ask some help.

I had my Multical 66C working on the esphome configuration Proudelm. But Vattenfall changed the kamstrup now for a 403..
The serial reading of the 403 is significant different than the 66C/602.
My knowledge on C# is little to 0, so I hope you're willing to get me on the road a little.

Your solution requires a connection to an USB cable/port, but my heatingexchanger is too far away from my home automation device, even with long usbcables.... therefore I'm searching for the esphome solution...

Greetings, Richard

CRC error?

Hello, I am currently working on implementing mqtt on my kamstrup 402. So far, mqtt sends the messages correctly, however they are all blank ( [] ).

I saw on the log that there are "CRC error" messages:

[2023-12-22 19:56:55 daemon.py run: 77 - INFO - Waiting 1 minute(s) for the next meter readout]
[2023-12-22 19:57:55 kamstrup_meter.py open: 118 - DEBUG - Opened serial port]
[2023-12-22 19:57:55 kamstrup_meter.py readparameter: 204 - WARNING - Message is invalid]
[2023-12-22 19:57:56 kamstrup_meter.py recv: 188 - ERROR - CRC error]
[2023-12-22 19:57:56 kamstrup_meter.py readparameter: 198 - WARNING - No response from meter]
[2023-12-22 19:57:56 kamstrup_meter.py readparameter: 204 - WARNING - Message is invalid]
[2023-12-22 19:57:56 kamstrup_meter.py recv: 188 - ERROR - CRC error]
[2023-12-22 19:57:56 kamstrup_meter.py readparameter: 198 - WARNING - No response from meter]
[2023-12-22 19:57:56 kamstrup_meter.py readparameter: 204 - WARNING - Message is invalid]
[2023-12-22 19:57:56 kamstrup_meter.py close: 126 - DEBUG - Closed serial port]
[2023-12-22 19:57:56 mqtt_handler.py publish: 46 - INFO - Publishing 'kamstrup/values' '{}' to ---.---.---.---]
[2023-12-22 19:57:56 daemon.py run: 77 - INFO - Waiting 1 minute(s) for the next meter readout]

I can tell that the IR reader is correctly placed on the position, as there are two small bumps that keep it in place.

Does anyone know what the CRC error is? As far as I can tell, my serial port is ttyUSB0 (confirmed it via dmseg).

Any help is greatly appreciated! ๐Ÿ˜„

Adding compatibility with kamstrup 403

Hello Matthijs,

Would it be possible to make it work with the kamstrup 403?

I have everything setup according to the instructions but I get this error message:

[2020-10-09 14:12:52 kamstrup_meter.py readparameter: 204 - WARNING - Message is invalid]
[2020-10-09 14:12:52 kamstrup_meter.py readparameter: 204 - WARNING - Message is invalid]
[2020-10-09 14:12:52 kamstrup_meter.py readparameter: 204 - WARNING - Message is invalid]
[2020-10-09 14:12:52 kamstrup_meter.py readparameter: 204 - WARNING - Message is invalid]
[2020-10-09 14:12:52 mqtt_handler.py publish: 55 - INFO - Publishing 'kamstrup/values' '{}' to 192.168.1.200:1883]
[2020-10-09 14:12:52 daemon.py run: 84 - INFO - Waiting 1 minute(s) for the next meter readout]

When I use the kamstrup app in windows I get a read out of the meter, so not sure where it goes wrong.

thx!

Cannot extract meter value - Kamstrup Multical 401

I hope you vcan help me. I have a Kamstrup Multical 401 and trying to use your script. I have tried to place de IR head at every possible way at the meter but the following warning keeps appearing. In MQTT i see the topic but no values.

[2022-10-05 21:27:18 daemon.py init: 41 - INFO - initializing daemon]
[2022-10-05 21:27:18 mqtt_handler.py connect: 43 - INFO - Connected to MQTT at: 192.168.2.98:1883]
[2022-10-05 21:27:18 mqtt_handler.py connect: 45 - INFO - with username pi, QoS level = 1 and retain = False]
[2022-10-05 21:27:18 kamstrup_meter.py close: 126 - DEBUG - Closed serial port]
[2022-10-05 21:27:18 kamstrup_meter.py open: 118 - DEBUG - Opened serial port]
[2022-10-05 21:27:20 kamstrup_meter.py rd: 131 - DEBUG - Rx timeout]
[2022-10-05 21:27:20 kamstrup_meter.py readparameter: 198 - WARNING - No response from meter]
[2022-10-05 21:27:22 kamstrup_meter.py rd: 131 - DEBUG - Rx timeout]
[2022-10-05 21:27:22 kamstrup_meter.py readparameter: 198 - WARNING - No response from meter]
[2022-10-05 21:27:24 kamstrup_meter.py rd: 131 - DEBUG - Rx timeout]
[2022-10-05 21:27:24 kamstrup_meter.py readparameter: 198 - WARNING - No response from meter]
[2022-10-05 21:27:26 kamstrup_meter.py rd: 131 - DEBUG - Rx timeout]
[2022-10-05 21:27:26 kamstrup_meter.py readparameter: 198 - WARNING - No response from meter]
[2022-10-05 21:27:26 kamstrup_meter.py close: 126 - DEBUG - Closed serial port]
[2022-10-05 21:27:26 mqtt_handler.py publish: 55 - INFO - Publishing 'kamstrup/values' '{}' to 192.168.2.98:1883]
[2022-10-05 21:27:26 daemon.py run: 84 - INFO - Waiting 1 minute(s) for the next meter readout]

This is my configuration file:

mqtt:
host: 192.168.2.98
port: 1883
client: kamstrup402
topic: kamstrup
qos: 1
retain: False
authentication: True
username: pi
password: [password]
serial_device:
com_port: /dev/serial/by-id/usb-Silicon_Labs_CP2102_USB_to_UART_Bridge_Controller_0001-if00-port0
kamstrup:
parameters:
- energy
- volume
- temp1
- temp2
poll_interval: 1

i hope anyone has an idea what i am doing wrong. Thanks in advance.

Errors running deamon.py

I'm stuck on this error when I run python3 daemon.py &:

[3] 2510
root@raspberrypi:/home/pi/kamstrup-402-mqtt# Traceback (most recent call last):
File "daemon.py", line 92, in
main()
File "daemon.py", line 88, in main
daemon = KamstrupDaemon()
File "daemon.py", line 50, in init
self.poll_interval = kamstrup_cfg["poll_interval"]
KeyError: 'poll_interval'

  • All prereqs installed
  • USB IR meter reader installed, placed on Kamstrup, works fine with modified multical402-4-domoticz.py script, I get valid data
  • MQTT broker running fine, is already successfully transmitting DSMR reader data to Home Assistant

RuntimeError: Message publish failed: The client is not currently connected

I'm getting this error when running the daemon.py
File "/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py", line 362, in wait_for_publish
raise RuntimeError('Message publish failed: %s' % (error_string(self.rc)))
RuntimeError: Message publish failed: The client is not currently connected.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.