npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

p1-monitor

v1.0.4

Published

Module to monitor a Smart Meter using its P1 port

Downloads

10

Readme

About

This project provides a simple library for reading data from a smart meter using its P1 port. It is written to be used with a P1 cable that transmits the P1 telegrams as a serial message over USB.

Those messages are then converted into a structured object and emitted as an event.

Getting started

Installation

Install the library using npm:

npm install p1-monitor

Now we can create a new P1Monitor instance by calling the factory function:

import { createP1Monitor } from 'p1-monitor';

const monitor = createP1Monitor({
    timezone: 'Europe/Amsterdam',
    path: '/dev/ttyUSB0',
    baudRate: 115200,
});

Before we start receiving events, we need to call the start method on the monitor:

monitor.start();

Configuration

The telegram parser can be configured to work with different kinds of smart meters. The default configuration adheres to the DSMR 5.0 specification, and can be changed by passing additional options to createP1Monitor().

Required options

type options = {
    /**
     *  The IANA timezone identifier, configured in your Smart Meter, that will
     *  be used when parsing the timestamps of the P1 messages.
     *
     *  Eg: "Europe/Amsterdam" or "America/New_York"
     */
    timezone: string;

    /**
     * The path to where the P1 cable is mounted.
     */
    port: string;

    /**
     * The baud rate to use when reading from the serial port. Most P1 cables
     * use 115200.
     */
    baudRate: number;
};

Additional options

type options = {
    /**
     * Whether to return a value with their unit, if specified.
     *
     * Defaults to: false.
     */
    withUnits?: boolean;

    /**
     * Whether the date values are returned as a Luxon DateTime,
     * or as the default JS Date object.
     *
     * Defaults to: false.
     */
    asLuxon?: boolean;

    /**
     * A timeout, in milliseconds, after which the last message is received, to
     * consider the serial port connection closed.
     *
     * Defaults to: 11 seconds.
     */
    timeout?: number;

    packet?: {
        /**
         * The character that denotes the start of the data in a P1 message.
         *
         * Defaults to: `/`
         */
        startChar?: string;
        /**
         * The character that denotes the end of the data in a P1 message.
         *
         * Defaults to: `!`
         */
        stopChar?: string;
    };
};

Usage

The P1Monitor will emit the following events:

  • connected: Emitted when the first message is received by the P1 monitor.
  • data: Emitted when a new message is received on the serial port.
  • error: Emitted when an error occurs.
  • close: Emitted when the serial port connection was closed.

Now, we can start listening to our monitor:

import { P1Packet } from './P1Packet';

monitor.on('data', (packet: P1Packet) => {
    console.log(packet);
});

The data event is emitted with a P1Packet object, which will look something like:

{
    "vendor_id": "Ene",
    "model_id": "XS210 ESMR 5.0",
    "version": "50",
    "transmitted_at": "2024-01-06T10:19:56.000Z",
    "electricity": {
        "equipment_id": "E1234567891234567",
        "tariff": 1,
        "received": {
            "tariff1": 2601.66, // kWh
            "tariff2": 2960.167, // KWh
            "active": 0.279 // kW
        },
        "delivered": {...},
        "active": {...},
        "failures": {...},
        "sags": {...},
        "swells": {...}
    },
    "gas": {
        "equipment_id": "G1234567891234567",
        "measured_at": "2024-01-06T10:15:00.000Z",
        "received": 5472.258 // m3
    }
}