New openHab2 EnOcean binding

Many thanks for this great binding!

I finally received my window sensor today and was able to add it fully functional to OpenHab much quicker than expected.

I use a Siegenia Senso Secure device. Even though it is super expensive I liked its features and I’m not planning to put them on every window (and I saved quite a bit of money by building my own gateway…). For configuration I setup a generic item that just transforms the whole message to a string item via the genericString channel:

(function(inputData) {

    var parts = inputData.split('|'),
        hexToBytes = function(hex) {
        for (var bytes = [], c = 0; c < hex.length; c += 2)
        bytes.push(parseInt(hex.substr(c, 2), 16));
        return bytes;
    };

    if(!parts || parts.length != 2)
        return null;

    var b = hexToBytes(parts[1]);
    switch(parts[0]){
        case "genericString":
            return "StringType" + "|" + b;
            break;

        case "genericRollershutter":
            break;

        case "genericDimmer":
            break;

        case "genericNumber":            
            break;

        case "genericColor":
            break;

        case "genericTeachInCMD":
            break;
    }

    return (inputData);
})(input)

I then implemented the message handling in a rule:

var messageBytes = newState.toString().split(",");

for (var i = 0; i < messageBytes.length; i++) {
  var messageType;
  switch (i) {
    case 0:
      // Message Type: 1 = Window Status, 2 = Alarm
      messageType = messageBytes[i];
      break;
    case 1:
      // Window Status or Burglary Alarm
      if(messageType == 1) {
        // Cut off first byte since it only indicates keepalive messages
        var windowState = messageBytes[i] & 127;
        if (windowState == 0) {
          // State: None
          break;
        }
        if (windowState < 4) {
          // Window is closed
          events.sendCommand('door_terrace_opening', "Geschlossen");
        } else if (windowState < 7) {
          events.sendCommand('door_terrace_opening', "Geöffnet");
        } else {
          events.sendCommand('door_terrace_opening', "Gekippt");
        }
        if (windowState == 1 || windowState == 4 || windowState == 7) {
          events.sendCommand('door_terrace_handle', "Geschlossen");
        } else if (windowState == 2 || windowState == 5 || windowState == 8) {
          events.sendCommand('door_terrace_handle', "Geöffnet");
        } else if (windowState == 3 || windowState == 6 || windowState == 9) {
          events.sendCommand('door_terrace_handle', "Gekippt");
        }
      } else {
        messageBytes[i] == 1 ? events.sendCommand('door_terrace_alarm', "ON") : events.sendCommand('door_terrace_alarm', "OFF");
      }
      break;
    // Bytes 2 - 5 are not interesting for us right now
    case 6:
      // Alarm messages are only 2 byte long so can be ignored here
      // First bit is the status
      if ((messageBytes[i] & 128) == 128) {
        events.sendCommand('door_terrace_battery_alarm', "ON");
      } else {
        events.sendCommand('door_terrace_battery_alarm', "OFF");
      }
      // Bits 2 - 8 are percentage values
      var batteryPercent = messageBytes[i] & 127;
        events.sendCommand('door_terrace_battery', batteryPercent);
      break;  
  }
}

I don’t acutally know if the alarm is working properly, I hit the window a couple of times but that was apparently not enough. I guess I’ll find out when the kids are playing soccer in the garden :rofl: