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.
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?
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.
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 + '%'.