MQTT 2.4 Thing Javascript Transformation

  • openHAB version: 2.4. New MQTT 2.4 Binding

  • Issue of the topic:

The Xiaomi Water Leak sensor publishes a tag “water_leak” with “true” or “false”.
I don´t get the transformation to work to a “contact” .
Hopefully somebody can help.

Thing configuration related to the issue

Bridge mqtt:broker:myAuthentificatedBroker "Mosquitto MQTT Broker" @ "MQTT" [ host="localhost", secure=false, username="stefan", password="xxx", clientID="openHAB2" ]
{
    //Water Sensors
    Thing topic XiaomiWaterSensor1 "Wassersensor" {
    Channels:
        Type contact : contact "Contact" [ stateTopic="zigbee2mqtt/XiaomiWaterSensor1", transformationPattern="JS:LeakContact.js"]
        Type number : voltage "Voltage" [ stateTopic="zigbee2mqtt/XiaomiWaterSensor1", transformationPattern="JSONPATH:$.voltage"]
        Type number : battery "Battery" [ stateTopic="zigbee2mqtt/XiaomiWaterSensor1", transformationPattern="JSONPATH:$.battery"]
    }
}

The LeakContact.js file is stored under the folder transformation.
LeakContact.js has the following code:

(function(x){
    var result = "";
    var json = JSON.parse(x);
    if (json.contact)
    {
        result="CLOSED";
    }
    else
    {
        result="OPEN";
    }
    return result;
})(input)

Item File:

Contact LeakageMetering1 "Wasser ausgetreten [%s]" <water> {channel="mqtt:topic:myAuthentificatedBroker:XiaomiWaterSensor1:contact"}

Logfile:

Command '{"battery":86,"voltage":2975,"linkquality":0,"water_leak":true}' not supported by type 'OpenCloseValue': No enum constant org.eclipse.smarthome.core.library.types.OpenClosedType.{"battery":86,"voltage":2975,"linkquality":0,"water_leak":true}
2019-03-27 14:37:49.202 [WARN ] [l.generic.ChannelStateTransformation] - Executing the JS-transformation failed: An error occurred while loading script.
1 Like

Besides the log warning, why aren’t you using the JSONPATH transformation for the first channel?

The result is true or false :wink:
But please change the jsonpath within the javascript to $.water_leak

if (json.water_leak)