This is a Python package that reads and decodes serial data from the HAN port of a few Kaifa electricity meters. It may be used as a Python library or as a standalone CLI.
Serial data frame decoding is fairly naive and based on inspiration from roarfred and Per Erik Nordbø. It uses the Construct library to parse messages in a simple and straight-forward manner that is easily understood and maintainable.
The CLI writes decoded data to either:
Feedback and improvement suggestions are welcome.
This package requires Python 3.6+. It depends on several third party packages, so I recommend installing it in a virtualenv. It is packaged to be installed through pip from GitHub.
$ pip install git+https://github.com/endrebjorsvik/kaifa-meter.git
Connect the electricity meter to a computer using a M-Bus-to-USB converter. It shall appear as a serial port (e.g. /dev/ttyUSB0
or COM3
). To print decoded data to your terminal, run
$ kaifa_meter serial /dev/ttyUSB0
To save decoded data to file, run
$ kaifa_meter serial /dev/ttyUSB0 -o /path/to/outfile.txt
It is also possible to dump all data to a PostgreSQL database. To test the database setup, run
$ kaifa_meter initdb --dbname=<name of database> --dbuser=<name of database user> --dbtable=<table to create in database>
You can then dump data directly to the database by
$ kaifa_meter serial /dev/ttyUSB0 --dbname=db --dbuser=user --dbtable=table
The package is tested on a Kaifa MA105H2E meter that is connected to a computer through the following cabling: