JSONPATH in MQTT Thing definition

Tags: #<Tag:0x00007f616d809d20> #<Tag:0x00007f616d809c58>

I have a question regarding JSONPATH transformation pattern applied in a MQTT Thing definition.
This is the enviroment I’m referring to:

my.things file

Bridge mqtt:broker:mymqtt [ 
    host="x.x.x.x", 
    port="1883", 
    secure=false, 
    qos=0,
    clientID="openhab2", 
    keep_alive_time=30000,
    reconnect_time=60000,
    username="xxxxxx", 
    password="xxxxxx"
    ]
{
    Thing topic homeRouterInfo "Router" @ "MQTT"  {
    Channels:
        Type string : DEVSSTATUS "Presenza Dev" [
            stateTopic="RTR/RTRINFO",
            transformationPattern="JSONPATH:$.devs"
         ]
    }
}

my.items file

String presDevCheck "Stato globale presenza device" { channel="mqtt:topic:homeRouterInfo:DEVSSTATUS" }`

my.rules file

rule "Risposta presenza device"
    when
        Item     presDevCheck changed
    then
        val String dev1 = transform("JSONPATH", "$.iPhonex1", presDevCheck.state)
        val String dev2 = transform("JSONPATH", "$.iPhonex2", presDevCheck.state)

end

Given the following json published by the topic RTR/RTRINFO:

{"wanstatus": "up", "tx_bw": 1.4698666666666667, "devs": {"iPhonex1": "OFF", "iPhonex2": "OFF"}, "rx_bw": 0.7141333333333333, "wanuptime": "1 day, 12:16, 31 secs", "version": "chkrouterV4", "wanip": "x.x.x.x", "elapsed": 0.1884160041809082, "updateint": 15}`

value of item presDevCheck is the following:

{iPhonex1=OFF, iPhonex2=OFF}

I would have expected colons in place of equal signs:

{iPhonex1:OFF, iPhonex2:OFF}

so that the above rule could work properly, assigning the string OFF to both dev1 and dev2.
Instead the the rule assigns the string {iPhoneRG=OFF, iPhoneAB=OFF} to both dev1 and dev2.
I know how to correct the rule so that it could work right, but my question is if the above depicted behavior is correct for the jsonpath transformation pattern applied in the thing definition or not.
:thinking:

Why don’t you create 2 switch channels instead?

{
    Thing topic homeRouterInfo "Router" @ "MQTT"  {
    Channels:
        Type string : DEVSSTATUS "Presenza Dev" [
            stateTopic="RTR/RTRINFO",
            transformationPattern="JSONPATH:$.devs"
         ]
        Type switch : IPhonex1Status "IPhonex1 Status" [
            stateTopic="RTR/RTRINFO",
            transformationPattern="JSONPATH:$.devs.iPhonex1"
         ]
        Type string : IPhonex2Status "IPhonex2Status" [
            stateTopic="RTR/RTRINFO",
            transformationPattern="JSONPATH:$.devsiPhonex2"
         ]
    }
}

items

String presDevCheck "Stato globale presenza device" { channel="mqtt:topic:homeRouterInfo:DEVSSTATUS" }
Switch Iphonex1_Status "I Phonex1 Status" { channel="mqtt:topic:homeRouterInfo:IPhonex1Status" }
Switch Iphonex2_Status "I Phonex2 Status" { channel="mqtt:topic:homeRouterInfo:IPhonex2Status" }

Hi @vzorglub,
thank you for the answer but the question is not how to handle the specific situation.
I put the question because I would like to know if the value returned by the jsonpath in the above example is correct or there is something I didn’t understand.

In the real scenario I have, the node "devs": {"iPhonex1": "OFF", "iPhonex2": "OFF"} of the json is the periodic answer given by a certain script to a question made previusly by OH2.

The question contains one or more couple of ids and MACs (ie: ```iPhonex1, 00:11:22:33:44:55"). Ids and MACs couples can change time by time.

When the answer is received, it contains again couples of ids with the relative “state” as “calculated” by the script.

The ids is then used to address the right item in OH2 (the items follow a well defined naming convention).

For those reasons I prefer not to statically code two (or more) channels :smile:

Then you are correct, the transformation is not returning a “sub-json” but a hybrid type of string.
I think this is a bug. @David_Graeff have you seen this before?

What you could do is this:

rule "Risposta presenza device"
    when
        Item     presDevCheck changed
    then
        val String jsonString = presDevCheck.state.toString.replace("=", ":")
        val String dev1 = transform("JSONPATH", "$.iPhonex1", jsonString)
        val String dev2 = transform("JSONPATH", "$.iPhonex2", jsonString)
end

Thank you @vzorglub, I’ve coded exactly that line! :grinning: