Tag archieven: Shelly

Connect a Shelly button to Homekit using MQTT

I bought a Shelli Plus I4 switch which I wanted to use to control my Philips Hue lights using Homebridge. The standard Shelly NG plugin did not work as expected so I started searching for a better solution. I was already using the Homebridge Mqttthing plugin so I investigated if this was capable of getting the right data from the buttons. After some practice I found out that you can create custom scripts in the Shelly which can send the data we need to be able to configure a statelessProgrammableSwitch in Mqttthing.

First I setup all the buttons to be in input mode “button”:

Shelly button Settings input mode set to "button"

Then I enabled MQTT on the Shelly in the “Settings” screen.

After this step I created a script (scripts->add script) in shelly which sends the right payloads to MQTT for Mqttthing to be able to use it:

Shelly.addEventHandler(function(e) {
  if (e.component === "input:0") {
    if (e.info.event === "single_push") {
      MQTT.publish("shelly-slaapkamer/event/input:0", JSON.stringify(0), 0, false);
    }
    else if (e.info.event === "double_push") {
      MQTT.publish("shelly-slaapkamer/event/input:0", JSON.stringify(1), 0, false);
    }
    else if (e.info.event === "long_push") {
      MQTT.publish("shelly-slaapkamer/event/input:0", JSON.stringify(2), 0, false);
    }
  }
  if (e.component === "input:1") {
    if (e.info.event === "single_push") {
      MQTT.publish("shelly-slaapkamer/event/input:1", JSON.stringify(0), 0, false);
    }
    else if (e.info.event === "double_push") {
      MQTT.publish("shelly-slaapkamer/event/input:1", JSON.stringify(1), 0, false);
    }
    else if (e.info.event === "long_push") {
      MQTT.publish("shelly-slaapkamer/event/input:1", JSON.stringify(2), 0, false);
    }
  }
  if (e.component === "input:2") {
    if (e.info.event === "single_push") {
      MQTT.publish("shelly-slaapkamer/event/input:2", JSON.stringify(0), 0, false);
    }
    else if (e.info.event === "double_push") {
      MQTT.publish("shelly-slaapkamer/event/input:2", JSON.stringify(1), 0, false);
    }
    else if (e.info.event === "long_push") {
      MQTT.publish("shelly-slaapkamer/event/input:2", JSON.stringify(2), 0, false);
    }
  }
  if (e.component === "input:3") {
    if (e.info.event === "single_push") {
      MQTT.publish("shelly-slaapkamer/event/input:3", JSON.stringify(0), 0, false);
    }
    else if (e.info.event === "double_push") {
      MQTT.publish("shelly-slaapkamer/event/input:3", JSON.stringify(1), 0, false);
    }
    else if (e.info.event === "long_push") {
      MQTT.publish("shelly-slaapkamer/event/input:3", JSON.stringify(2), 0, false);
    }
  }
});

This enables all data to be sent to MQTT properly.

Afterwards I added the following config to the Homebridge Mqtthing config:

{
    "accessory": "mqttthing",
    "type": "statelessProgrammableSwitch",
    "name": "Slaapkamer schakelaar",
    "logMqtt": true,
    "topics": {
        "getSwitch": [
            {
                "topic": "shelly-slaapkamer/event/input:0"
            },
            {
                "topic": "shelly-slaapkamer/event/input:1"
            },
            {
                "topic": "shelly-slaapkamer/event/input:2"
            },
            {
                "topic": "shelly-slaapkamer/event/input:3"
            }
        ]
    },
    "switchValues": [
        [0,1,2],
        [0,1,2],
        [0,1,2],
        [0,1,2]
    ]
}

This exposes the buttons as buttons in Homekit where you can configure single push, double push and long push for each button:

Shelly switch settings in Homekit
Share