I had a chance to play, my first grown-up transform
To invert the sense of a Modbus discrete input:
Create the following javascript, call it mbinvert.js
(placed in /configurations/transform in OH1)
// function to invert Modbus binary states
// input variable i contains data passed by OpenHAB binding
(function(i) {
var t = i ; // allow Undefined to pass through
if (i == 'OPEN') {
t = 'CLOSED' ;
} else if (i == 'CLOSED') {
t = 'OPEN' ;
}
return t ; // return a string
})(input)
Note the ‘input’ to the script is pre-processed by the Modus binding into OPEN or CLOSED, suitable for the Contact Item it is bound to. The ‘input’ is not the raw 0 or 1 from the Modbus read.
Item defintion
// This default Contact shows OPEN for Modbus discrete=1
Contact Standard "sensor [%s]" {modbus="slave1:0"}
// This inverted Contact shows CLOSED for Modbus discrete=1
Contact Inverted "sensor [%s]" {modbus="<[slave1:1:trigger=CHANGED,transformation=JS(mbinvert.js)]"}
This generates logs and bus events only when the state changes.