//home | tags | archives | about | grosu.nl | eulinux.org | RSS Feed

Gather metrics from a Dutch smart meter via hass & usb2net

floyd - Mon 26 October 2020 - raspberrypi, pi, homeassistant, DSMR

Preface

Modern electricity/gas meters in the Netherlands support the so-called DSMR standard(is it even a standard?), also known as Slimme meter, or P1 poort. One can easily find a FTDI-based "P1 cable" in many on-line Dutch shops (will not link to a particular one but a quick internet search on "P1 cable" or "P1 slimme meter kabel" will return more than enough results).

Collecting data

To collect the data from your meter, you would use this "USB-to-P1 cable" (for the lack of a better name), and some software, e.g DSMR reader, or HomeAssistant (see the DSMR integration)

Since my home lab is nowhere close to the "meterkast", I would have needed a really long USB cable... Instead, we can fix this with science! A quick search through my modest hardware pile produced a Raspberry Pi 1 (the original!), a short network cable, an SD card and a pl2303-based USB to TTL adapter (see Serial console on a Raspberry Pi), and the idea to use ser2net emerged, as the dsmr reader integration supports it.

Here comes OpenWRT

Raspbian or anything as heavy as that would be overweight for this old board, so I went for OpenWRT, really lightweight (the final install uses around 14MB), and easy to configure.

I followed the official OpenWRT on Raspberry Pi docs for the installation, and performed the initial configuration (network, root password, etc) via the serial console. After making sure that the system can be reached via ssh, I moved it next to the smart meter.

To use the FTDI adapter, and ser2net, we need to install some prerequisites first:

opkg update
opkg install ser2net
opkg install kmod-usb-serial-ftdi

There's already a (disabled) ser2net config for ttyUSB0:

# uci show ser2net
...
ser2net.@proxy[1]=proxy
ser2net.@proxy[1].enabled='0'
ser2net.@proxy[1].port='5001'
ser2net.@proxy[1].protocol='telnet'
ser2net.@proxy[1].timeout='0'
ser2net.@proxy[1].device='/dev/ttyUSB0'
ser2net.@proxy[1].baudrate='115200'
ser2net.@proxy[1].databits='8'
ser2net.@proxy[1].parity='none'
ser2net.@proxy[1].stopbits='1'
...

The ser2net proxy has been enabled, and some serial parameters had to be changed as my meter is an Iskra which uses the 2.2 version of DSMR::

root@OpenWrt:~# uci set ser2net.@proxy[1].enabled=1
root@OpenWrt:~# uci set ser2net.@proxy[1].protocol='raw'
root@OpenWrt:~# uci set ser2net.@proxy[1].baudrate=9600
root@OpenWrt:~# uci set ser2net.@proxy[1].parity=even
root@OpenWrt:~# uci set ser2net.@proxy[1].databits=7
root@OpenWrt:~# service ser2net enable
root@OpenWrt:~# service ser2net start

telnet can be used to validate that this actually works (a "telnet <ip> 5001" would return several lines of data every 10 seconds).

At this point, I could follow the DSMR integration docs to add the meter to HomeAssistant.

Bonus: prometheus metrics

HomeAssistant has a Prometheus integration. Short config excerpt for exporting only the DSMR metrics to prometheus:

# configuration.yaml
prometheus:
  namespace: hass
  filter:
    include_entity_globs:
      - sensor.energy_*
      - sensor.gas_*
      - sensor.power_consumption
      - sensor.power_production
      - sensor.power_tariff

Bonus: prometheus metrics take 2

It looks like there's even a prometheus-node-exporter clone in OpenWRT, that can provide a bunch of system metrics (if you're running Prometheus for your network monitoring of course):

opkg install prometheus-node-exporter-lua
uci set prometheus-node-exporter-lua.main.listen_interface=lan
service prometheus-node-exporter-lua enable