Home Assistant and HAN port electricity meter
All modern electricity meters in Norway have a local interface called the HAN port. It allows you to read out live data from the meter using your own hardware. Even though all electricity suppliers these days provide apps that show you your usage they rarely do it in real-time and you can't use it ouside of the app. With Home Assistant you can gather data to see the baseline consumption of your house and easily find things like bathroom floor heating left on or observe your fridge compressor turning on and off:
You can safely skip to the next section if you are not interested in the electrical aspects of M-Bus.
M-Bus is an interesting standard because it uses only 2 simple wires to deliver power AND data. There is a single M-bus master on the bus and multiple slaves. Master provides power to the bus and initiates all communication.
The best freely available resources about the electrical part I have found are transceiver datasheets. For example the TSS721A or or NCN5150. The master provides the supply voltage, usually 24 V in the idle state. All slaves have to consume a fixed current all the time (ie. they have to be current sinks). Data from master to the slaves is sent by changing the supply voltage between 24 and 12 V. Data from a slave to the master is sent by changing the load current. On the logical level the bus uses plain old UARTs at a low speed (around 1200 or 2400 baud).
Slave transceivers are available as chips. The transceiver usually has two bus pins (that are polarity independent), integrated rectifier, small 3.3 V LDO and TXD/RXD pins for the UART. Overall, it is quite similar to a LIN system basis chip. I have not yet seen a master transceiver as a chip. Probably it has to be made out of discretes and sized appropriately depending on the size of the bus.
It is important to buy an M-Bus slave adapter if you want to use the HAN port. Master will not work with the HAN port.
The adapter is pretty basic. Just a USB cable and a screw terminal block on the other end. Inside there is an FTDI USB-to-serial chip and some discretes.
Connecting to the electricity meter
By default the HAN port is disabled so you have to contact or see the website of your grid operator or electricity supplier to get it activated. This is free of charge. In my case (Tensio) activation took less than 5 minutes.
My meter has the HAN port on the front behind a small rubber gasket. Don't confuse it with another M-Bus port below. I cut off the plug from one end of a standard Ethernet cable. The other end goes into the meter. M-Bus is wired to pins 1 & 2 which in a standard cable are the orange and white-orange wires. Polarity of the wires does not matter.
Home Assistant configuration
The adapter appears in Linux as a standard
ttyUSB device. I used
picocom to open the device at 1200 baud and I immediately
could see some binary junk being output every couple of seconds. The LEDs on the adapter also blinked. Success! 🙂
HAN port integration is not a part of standard Home Assistant installation so it has to be added as a custom integration.
I picked amshan. To manually install the integration you have to
create a directory called
custom_components in the same directory as
configuration.yaml, unzip the release zip file
and restart Home Assistant. All steps are described in amshan github.
Next, the integration has to be added under "Set up a new integration" in Home Assistant (type "amshan" in the search box).
The only settings are the serial port (it is best to use the unique name from
/dev/serial/by-id/, baud and parity.
I had to pick 1200 baud, 8 data bits, 1 stop bit, and even parity. You may have to experiment with these settings depending on your model of the meter. After a couple of seconds some new entities will become available that contain data from the meter and can be used for live display on a dashboard or the "Energy" panel. 🙂