[MQTT][JSONPATH] Howto transform mqtt message to location

Receiving the following message from TTN (abbreviated):

{"payload_fields":
{"Latitude":0,"Longitude":0}}

How get I transform them to a location item?

With

JSONPATH:$.payload_fields.Latitude

or

JSONPATH:$.payload_fields.Longitude

I can get the individual values. But how should the transform look like to get a result that is valid for a location type item?

Have you tried it with $.[Latitude,Longitude] - should return [0, 0]

Hope that helps - not sure about the location type item though.

Unfortunately this does not work:

JSONPATH:$.payload_fields.[Latitude,Longitude]

Executing the JSONPATH-transformation failed: An error occurred while transforming JSON expression.

The JSONPATH transformation service does not deal very well with arrays. It can only return a single string. That might look like “[x,y]” but I don’t think that will help as I think Location types look for “x,y” format.

You could chain on another transformation to strip the brackets, but you might as well use just one javascript transformation to both extract the JSON parts and combine into a single string.

1 Like

OK, I will try to read through the docs to understand how to build a javascript transformation.

Oh, next problem, when trying to install the javascript transformation add-on :frowning:

Failed installing 'openhab-transformation-javascript': Error:
Error downloading mvn:org.openhab.addons.bundles/org.openhab.transform.javascript/3.0.1

Looks relevant -

1 Like

maven has kar files if you need in meantime.
https://search.maven.org/artifact/org.openhab.distro/openhab-addons/2.5.12/kar
just change search for 3.x

Ah, just bad luck then :slight_smile:

Well, I’ll wait till tomorrow, then. I need to understand how to use the javascript transformation anyway first :wink:

So, the add-on did automatically install this night. Now I need to see how to use it. I will report, whatever the outcome will be :slight_smile:

is there any documentation on howto use the javascript transformation in the GUI?

Do I need to create a script in the GUI? In the channel configuration page, what do I need to insert for the transformation to call the javascript transformation? Probably something similar to “JSONPATH:…”?

EDIT: Seems to be similar like “JS:mqtt_location.js”. This at least tries to load the file, which it doesn’t find. I now need to find out, if the GUI creates a file or if the GUI creates scripts will also be called.

“You can’t” (yet).
You need to create a file in the correct folder, and edit in your script with the editor of your choice…

Usage of transformations is context dependent, they can be employed in various parts of OH.
For a channel, that is part of a binding configuration and you should refer to binding docs.
It’s very like the JSONPATH you already used, but instead of a text string parameter there is a file to point at.

Well, very strangely, I got it working on the very first try :upside_down_face:

That was nearly too easy. Although I would really like to see a support for creating the scripts in the GUI (as my goal is to move all or most of the configuration from my text files to the GUI).

Thanks for your help, btw!! :heart_eyes:

EDIT: Here is my solution:

In the channel config as the transformation config:

JS:mqtt_location.js

Then in the openhab conf folder, create the file “transform/mqtt_location.js” with following content:

(function(json){
	var data = JSON.parse(json);
	return data.uplink_message.decoded_payload.Latitude + "," + data.uplink_message.decoded_payload.Longitude + "," + data.uplink_message.decoded_payload.Altitude;
})(input)

P.S.: This is for a TTN v3 uplink message.

3 Likes