Shelly: Integrating the KL LD and KL presence sensors

Here is how to connect the KL LD and KL presence sensors to a Shelly, with all settings on the sensor and on the Shelly.

Applies to KL LD and KL · Sensor firmware KL LD 1.1.3, KL 3.2.3 and newer · As of 12/5/2026

Contents

Shelly is not a hub for third-party sensors. The presence sensor is not added as a device in the Shelly app. The way to do it is a script on a Shelly relay: it receives presence over MQTT and switches the relay as long as someone is in the room, for example the light. The sensor reports every change immediately, and the relay switches without delay. If you like, a virtual component on the Shelly additionally shows “present/absent” (not on Gen2 Plus devices). This applies equally to KL LD and KL.

Without an MQTT broker, there is a second version of the script that polls the sensor every 3 seconds over HTTP (see Without a broker).

Replace the placeholders in this guide with your own values:

Placeholder Meaning
<Hauptthema> what is entered on the sensor under Connections → Main topic
<Adresse des Brokers>, <Port> Name or IP address and port of the broker, the same as on the sensor (usually 1883)
<Login>, <Passwort> Access data of the broker, only if it requires a login
<IP des Shelly> IP address of the Shelly
<IP des Sensors> IP address of the sensor, only for the HTTP version

Requirements

  • A Shelly device with relay and scripts: Gen2 (Plus and Pro), Gen3, Gen4. Not suitable: Gen1 and the Shelly Wall Display (no scripts).
  • For the display as a virtual component, additionally: Gen3, Gen4 and the Pro devices of Gen2, not the Plus devices of Gen2.
  • Current firmware on the Shelly.
  • An MQTT broker on the network (e.g. Mosquitto). A Shelly is not a broker. Without a broker, the HTTP version remains.
  • KL LD presence sensor with firmware 1.1.3 or KL with firmware 3.2.3 (older ones, see Limitations) on the same network as the Shelly.

Setting up the sensor

  • Enter under Connections:

    Field Value
    Connection type MQTT Broker
    Server, Port <Adresse des Brokers> and <Port>
    Login, Password only if the broker requires them
    Main topic only letters, digits and _
    Home Assistant off
  • How long the relay stays on after the last detection is determined by the sensor's timeout (main page, Timeout).

Setup

  1. Open the Shelly's web interface (http://<IP des Shelly>).

  2. Connect the Shelly to the same broker as the sensor. Enter the following under the MQTT settings and then restart the Shelly:

    Setting Value
    Enable MQTT (enable) on
    Server (server) <Adresse des Brokers>:<Port>
    User / password (user, pass) <Login> and <Passwort>, only if the broker requires them
  3. Scripts → Create script, enter a name (e.g. praesenz), Create.

  4. Paste in this code and adjust the lines marked with <<<:

    // SENVOLON presence sensor KL LD / KL -> Shelly (MQTT)
    let TOPIC       = "stat/<Hauptthema>/PRESENCE";  // <<< ANPASSEN: <Hauptthema> ersetzen
    let SWITCH_ID   = 0;     // <<< Relais, das der Praesenz folgt; -1 = keins schalten
    let PRESENCE_ID = -1;    // <<< optional: ID der virtuellen Komponente (z. B. 200); -1 = keine
    
    let presence = PRESENCE_ID >= 0 ? Virtual.getHandle("boolean:" + PRESENCE_ID) : null;
    let last = null;
    
    function onMessage(topic, message) {
      let p;
      if (message === "True") p = true;
      else if (message === "False") p = false;
      else return;
      if (p === last) return;   // the sensor repeats PRESENCE, e.g. on every reconnect
      last = p;
      console.log("Praesenz", p);
      if (SWITCH_ID >= 0) Shelly.call("Switch.Set", { id: SWITCH_ID, on: p });
      if (presence) presence.setValue(p);
    }
    
    if (PRESENCE_ID >= 0 && !presence) console.log("Virtuelle Komponente boolean:" + PRESENCE_ID + " fehlt");
    MQTT.subscribe(TOPIC, onMessage);
    
    Setting in the code Value
    TOPIC stat/<Hauptthema>/PRESENCE
    SWITCH_ID 0 for the first relay, 1 for the second …, -1 = switch nothing
    PRESENCE_ID -1, or the number of the virtual component (see “Optional”)
  5. Save, then Start. In the script list, switch on Run on startup so the script survives a restart of the Shelly.

The sensor sends presence as retained. The script therefore gets the last value immediately at startup.

Quick test

  • After the start, the console below the editor shows Praesenz true or Praesenz false, and the relay is set accordingly.
  • Leave the room: after the sensor's timeout, Praesenz false appears and the relay switches off.
  • If nothing appears after the start: check whether the Shelly and the sensor are on the same broker and whether TOPIC is exactly right (mind upper and lower case of the main topic).

Without a broker: HTTP version

This version does not need a broker. It polls the sensor every 3 seconds, so the relay switches up to 3 seconds later. The sensor needs a fixed IP address (permanently assign the address in the router). The MQTT settings on the sensor and the Shelly are not needed.

Check that http://<IP des Sensors>/measurements shows a line with "presence": true or "presence": false in the browser. Leave Authentication under System switched off, or append the sensor's access data to the address: …/measurements?login=<Login>&password=<Passwort>.

Create the script as above (steps 3 to 5), but with this code:

// SENVOLON presence sensor KL LD / KL -> Shelly (HTTP)
let SENSOR_URL  = "http://<IP des Sensors>/measurements";  // <<< ANPASSEN: IP des Sensors
let SWITCH_ID   = 0;     // <<< Relais, das der Praesenz folgt; -1 = keins schalten
let PRESENCE_ID = -1;    // <<< optional: ID der virtuellen Komponente (z. B. 200); -1 = keine
let INTERVAL_S  = 3;     // poll every 3 s

let presence = PRESENCE_ID >= 0 ? Virtual.getHandle("boolean:" + PRESENCE_ID) : null;
let last = null;
let busy = false;

function apply(p) {
  if (p === last) return;
  last = p;
  console.log("Praesenz", p);
  if (SWITCH_ID >= 0) Shelly.call("Switch.Set", { id: SWITCH_ID, on: p });
  if (presence) presence.setValue(p);
}

function onReply(result, error_code, error_message) {
  busy = false;
  if (error_code !== 0 || !result || result.code !== 200) {
    console.log("Sensor nicht erreichbar:", error_code, error_message, result ? result.code : "");
    return;
  }
  let d;
  try {
    d = JSON.parse(result.body);
  } catch (e) {
    console.log("Antwort ist kein JSON");
    return;
  }
  if (!d || typeof d.presence !== "boolean") {
    console.log("Keine Praesenz in der Antwort");
    return;
  }
  apply(d.presence);
}

function poll() {
  if (busy) return;
  busy = true;
  Shelly.call("HTTP.GET", { url: SENSOR_URL, timeout: 2 }, onReply);
}

if (PRESENCE_ID >= 0 && !presence) console.log("Virtuelle Komponente boolean:" + PRESENCE_ID + " fehlt");
Timer.set(INTERVAL_S * 1000, true, poll);
poll();
Setting in the code Value
SENSOR_URL http://<IP des Sensors>/measurements
SWITCH_ID 0 for the first relay, 1 for the second …, -1 = switch nothing
PRESENCE_ID -1, or the number of the virtual component (see “Optional”)

After the start, the console shows Praesenz true or Praesenz false. If it says “Sensor nicht erreichbar” there: check the IP address and, if authentication is switched on at the sensor, the access data in the address.

Optional

Display on the Shelly (virtual component, not Gen2 Plus): create a virtual component of the type Boolean. If you cannot find that option in the web interface, this call in the browser creates it:

http://<IP des Shelly>/rpc/Virtual.Add?type="boolean"&config={"name":"Praesenz"}

The response gives the number of the component, {"id": 200} when created for the first time. Enter this number in the script at PRESENCE_ID, save and restart the script.

Limitations

  • Presence is not a device of its own in the Shelly app, but a relay or a value on the Shelly device on which the script runs.
  • The script only evaluates presence. It does not take over the sensor's measured values, and it does not change the sensor's settings.
  • The script only switches when presence changes. If you switch the relay by hand, it stays that way until the next change.
  • If the sensor fails, the relay stays as it is, even when switched on. There is no “unreachable” indicator, only the line in the console.
  • If MQTT is off on the Shelly, the script aborts. Switch MQTT on and restart the script.
  • HTTP version: presence is up to 3 seconds old.
  • Older firmware on the sensor (KL LD up to 1.1.2, KL up to 3.2.2): after a restart of the sensor, the old value may remain until the first change. Update the firmware.

Verification status

This guide is based on the documentation from Shelly.

Next

Manual

KL LD presence sensor

Mounting, connecting to Wi-Fi, setup.

Manual

KL presence sensor

Mounting, connecting to Wi-Fi, setup.

Smart home

All systems

The guides for all presence sensors and systems at a glance.

Support

Help and contact

Phone, email and contact form.