General MQTT Thing: JSONPATH transform incoming value format field is missing


I am having trouble formatting a number extracted within the general MQTT thing from a JSON string using a JSONPATH transformation.

For example, my electricity meter outputs the following string to a Mosqitto MQTT broker:


I created an “Electricity Meter” thing with an “Energy” channel :

UID: mqtt:topic:29be605c8c:2e5f28b0fe
label: Electricity Meter
thingTypeUID: mqtt:topic
  payloadNotAvailable: offline
  availabilityTopic: smartmeter/status
  payloadAvailable: online
bridgeUID: mqtt:broker:29be605c8c
  - id: Energy
    channelTypeUID: mqtt:number
    label: Energy
    description: ""
      stateTopic: smartmeter/state
      transformationPattern: JSONPATH:$.[0].energy

The channel configuration dialog allows me to specify a JSONPATH transformation JSONPATH:$.[0].energy to extract the energy value from the JSON string. This is all very good. However, I am missing a field “Incoming Value Format” in the configuration dialog, similar to the “Outgoing Value Format” field:

Currently I am getting the follwing output, which has a ridiculously number of decimal places:

I principle, the JSONPATH transformation could be applied later when linking the channel to an item. The JSONPATH option is the profile dialog has a “State Formatter” field, which I was looking for earlier in the MQTT thing dialog. However, when given the transformation here, it is not applied and I get “NONE” as output.

The only way this works is when I do the JSONPATH transformation in the MQTT channel dialog as show above, and add a “State Description” to the energy “Item” with a “Pattern”. However, the pattern overrides the “unit of measurement” given in the MQTT channel dialog, but finally formats the number as I want:

Overall the option to enter a number format is not very consistent. I feel there should be a possibility to format the number right in the general MQTT thing dialog before it is passed on to the main.ui. I am using openHAB 3.2.0 Release Build in the official docker container under aarch64 (Odroid C2).

Any idea how this could be handled differently? I am missing something here?

That’s because there isn’t one. The format is a place where the value being published can be sightly formatted before it’s published (e.g. remove decimal places).

One thing a lot of people don’t realize is that not every number can be represented using floating point numbers on computers. There are a bunch that can only be represented by values that are really close to the “real” value.

This presents a problem because the user usually doesn’t want to see all those decimal places but if you truncate or round the value before showing it to the user, before too long any calculations you do with that rounded value is going to accumulate so many errors as to result in widely inaccurate values.

For this reason, in OH, the place where a value is formatted for display to the human is not in the binding. Instead it’s at the Item, in specific the Item’s State Description metadata. You can set a %.2f there to round to two decimal places when the Item is shown to you in MainUI.

Unfortunately this is a known issue with the MQTT binding which causes problems with units like this. However, I think this should work if you remove the units from the MQTT Channel config and apply it in the State Description only. If you need to change the units, you need to use a Proxy Item or a JS Transformation profile between the Channel and the Item.