Mqtt Transformation

Hi,
I would like to change a channel from my Mosquitto Broker to a Location Item. In the channel comes the following:

{“battery_level”:29,“longitude”:13.7879,“latitude”:53.624542,“altitude”:84.4000015258789,“gps_accuracy”:20,“tst”:1648498100}

I thought I could insert the values for longitude, latitude and altitude as comma separated values into a location item by inserting the following per JSONPath at the Incoming Value Transformations item:

$.longitude, $.latitude, $.altitude

I have also tried:

[$.longitude, $.latitude, $.altitude]
$.longitude∩$.latitude∩$.altitude

Unfortunately it does not work I always get the following error

[WARN ] [rnal.handler.GenericMQTTThingHandler] - Channel configuration error

java.lang.IllegalArgumentException: The transformation pattern must consist of the type and the pattern separated by a colon

How can I transform it correctly?

Unfortunately, I don’t think it’s possible to do this in one go. You’ll have to extract each parameter into separate Items, and then combine using a rule.

Alternatively a JavaScript transformation can be used, that’s more flexible

1 Like

It means e.g.
[$.longitude, $.latitude, $.altitude]
will not do, you need to tell it which transformation service to use
JSONPATH:[$.longitude, $.latitude, $.altitude]
or
JSONPATH:this∩JSONPATH:that
which will satisfy the syntax, but as you have found will not do what you want as the transformation service is limited to single-string answers, and chaining with ∩ pipes one output into the next input.

As already suggested, use JS instead to extract and manipulate into the single comma string that you want.

Thanks for your answers, i made a rule to transform it

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: GeolocationMQTT_DanielRohdaten
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: >-2
            val String json = (GeolocationMQTT_DanielRohdaten.state as StringType).toString
            {
        		val String lat  = transform("JSONPATH", "$.latitude", json)
        		val String lon  = transform("JSONPATH", "$.longitude", json)
        		val String acc  = transform("JSONPATH", "$.gps_accuracy", json)
        		val String batt = transform("JSONPATH", "$.battery_level", json)
        		mqtt_PhoneDaniel_Location.postUpdate(lat + "," + lon)
        		mqtt_PhoneDaniel_Accuracy.postUpdate(acc)
        		mqtt_PhoneDaniel_Battery.postUpdate(batt)
        		mqtt_PhoneDaniel_LastUpdate.postUpdate(new DateTimeType())		
        	}
    type: script.ScriptAction