I recently installed a PV power plant on my roof and wanted to measure the power generation and the resulting energy delivery and consumption of my home.
I am a customer of Salzburg AG which uses the KAIFA KA309M smart meter. Unfortunately, neither home assistant nor iobroker have a direct integration out of the box, so we need to do some legwork to get it working.
The following project https://www.libe.net/smart-meter has basically all the necessary ingredients to get it to work, however it stops at the MQTT part and leaves the rest (i.e. which MQTT server, how to get everything to work) up to the user to figure out.
Hardware
- USB to MBUS Slave Converter: https://amzn.to/3Eb0cpR
- Raspberry PI 4 (any version should actually do, 2GB should be sufficient)
Installing the Raspberry PI
As I didn’t have all the cables for the raspberry pi to connect a monitor and keyboard I chose the headless variant: https://www.tomshardware.com/reviews/raspberry-pi-headless-setup-how-to,6028.html
I chose bullseye 64 bit lite as I didn’t need a UI.
After the initial installation, I upgraded everything to the latest version
After this was finished a reboot should be done
While we wait for the raspberry to come back up, its a good time to configure a static DHCP mapping on your router or configure a static ip directly on your raspberry pi.
I didn’t specifically install node.js but I initially intended to use iobroker which installed node.js automatically for me, see https://www.allroundbastler.de/projekte/iobroker-installation-auf-dem-raspberry-pi/ for instructions. Otherwise, this article might give you some insights.
Mosquitto
Installing mosquitto on raspberry is fairly trivial:
Then we configure password based authentication:
with the following content
If you need to access the MQTT broker from different machines in your network I recommend setting up TLS as well
To generate the key files take a look at this FAQ article.
Test and ensure everything is working
This should produce
Note that testing on a mac with MQTTX was not successful for me after enabling TLS (your mileage may vary).
Setting up the MBUS Slave
This project has all the bits and pieces necessary to read out our KAIFA power meter. https://github.com/nooxnet/smart-meter-mbus-dlms
As of today it only works without TLS secured mosquitto but I have created a PR to support TLS (i.e. to ignore certificate errors with self signed certs): https://github.com/nooxnet/smart-meter-mbus-dlms/pull/1
The readme is pretty good and should get you started quite quickly.
I have added the following package.json
(as the distribution did not contain one)
Then you can start and test the script with npm start
.
If you enabled TLS (and you have the patched version), then you need to configure the mqtt
section as follows:
specifically the new option rejectUnauthorized: false
is needed.
After launching the script via npm start
you should hopefully see something like this
If your MQTT configuration is correct then everything should be pushed to mosquitto where it can be picked up by homeassistant.
Also after testing you might want to set maxApplicationDataUnits: 0
to avoid
having the script stop after a single data unit.
Configuring HomeAssistant MQTT
I assume at this point that you have a running home assistant setup. I am running mine in docker but the way how you run it should not matter.
First install the mqtt integration here https://www.home-assistant.io/integrations/mqtt.
Then go to your user settings and ensure that you have enabled advanced mode.
Then, setup the mosquitto broker as follows (you first have to enter the basic connection details and check the “advanced options” switch):
Note that if you have not enabled TLS (which seriously, you should do) then you don’t need to worry about the CA certificate file or the advanced options.
Now, if you set up everything successfully you can test it in the MQTT settings
screen by listening to the topic SmartMeter/Electricity/Main/SummeWirkenergieBezugA
(enter the topic name and click Start Listening
)
Setting up the Home Assistant energy sensors
Now that the MQTT connection is working all that is left is to configure the energy sensors which are being received via MQTT.
To do that you need to edit your configuration.yaml
in the Home Assistant
installation directory.
Restart your Home Assistant to make those config values stick and you should now have the new sensors available.
The only thing left to do is to configure the energy dashboard.
Configuring the Energy dashboard
You can reach the configuration screen via Settings->Dashboard->Energy
Make the following configuration
Then your dashboard should look something like this
Finally you can add another dashboard to get more details about your current consumption vs delivery (I recommend apex charts).
With the following graph you can nicely visualize what you are pulling from the grid vs what you provide.
Obviously, this requires that your inverter is also integrated into Home Assistant.
In the next few days I will probably document my Huawei SUN2000 setup as well.
Thanks for reading!
Links
- Documentation of the whole process: https://www.libe.net/smart-meter
- Installation and configuration of mosquitto: https://myhomethings.eu/de/mosquitto-mqtt-broker-installation-auf-raspberry-pi/
- Install raspberry pi without monitor: https://www.tomshardware.com/reviews/raspberry-pi-headless-setup-how-to,6028.html
- setup MQTT energy sensors for Home Assistant https://community.home-assistant.io/t/energy-setup-using-mqtt-energy-sensors/367175
- MQTT on raspberry pi: https://www.az-delivery.de/blogs/azdelivery-blog-fur-arduino-und-raspberry-pi/mqtt-auf-dem-raspberry-pi-und-mit-micropython-auf-der-esp-familie-teil-1
- Setting up TLS for mosquitto: http://www.steves-internet-guide.com/mosquitto-tls/
- Testing mosquitto on raspberry pi: https://www.sigmdel.ca/michel/ha/rpi/mosquitto_rpios_bullseye_en.html
- Setting up TLS for mosquitto which works with homeassistant: https://community.home-assistant.io/t/solved-mqtt-with-ssl-tls-not-working-with-home-assistant/180361/11
- Static IP for Raspberry PI: https://www.tomshardware.com/how-to/static-ip-raspberry-pi
- Apex Charts Home Assistant: https://github.com/RomRider/apexcharts-card