[SOLVED] [Modbus] invertstate issue

Hi @ssalonen I started to get the following warn when using transformation invertstate script that you’ve provided here

Previously it was working properly, any hints?

2019-01-20 21:28:12.442 [WARN ] [ernal.handler.ModbusDataThingHandler] - Channel switch will not be updated since transformation was unsuccessful. Channel is expecting the following data types [OnOffType, UnDefType]. Input data: number value 0 (value type 'bit' taken into account) and bool value false. Transformation: Transformation@15699a[tranformation=JS(invertstate.js),transformationServiceName=JS,transformationServiceParam=invertstate.js]

invertstate.js

// function to invert Modbus binary states
// variable "input" contains data passed by OpenHAB binding
(function(inputData) {
    var out = inputData ;      // allow Undefined to pass through
    if (inputData == '1' || inputData == 'ON' || inputData == 'OPEN') {
        out = '0' ;
    } else if (inputData == '0' || inputData == 'OFF' || inputData == 'CLOSED') {
        out = '1' ;
    }
    return out ;      // return a string
})(input)

thing configuration:

Thing data FatekPLC_Modbus_M1224 "FatekPLC_Modbus_M1224" @ "Modbus" [ readStart="3224", readValueType="bit", readTransform="JS(invertstate.js)", writeStart="3224", writeValueType="bit", writeType="coil" ]
Thing data FatekPLC_Modbus_M1225 "FatekPLC_Modbus_M1225" @ "Modbus" [ readStart="3225", readValueType="bit", readTransform="JS(invertstate.js)", writeStart="3225", writeValueType="bit", writeType="coil" ]

The vital information is which channel type you used for your Item.

I’ll guess you have a switch Item and are using channel=xxx:FatekPLC_Modbus_M1224:switch ??

You’ll want the input transform to return ON or OFF

The existing js should be fine for write transform.

My item is as follows:

Switch Input_26 "KON Door" <frontdoor> (Alarm) { channel="modbus:data:fatekplcusb0:markers_M1200_to_M1263:FatekPLC_Modbus_M1225:switch", autoupdate="false" }

You’ll want to make a new input transform js to return ON or OFF

1 Like

Sorry, I don’t get what you’re talking about. What is wrong with the current configuration and js? Why did it stop working?

Do you mean this?

// function to invert Modbus binary states
// variable "input" contains data passed by OpenHAB binding
(function(inputData) {
    var out = inputData ;      // allow Undefined to pass through
    if (inputData == '1' || inputData == 'ON' || inputData == 'OPEN') {
        out = 'OFF' ;
    } else if (inputData == '0' || inputData == 'OFF' || inputData == 'CLOSED') {
        out = 'ON' ;
    }
    return out ;      // return a string
})(input)

Yes. The transform js you had to begin with returned 0 or 1.
That doesn’t fit an openHAB switch Item looking for ON/OFF.

The transform you’ve just shown us returns ON/OFF so that one ought to work fine as a read transform.

Because you are doing an invert, you will also need a write transform. The first js you showed us should work for a write transform.