Read Smartmeter P1 using Raspberry Pi and Node-RED

I’ve spend many hours to get my P1 smartmeter working with Node-RED on a Raspberry PI.
In my setup the P1 Smartmeter data is send over MQTT in a json payload and insert in a mongoDB, but feel free to use other outputs to send the payload.

In the tutorial below i asume you’re familiar with the P1 port, have connected to the the Pi and read data from it before.
So i only explain how to configure Node-RED to read the serial/tty port and extract the P1 data from the serialstream and send the output as payload with Node-RED.

Full Node-RED flow for reading P1 Smartmeter data
  1. Add the “Serial input” node to the flow and configure it as below where /dev/ttyUSB0 needs to correspondent to your tty port.

  2. Add a “Function” node and add the code below to the function node, outputs needs to be set to 1.
    var regExpDeliveredKWTariff1 = /^1-0:1\.8\.1\(0+(\d+\.\d+)\*kWh\)/m;
    var regExpDeliveredKWTariff2 = /^1-0:1\.8\.2\(0+(\d+\.\d+)\*kWh\)/m;
    var regExpGeneratedKWTariff1 = /^1-0:2\.8\.1\(0+(\d+\.\d+)\*kWh\)/m;
    var regExpGeneratedKWTariff2 = /^1-0:2\.8\.2\(0+(\d+\.\d+)\*kWh\)/m;
    var regExpCurrentKWTariff = /^0-0:96.14.0\(0+(.*?)\)/m;
    var regExpCurrentWattDelivered = /^1-0:1.7.0\((.*?)\*/m;
    var regExpCurrentWattGenerated = /^1-0:2.7.0\((.*?)\*/m;
    var regExpDeliveredGasM3 = /^0-1:24\.2\.1\(\d+[SW]\)\((\d+\.\d+)\*m3\)/m;
    
    var DeliveredKWTariff1 = regExpDeliveredKWTariff1.exec(msg.payload);
    var DeliveredKWTariff2 = regExpDeliveredKWTariff2.exec(msg.payload);
    var GeneratedKWTariff1 = regExpGeneratedKWTariff1.exec(msg.payload);
    var GeneratedKWTariff2 = regExpGeneratedKWTariff2.exec(msg.payload);
    var CurrentKWTariff = regExpCurrentKWTariff.exec(msg.payload);
    var CurrentWattDelivered = regExpCurrentWattDelivered.exec(msg.payload);
    var CurrentWattGenerated = regExpCurrentWattGenerated.exec(msg.payload);
    var DeliveredGasM3 = regExpDeliveredGasM3.exec(msg.payload);
    
    msg.payload = '{"smartmeter":' + '{"timestamp":' + Math.round(new Date().getTime()/1000.0) + ', ' + '"DeliveredKWTariff1":' + DeliveredKWTariff1[1] + ', ' + '"DeliveredKWTariff2":' + DeliveredKWTariff2[1] + ', ' + '"GeneratedKWTariff1":' + GeneratedKWTariff1[1] + ', ' + '"GeneratedKWTariff2":' + GeneratedKWTariff2[1] + ', ' + '"CurrentKWTariff":' + CurrentKWTariff[1] + ', ' + '"CurrentWattDelivered":' + CurrentWattDelivered[1]*1000 + ', ' + '"CurrentWattGenerated":' + CurrentWattGenerated[1]*1000 + ', ' + '"DeliveredGasM3":' + parseFloat(DeliveredGasM3[1],10) + '}'+ '}';
    
    return msg;
    Connect an desired "Output" node to the function node and you're ready to send the payload...

As told i’m sending the filtered P1 json payload to a MQTT server and to mongoDB. Below an example how it looks like:

 

Leave a Reply

Your email address will not be published. Required fields are marked *