Normalising value unit of measure in MQTT topic

Hello - recently recevied a Glow Display CAD, and have it sending import, export energy and power into MQTT. Somewhat annoyingly however, the value’s unit of measure can switch between watts and kilowatts (and presumably kilowatt hours and megawatt hours). The unit of measure is available in the same JSON object as the value. Is there a simple way of normalising the value’s unit of measure when the MQTT topic is read and income transformation is applied? Or do I need to have an intermediate item which a rule then converts into watts?

Example output below:

{
  "electricitymeter": {
    "timestamp": "2025-07-21T20:36:58Z",
    "energy": {
      "export": {
        "cumulative": 242.531,
        "units": "kWh"
      },
      "import": {
        "cumulative": 9033.732,
        "day": 11.733,
        "week": 11.733,
        "month": 301.423,
        "units": "kWh",
        "mpan": "redacted",
        "supplier": "redacted",
        "price": {
          "unitrate": 0.3145,
          "standingcharge": 0.6584
        }
      }
    },
    "power": {
      "value": 0.555,
      "units": "kW"
    }
  }
}

It migt be possible to extract the value and the units as one return using JSONPATH but I’m not good enough to figure that out quickly.

But you can use a script transformations to do it. The following is in JS to get at the power.

(function(input){
    const parsed = JSON.parse(input);
    return parsed.electricitymeter.power.value + ' ' parsed.electricitymeter.power.units;
})(input)

Given your JSON above that will return "0.555 kW".

I think you will want to remove the units property from your MQTT Thing if you are using it. But then you will want to make sure to use an appropriate Item type (e.g. Number:Power) and set the unit metadata to what you want to normalize to (e.g. W).

With this configuration your Item will always be converted to W no matter what unit the meter reports.

This could be made into a one-liner to use as an inline transform, but you’ll have to parse the input twice.

JS | JSON.parse(input).electricitymeter.power.value + ' ' + JSON.parse(input).electricitymeter.power.units

OH JSONPath is based on Jayway, so the concat function should be an option (although I’ve never tried it).

The JSONpath transform should look something like this:

$.concat($.electricitymeter.power.value," ",$.electricitymeter.power.units)
2 Likes