Error using JSONPATH in the .items file for a Number item

Greetings! I’m using OpenHAB 2.4.0 on Windows 10 and I need some help with JSONPATH in the .items file for a Number item. I’m close and got to this point by reading every bit of documentation I could set my eyes on, but now I’m stuck. I have configured a MQTT Broker and Channel using PaperUI, and I would like to stick with it, if possible. I have 1 item named Payload that is a String and displays the entire payload received from the broker:

“AmpHr”: 3.83, “Amps”: 49.6, “DaysSinceCharged”: 0.0, “DaysSinceEqualized”: 0.0, “FilteredAmps”: 49.6, “FilteredVolts1”: 29.8, “PercentFull”: 100.0, “Volts1”: 29.8, “Volts2”: 0.0, “Watts”: 1480.0, “Counter”: 57785}

Before attempting to create the items in .items, I have confirmed that setting the Incoming Value Transformation of a new channel to ‘JSONPATH:$.Counter’ will, in fact, return the value of Counter as a Number in the item. However, I would rather create items in conf so that I do not need to create a new channel for each item in the payload (doing this in text editor would be much quicker). I found that adding this in .items works fine for a string:

String XFormTestString “XFormTestString” { channel=“mqtt:topic:a5ed7cf5:Payload”[profile=“transform:JSONPATH”, function="$.Counter"] }

However, I want the item to be a number, but this does not work:

Number XFormTestNum “XFormTestNum " { channel=“mqtt:topic:a5ed7cf5:Payload”[profile=“transform:JSONPATH”, function=”$.Counter", sourceFormat="%d"] }

Instead, I get an error in openhab.log:

2019-10-27 13:10:23.330 [WARN ] [ofiles.JSonPathTransformationProfile] - Could not transform state ‘{“AmpHr”: 3.83, “Amps”: 49.6, “DaysSinceCharged”: 0.0, “DaysSinceEqualized”: 0.0, “FilteredAmps”: 49.6, “FilteredVolts1”: 29.8, “PercentFull”: 100.0, “Volts1”: 29.8, “Volts2”: 0.0, “Watts”: 1480.0, “Counter”: 58852}’ with function ‘$.Counter’ and format ‘%d’

Thoughts on how to do the transform in .items and set it to a Number? Thanks in advance.

Matt

  • Platform information:
    • OS: Windows 10
    • Java Runtime Environment: Oracle jre1.8.0_231
    • openHAB version: 2.4.0

Transform profile is (currently) limited to String outputs.
Similar thread -

Enhancement request -
https://github.com/openhab/openhab2-addons/issues/5750

In addition to the ultimate limitation of profiles, I think the sourceFormat="%d" is the source of this specific error. sourceFormat is formatting that is applied to the string before it get’s passed to the transform. The incoming String is JSON and cannot be formatted as an integer (%d).