Howto: P1 Meter monitoring in CMK

I recently bought myself a P1 meter from https://www.homewizard.com/nl/p1-meter/, to obtain insight into my power-usage.

The P1 meter offers via http API information in the format of a json array(s) (example below)

{
  "wifi_ssid": "my_ssid",
  "wifi_strength": 100,
  "smr_version": 50,
  "meter_model": "Landis + Gyr GF0010453483287",
  "unique_id": "my_unique_power_meter_id",
  "active_tariff": 1,
  "total_power_import_kwh": 85210.941,
  "total_power_import_t1_kwh": 45447.99,
  "total_power_import_t2_kwh": 39762.951,
  "total_power_export_kwh": 1848.317,
  "total_power_export_t1_kwh": 666.244,
  "total_power_export_t2_kwh": 1182.073,
  "active_power_w": 1580,
  "active_power_l1_w": 258,
  "active_power_l2_w": 1276,
  "active_power_l3_w": 46,
  "active_voltage_l1_v": 228.6,
  "active_voltage_l2_v": 225.4,
  "active_voltage_l3_v": 229.9,
  "active_current_l1_a": 1.129,
  "active_current_l2_a": 5.661,
  "active_current_l3_a": 0.2,
  "voltage_sag_l1_count": 2503,
  "voltage_sag_l2_count": 18750,
  "voltage_sag_l3_count": 57735,
  "voltage_swell_l1_count": 649,
  "voltage_swell_l2_count": 123,
  "voltage_swell_l3_count": 6,
  "any_power_fail_count": 8,
  "long_power_fail_count": 11,
  "total_gas_m3": 11668.551,
  "gas_timestamp": 240828023005,
  "gas_unique_id": "my_unique_gasmeter_id",
  "external": [
    {
      "unique_id": "my_unique_gasmeter_id",
      "type": "gas_meter",
      "timestamp": 240828023005,
      "value": 11668.551,
      "unit": "m3"
    }
  ]
}

The reason i bought this P1 meter is i also have solar panels+converter that switches off with a ‘Grid Overload’ alarm.
But without data/insight i am unable to trace a/the problem and solve it.

As the P1-Meter cannot run an agent i have made (with documentation at https://docs.checkmk.com/latest/en/datasource_programs.html) a datasource python script to get the data in CMK.

The below script should be placed in ~/local/bin/ and made executable.

#!/usr/bin/env python3

# This file should be placed in ~/local/bin/ and used for a monitoring host that has a/the IP of the device
# in combination with a rule for "individual program call instead of agent" as described on the official docs of CheckMK:
# https://docs.checkmk.com/latest/en/datasource_programs.html
#
# The commandline to be used in CheckMK is : this_script_name.py $HOSTADDRESS$

import sys
import json
import requests

address = sys.argv[1]
response = requests.get('http://{}/api/v1/data'.format(address))
parsed = response.json()

# Using For Loop to iterate thru the data offered by the P1-API
# Threshold values are tuned to my own setup, they might need ajustment for others.

def iterate_json(json_obj):
    for key, value in json_obj.items():
        if isinstance(value, dict):
            iterate_json(value)
        else:
            if key.startswith("active"):
                if key.endswith("_w"):
                    print(f"P {key} watt={value};2100;2200;2300;0;2500 Current Watts:{value}")
                if key.endswith("_v"):
                    print(f"P {key} volt={value};252;253;260;0;300 Current Volts:{value}")
                if key.endswith("_a"):
                    print(f"P {key} amps={value};8;10;15;0;20 Current Amps:{value}")
                if key.endswith("tariff"):
                    print(f"P {key} tariff={value};;;;; Current Tariff:{value}")

# Additional information is available, but not at current needed in my case
#            if key.startswith("external"):
#                continue
#            else:
#                print(f"0 {key} - {value}")

print('<<<local>>>')
iterate_json(parsed)

Configuration in CMK : create a rule for individual program call instead of agent for the P1 host:

The above is a 1st attempt , and should be seen as a raw attempt in getting the info whilst flexing/developing my python insights.
As you can see the threshold values are hard-coded, and tuned to my environment.
Also a lot of static (additional-) data i have omitted in the output as in my case i am purely interested in the voltage, watts and amps of my 3-phase powermeter.

I just wanted to share this, as the chance that i am the only one in the world facing this ‘challenge’ are very remote :slight_smile:

  • Glowsome
1 Like

Hi Michael,

while you could put your device into CheckMK, have you had a glance to take a peak into a software called “Homeassistant”? There’s a ready-to-use integration for your device and you can do lots of other fun stuff with it.

Here’s a link to the integration:

Regards,
Simon (a cmk and homeassistant user)

1 Like

Hi Simon,

Your suggestion is highly appreciated :+1:
However:

  • i was unwilling to erect/install another box with Home Assistant on it for this ‘one’ usage (bit of an overkill for just this P1 Meter)
  • I’m still exploring my own capabilities in dealing with presented input from a (in this case non-Agent-capable) device by means of either bash or python scripts in possibilities with CheckMK.

So yes, maybe in the future, when more devices are eligible in my home it will most definitely be considered as a way to go for Home Assistant.

But for now my little crude howto above has found its place neatly in my CheckMK system alongside the other 73 hosts / 2186 service (so total with the above is 74 hosts, 2200 services)

  • Glowsome

Hi all,

Just to keep things aligned, i have chosen to no longer post code-updates regarding this (and others) due to me having to maintain updates in a/the post.

Instead use the link to my git-repository: https://git.comsolve.nl/Comsolve/Checkmk-Development
The repository holds all (code-)changes i make to (local-)checks, datasources and/or Special agents.

  • Glowsome