Understanding JSONPath transformation in Profile Configuration

OpenHAB3.1.1 on RPi

Two use cases:

  1. Temperature e.g. “25,2 °C” (from KNX binding in 9.001 format) is being displayed as “25”. I would like to transform in a Profile Configuration to [%.1f °C].
  2. Wind speed e.g. “1,03 m/s” (from KNX binding in 9.005 format). I would like to transform in a Profile Configuration to [%.2f km/h].

What do I need to do? I don’t get the documentation about transformations in profiles. :roll_eyes:

KNX doesn’t seem to have anything to do with JSONPATH?

Help us to help you by revealing your starting point here.
What kind of Items are involved here?
Are they both ‘sensors’, i.e. incoming data only, no need to send out data (like a thermostat setpoint)?
Are you setting Things and Items up via GUI or file based?

KNX binding does not (yet) really work with units, but yes with the add of a transformation profile we can add some assumed units to incoming data.
If you use Quantity type Items then auto conversions can be carried out by the openHAB framework.

Temperature
My KNX weather station (thing) provides a temperature measurement (channel). I created an item “Temperature” which is linked to this channel. In order to display 25,2 °C I can declare “Temperature [%.1f °C]” in the sitemap label. – No real problem here.

Wind speed
My KNX weather station (thing) provides a wind speed measurement (channel). I created an item “Wind speed” which is linked to this channel. Number:Speed expects km/h instead of m/s. Transformation required.

I am trying to stick to the web gui.

Number:Speed expects any valid speed quantity you like - 30mph if you wish.

Also, if you declare a default unit in the Item metadata, you can post just-a-number with no units as the KNX binding does. The plain number will be assumed to be in default units.

So you use Item metadata ‘pattern’ to declare that default unit, this is very like the sitemap [state presentation]. Set it to %.1f m/s

In MainUI your Item state should now get shown as “10.0 m/s”. But you don’t care about that if you are using sitemaps.

In your sitemap widget, use a label="Whatever [%.2f km/h]" and the framework will automatically convert state m/s to km/h for display.

No transformation required, but its “cheating” a bit


So, the “proper” way instead.

Number:Speed expects any valid speed quantity you like - 30mph if you wish.

KNX channel only supplies a numeric - but we can append a unit (assuming we know what comes from KNX device).

Profiles attach to the link between channel and Item, and can manipulate states before they get to the Item - just what we want. We can use a JS javascript transform to append the units text.

First install JS transformation service (nothing to do with scripting)

Then use your favourite text editor to make a little javascript file in your /transform folder.
Give it a file name like addms.js

// input variable i contains data passed by OpenHAB binding
(function(i) {
    if (i == 'UNDEF') { return i; }  // pass through non-numeric
    else { return  i + ' m/s' ; }
})(input)

Add a JS transformation profile to your channel-Item link, pointing at your new addms.js file.

Now your Item state should be updating like 10m/s. But you wanted it in km/h … so you have to tell it.
Use Item metadata ‘pattern’ to declare that a unit, this is very like the sitemap [state presentation]. Set it to %.2f km/h.
The 10m/s update should now be seen in MainUI as 36.00 km/h

Put what you like in sitemap widgets, and auto conversion will happen as required.


There are other options that are not available to you as an OH 3.1 user, same principle but avoiding the little script file.