Concatenate strings with JSONPATH

  • Platform information:
    • openHAB version: 2.5.9

I am trying to use OwnTracks on my phone with a simple MQTT channel.

Thing mqtt:topic:server:owntracks  "OwnTracks"
     (mqtt:broker:server) @ "People"
        Type location : sergis_phone_location "Sergi's Phone"
                stateTopic = "owntracks//phoneSergi", transformationPattern="JSONPATH:concat($.lat$,\",\",.lon)"

According to the documentation, the location must be a comma-separated string. I am trying to concatenate lat and lon using a JSONPATH expression but that throws an error:

16:37:45.369 [WARN ] [tt.generic.ChannelStateTransformation] - Executing the JSONPATH-transformation failed: Invalid path 'concat($.lat$,",",.lon)' in '{"_type":"location","acc":83,"alt":154,"batt":60,"conn":"w","inregions":["Home"],"lat":XX.XXX,"lon":Y.YYY,"t":"u","tid":"1","tst":1603463865,"vac":2,"vel":1}'

Is there any way to map that json I receive to a Location item?

Thank you!

I’ve never seen a JSONPATH in OH like that. I do know that OH does not support full JSONPATH and perhaps concat is not supported. All I can recommend is to either use a Rule or use the GPSTracker add-on which works with OwnTracks natively without MQTT.

You might use a javascript transformation to parse JSON, pull out two bits, and stick them together. You do have to return a single string, but that’s fine here.

1 Like

I use this binding for owntracks. It took me as long to setup as write this post.

Thank you all for the suggestions.
I wanted to avoid using a dedicated binding, but I’ve seen GPSTracker has also some nice additions.

Ok then there is always another way :wink:

You have this to an Item I presume?

Lets assume you have an item named mqttPositionRaw that has that json string in it. You also have a location item MY_location

You can have a Rule something like

rule "Mqtt Postion Parse"
    Item mqttPositionRaw changed
    val String json = (mqttPositionRaw.state as StringType).toString
    val String type = transform("JSONPATH", "$._type", json)
    if (type == "location") {
      val String lat  = transform("JSONPATH", "$.lat", json)
      val String lon  = transform("JSONPATH", "$.lon", json)

      MY_location.postUpdate(new PointType(lat + "," + lon))
1 Like