KNX Binding: Profile with ECMA inline script to invert value

Hello everyone,
I have a KNX gateway for my garage door and the absolute position (0-100%) is inverted.

With OH4, a profile is probably the recommended method for such an inversion.

I can’t figure out from the examples and the documentation how to invert the status feedback via the JS ECMA Binding in an inline function.

Can someone show me an example?

Many thanks in advance.

(The problem has been discussed several times, but the answers are mostly binding specific, or outdated).

Create a new transformation of type JS inside the UI and paste the following code:

(function(data) {
 var int = parseInt(data)
 return (100 - int)
})(input)

Then open the channel link page of that channel link (either from the Thing‘s channels or the linked Item), select SCRIPT ECMAScript as profile and then use the Transformation UID (shown in the Transformations list or the details that can be opened from the bottom when editing the transformation) as „Thing To Item“ transformation.

Thank you very much. Got it working with your example and understand the idea now.

I also think this is a better approach than the inline approach I asked about.

1 Like

Ok, too soon, apparently all commands are now transformed. This means that up/down no longer works. Can you kindly tell me how I can apply the transformation to the status only? So how can I recognize whether a switching command or a number arrives at the channel?

Thanks again in advance.

Not sure how proficient you are with JS, but this should do the trick:

(function(data) {
 var int = parseInt(data) // parse a string starting with an integer
 if (isNaN(int)) return data // if the string does not start with an integer, early return it, e.g. UP/DOWN commands
 return (100 - int) + ' %' // invert percentage value
})(input)

This script relays UP/DOWN, STOP/MOVE and REFRESH commands and inverts percentage commands.

You can apply transformations on three different types of communication between the Thing and the Item:

  • Thing to Item: Applies to all updates send from the Thing to the Item
  • Command from Item to Thing: Applies to commands from the Item only
  • State from Item to Thing: Applies to state updates from the Item only

The first two communication directions are just forwarded without a profile, the last communication is normally not done and only needed in very rare cases.

To fully invert the absolute position of the garage door, you need to apply the transformation to both “Thing To Item” and “Command From Item” - sorry for thinking enough about this in the first place.

1 Like

Many thanks for that. I understand the code, had the same strategy, only I assumed that UP/DOWN is parsed to 1/0, which is why I didn’t come up with NaN.

The code works with the exception that I had to remove + '%'.

1 Like