volkszaehler / mbmd

ModBus Measurement Daemon - simple reading of data from ModBus meters and grid inverters
BSD 3-Clause "New" or "Revised" License
229 stars 81 forks source link

Feature request: JSON support #255

Open RichieB2B opened 2 years ago

RichieB2B commented 2 years ago

mbmd is really cool and almost exactly what I was looking for. Currently it is publishing each measurement in it's own MQTT topic like this:

mbmd/orno3p1-1/ReactivePower 7.000
mbmd/orno3p1-1/ApparentPower 8.000
mbmd/orno3p1-1/Voltage/L1 234.500
mbmd/orno3p1-1/Frequency 50.000
mbmd/orno3p1-1/Current/L1 0.000
mbmd/orno3p1-1/Cosphi/L1 0.000
mbmd/orno3p1-1/Voltage/L2 233.200
mbmd/orno3p1-1/Current/L2 0.000
mbmd/orno3p1-1/Cosphi/L2 0.000
mbmd/orno3p1-1/Current/L3 0.040
mbmd/orno3p1-1/Voltage/L3 235.400
mbmd/orno3p1-1/Cosphi 0.420
mbmd/orno3p1-1/Cosphi/L3 0.440
mbmd/orno3p1-1/Sum/L1 164.530
mbmd/orno3p1-1/Sum 495.800
mbmd/orno3p1-1/Sum/L3 166.930
mbmd/orno3p1-1/Sum/L2 164.340
etc

It would be great if mbmd has an option to publish all measurements in a JSON message in a single topic like this:

(newlines added for readability)
mbmd/orno3p1-1 {
  "Power": {
    "Reactive": 7,
    "Apparent": 8
  },
  "Voltage": {
    "L1": 234.5,
    "L2": 233.2,
    "L3": 235.4
  },
  "Frequency": 50,
  "Current": {
    "L1": 0,
    "L2": 0,
    "L3": 0.04
  },
  "Cosphi": {
    "L1": 0,
    "L2": 0,
    "L3": 0.44,
    "Total": 0.42
  },
  "Sum": {
    "L1": 164.53,
    "L2": 164.34,
    "L3": 166.93,
    "Total": 495.8
  }
}

This matches more with other uses of MQTT that I have seen. Also, it would mean tools like mqtt2prometheus would also work with the MQTT output.

andig commented 2 years ago

Happy to take a PR. Should either be an option or just published additionally.

RichieB2B commented 2 years ago

I would if I had any experience with writing Go which I unfortunately don't. The required code is quite similar to allDevicesHandler() in http.go which probably could be called from Run() in mqtt.go

RichieB2B commented 2 years ago

I solved my need (getting mbmd data into prometheus) using this small script:

#!/bin/bash

while [ 1 ]; do
  curl -s http://localhost:8080/api/last/ORNO3p1.1 | mosquitto_pub -t mbmd/orno3p1-1 -l
  sleep 5
done

And this mqtt2prometheus config.yml:

mqtt:
  server: tcp://127.0.0.1:1883
  topic_path: mbmd/orno3p1-1
  qos: 0
cache:
  timeout: 1h
metrics:
  - prom_name: mbmd_power
    mqtt_name: Power
    help: power consumption in Watt
    type: gauge
  - prom_name: mbmd_usage
    mqtt_name: Import
    help: energy usage in kWh
    type: counter
  - prom_name: mbmd_currentL1
    mqtt_name: CurrentL1
    help: L1 current in Ampere
    type: gauge
  - prom_name: mbmd_currentL2
    mqtt_name: CurrentL2
    help: L2 current in Ampere
    type: gauge
  - prom_name: mbmd_currentL3
    mqtt_name: CurrentL3
    help: L3 current in Ampere
    type: gauge

It would still be nice though to have mbmd publish JSON data to MQTT.