MenuBluefi.re
Legal
Back to Posts tech
24-04-19

Make Your Smart Electricity Meter... Actually Smart

In the Netherlands, smart electricity meters come with a so-called "P1 port" (wiring table shown below, source) that outputs power usage, gas consumption, solar energy production and more. When attaching a device to this port, the smart meter will send this information over serial every one to ten seconds using the DSMR standard.

PinSignal nameDescriptionRemark
1+5V+5V power supplyPower supply line
2Data RequestData RequestInput
3Data GNDData ground
4n.c.Not connected
5DataData lineOutput. Open collector
6Power GNDPower groundPower supply line

This made me curious, and I wanted to see whether it was possible to get insight into this data. There are commercial devices on the market that allows you to use the P1 port, but those were prohibitively expensive for me. As such, I decided to look into a DIY solution. If possible, I also wanted this to be integrated with Home Assistant. For those of you who are not in the know, Home Assistant is self-hosted platform that allows you to control and automate smart devices and services in their home.

In this post, I will describe how I set things up. It is important to know that this set up relies on a Home Assistant, so it is crucial that you've already got Home Assistant set up on some kind of device in your home. Up-to-date instructions can be found on the Home Assistant website. In my case, I am using a server inside of my home that I already had set up, and simply running Home Assistant as a Docker container.

Equipment

Assuming we've got Home Assistant running, we are going to need a serial-to-USB converter with a P1 plug on one side no matter what. I've got my cable for around $ 9 from AliExpress. When ordering such a cable, make sure that the title or description explicitly mentions compatibility with your smart meter. In my case, my smart meter is a Sagemcom T-210D, so I'll need to check the title and description if this model is explicitly mentioned anywhere.

If you're attaching the smart meter directly, to the device running Home Assistant, that's it. However, in my case, my Home Assistant server is not situated anywhere close to the smart meter. As such, we need a kind of "relay" that will relay this kind of information over the network to Home Assistant. I ended up using a Raspberry Pi B+, but any single-board computer with a USB port that is capable of running Linux should work. Make sure you have all the necessary accessories such as a power adapter, SD card and Wi-Fi dongle (if needed).

Flashing and setting up Linux

As mentioned earlier, I am using a Raspberry Pi B+ as my relay. We are firstly going to flash Raspberry OS, a version of Debian made to run on Raspberry Pi systems, onto a (micro) SD card. The easiest way of doing this is using the Raspberry Pi Imager, which can be downloaded from the Raspberry Pi website. When attempting to flash, you will be prompted to configure some settings as well. Use this opportunity to insert your Wi-Fi credentials, username/password and enable SSH. This will greatly simplify the following steps. Save and flash the software when you are ready.

Now, insert the (micro) SD card you just flashed into the Raspberry Pi and wait for it to boot up and connect to the network. This can take up to 10 minutes. Now, log into your router to find the IP address of your Raspberry Pi. The URL and login details of your router can usually be found on the underside of your router. The instructions on where to find a list of devices connected to the network inside of your router's administration differ between routers, so you will need to do some of your own research there.

Once you have found the IP address of your Raspberry Pi, it is time to connect to it over SSH. SSH allows us to run commands on the Pi, and basically access its terminal. Linux and Mac users can simply open the terminal application on their computer. On Windows, you will first need to install OpenSSH Client, after which you can then open Powershell or the Command Prompt. For simplicity's sake, I will refer to the terminal on Linux/Mac and Powershell/Command Prompt both as "terminal" henceforth.

Inside of your terminal, connect to your Raspberry Pi (or similar single-board computer) using the following command. bash ssh [email protected] username, in this case, is the username you set while flashing the device (or the default username if you did not set any). 192.168.x.x is the IP address of that device on the network.

Now, press enter! You might be prompted for a password (or not, if you're fancy and set up key authentication). That password is simply the one you set up, or the default password of the image you used to flash the device. Enter the password (it is normal no character appear on screen as you type) and press enter.

Before we do anything else, we will want to make sure that all the software is up-to-date. When using Raspberry OS, simply run the following command and enter your password if prompted. bash sudo apt update -y && sudo apt upgrade -y Once these commands have run, we are ready to roll.

Setting up ser2net

ser2net is a piece of software that allows us to relay serial information over the network, where it can then be picked up by Home Assistant. On Raspberry OS, this software is not included by default, so we'll go ahead and install it first.

sudo apt install ser2net

Next, we will need to configure the software. We do this by editing a configuration file located at /etc/ser2net.yaml. We will use Nano to edit this file, which is included by default on Raspberry OS. Simply run the following (you might need to enter your password).

sudo nano /etc/ser2net.yaml

Now that we are editing, this file, make sure that its contents match the ones below. This will involve removing most of the default configuration and tweaking what remains.

%YAML 1.1
---
# This is a ser2net configuration file, tailored to be rather
# simple.
#
# Find detailed documentation in ser2net.yaml(5)
# A fully featured configuration file is in
# /usr/share/doc/ser2net/examples/ser2net.yaml.gz
# 
# If you find your configuration more useful than this very simple
# one, please submit it as a bugreport

define: &banner \r\nser2net port \p device \d [\B] (Debian GNU/Linux)\r\n\r\n

# Example /etc/ser2net.yaml for proxying USB/serial connections to DSMR smart meters
connection: &con0096
    accepter: tcp,2001
    enable: on
    options:
      banner: *banner
      kickolduser: true
      telnet-brk-on-sync: true
    connector: serialdev,
              /dev/ttyUSB0,
              115200n81,local

In the above configuration file, /dev/ttyUSB0 represents the USB-to-serial adapter that you are using. Should you encounter problems later, and have attached multiple USB serial devices, it can be worth incrementing this value and trying again (e.g. /dev/ttyUSB1, /dev/ttyUSB2, etcetera).

Press Ctrl + O, followed by Enter to save the file. We will now restart the system, and from there we should be ready to move on. On Raspberry OS, restarting can be done as follows.

sudo reboot

Once that is done, you can now close the terminal and open up Home Assistant.

Setting up Home Assistant

We are almost there! We will now set up Home Assistant to read the serial data from the network relay we set up. To get started, simply follow this link to automatically get taken to the correct page. Alternatively, go to "Devices and integrations" in your Home Assistant settings, add a new integration and look for the "DSMR Slimme Meter" integration. Go through the setup wizard.

For the connection type, choose network. Only choose serial if the USB to serial adapter is connected directly to the device running Home Assistant.

On the following screen, we will want to enter the IP address of the Raspberry Pi (or other single board computer) we just set up (shown as 192.168.x.x in the example). The port should be equal to 2001. As for the DSMR version, most smart meters will be using version 5. However, if your smart meter is especially old, or you live in another country with a variant of DSMR like Belgium or Luxemburg, you may want to go over the integration set up wizard again and try the other DSMR versions if version 5 fails to set up. If you want to be sure, look if the DSMR version is marked anywhere on your smart meter.

Continue and wait for a moment. With any luck, the configuration is successful and the data should begin to propagate over Home Assistant! You should now be able to check your smart meter's info.

In a few hours, energy and gas information should show up in Home Assistant's "Energy" dashboard. This gives you a nice overview of the energy being used in your home.

Improvements

In our set up, the Raspberry Pi is used to relay the serial data from the P1 port to the Home Assistant server over the network. We also established that this isn't a very intensive task to do. This means that a Raspberry Pi is somewhat "overkill" for what we are using it for.

For the future, it might be useful to see if a simpler and cheaper device such as an ESP32 can also take USB serial data and relay that over the network. This comes with two benefits. Firstly, it is much easier to replicate this setup. The hardware is a lot more affordable, and the software can simply be flashed onto the device directly. Secondly, because it is a much simpler device, it takes up less space and uses less power.

Bonus: Smart Plugs for Individual Device Monitoring

To get a more precise idea of what exactly is using power at the moment, it can be worth considering looking into smart power plugs with energy monitoring. This is basically a plug that sits in-between the power outlet. You can use this to control and measure power to whatever device is attached to it. On AliExpress, I was able to find Zigbee smart power plugs with energy monitoring for around $ 5 including shipping, with Wi-Fi variants being cheaper. This means that buying a bunch of power plugs for around the house isn't very expensive either, especially if many of your devices are already plugged into power strips as is the case in my house hold. Do keep in mind though that that not every smart plug is accurate or updates frequently. Make sure to look up reviews first if possible, though especially with places like AliExpress, it'll always be a bit of a gamble.