---
title: "Shelly Door/Window 2"
description: "Shelly Door/Window 2 contact sensor via MQTT. Detects open/close state and reports temperature, luminance and battery level in TapHome."
url: "https://taphome.com/en/compatibility/shelly-door-window-mqtt/"
language: en
lastmod: 2026-10-08
generator: hugo
ai:
  topics: ["mqtt","sensor","wifi","door-window-detection","battery-powered","contact-sensor"]
  questions: ["How to integrate Shelly Door/Window 2 with TapHome via MQTT?","What does the Shelly Door/Window 2 TapHome template detect?","How to configure multiple Shelly Door/Window sensors in one TapHome template?","Why is my Shelly Door/Window sensor not reporting data?","How to find the Shelly Door/Window 2 MQTT Device ID?","Does Shelly Door/Window 2 report battery level, temperature and luminance in TapHome?","Can Shelly Door/Window 2 detect tilt and vibration in TapHome?"]
---

# Shelly Door/Window 2

Communication: Packet Parser → MQTT

- [Shelly Door/Window 2 Product Page](https://www.shelly.com/en/products/shop/shelly-door-window-2)

> Network address
> 
> With the MQTT broker built into TapHome, enter `127.0.0.1` as the broker address in TapHome. In the device, enter the IP address of the TapHome Core and give the Core an IP reservation in the router.
> 
> [Network Preparation](https://taphome.com/en/docs/setup/network-preparation/)

## Integration guide

The Shelly Door/Window 2 (SHDW-2) is a battery-powered (2x CR123A) Wi-Fi contact sensor. TapHome communicates with the device over MQTT — the recommended protocol for battery-operated Shelly devices, since the sensor sleeps most of the time and publishes data only on wake-up events (magnet state change, vibration, brightness change, or periodic timer).

The template supports up to 5 Shelly Door/Window sensors per module. Each sensor instance is distinguished by a `sensorN_topic` custom variable pointing to the device's MQTT Client ID.

## Configuration

### Device ID

Each Shelly Door/Window 2 sensor has a unique MQTT Device ID in the format `shellydw2-<MAC6>`, where `<MAC6>` is the last 6 characters of the MAC address in hex (e.g., `shellydw2-A1B2C3`).

The Device ID can be found:

- On the device label (MAC address)
- In the Shelly web UI: **Settings** → **Device Info**
- Via API: `GET http://<device-ip>/settings` → `device.hostname` field

> [!TIP]
> This device supports **mDNS discovery**. You can use the hostname `shellydw2-{MAC}.local` instead of an IP address when configuring TapHome. Replace `{MAC}` with the last 6 characters of the device's MAC address in lowercase hex (e.g., `shellydw2-A1B2C3.local`). Using a hostname prevents connectivity issues when the device's IP address changes.

### Template setup

After importing the template in TapHome:

1. Open the **Shelly Door/Window MQTT** module
2. Set the **MQTT Broker IP** and **Port** (default `1883`)
3. For each sensor instance, set the `sensorN_topic` custom variable to the Device ID of the corresponding Shelly Door/Window 2 (e.g., `shellydw2-A1B2C3`)

The module subscribes to `shellies/#` and the listener scripts filter messages by the configured topic prefix.

> [!TIP]
> Only configure the sensor slots you actually use. Unconfigured slots (with the default `shellydw2-deviceid` value) will show an error message prompting you to set the correct topic.

## Device capabilities

### Open/close detection

Each sensor instance is mapped as a Reed Contact device in TapHome. The door/window state is read from the `shellies/<id>/sensor/state` topic:

- `open` → **Alarm** (door/window open)
- `close` → **OK** (door/window closed)

The sensor wakes up and publishes its state when the magnet moves (door or window opens or closes), and also during periodic reporting intervals (default every 3 minutes).

### Battery, temperature and luminance monitoring

Each sensor instance exposes three service attributes:

- **Battery** — battery level percentage read from `shellies/<id>/sensor/battery`. A low battery warning is triggered when the level drops below 20%.
- **Temperature** — ambient temperature in °C read from `shellies/<id>/sensor/temperature`.
- **Luminance** — ambient light level in lux read from `shellies/<id>/sensor/lux`.

All attributes are read-only and update whenever the sensor wakes up and publishes data.

### Additional capabilities (not implemented)

The Shelly Door/Window 2 also publishes **tilt angle** (0–180 degrees) and **vibration detection** via MQTT, as well as an online/offline status via the LWT topic. These capabilities are available in the device's MQTT output but are not mapped in the current TapHome template. They can be added in a future template update.

## Troubleshooting

### Sensor not reporting data

1. Verify the Shelly Door/Window 2 is connected to Wi-Fi and MQTT is enabled in the device settings
2. Check that the `sensorN_topic` custom variable matches the Device ID exactly (e.g., `shellydw2-A1B2C3`)
3. If the MQTT broker address changed, update the broker address in the sensor's MQTT settings (wake the sensor first so that its web interface is reachable)
4. Use an MQTT client (e.g., MQTT Explorer) to subscribe to `shellies/#` and verify the sensor publishes messages on wake-up

### Open/close state not updating

1. Make sure the magnet is properly aligned with the main unit — when the door/window is closed, the magnet should be within 15 mm of the sensor
2. If the sensor was recently moved, use the calibration endpoint (`GET http://<device-ip>/calibrate?opened=1`) with the door/window in the open position to recalibrate
3. Check TapHome for the Reed Contact device state — 1 = open (alarm), 0 = closed (OK)

### Battery draining quickly

1. Shelly Door/Window 2 uses 2x CR123A batteries (not rechargeable) with typical battery life of approximately 18 months
2. Frequent wake-ups (e.g., due to unstable Wi-Fi requiring repeated reconnection or a very short sleep period) can drain the battery faster
3. The default sleep period is 3 minutes — increasing it via `sleep_mode.period` in the device settings reduces power consumption
4. Ensure strong Wi-Fi signal at the sensor location

> [!WARNING]
> Gen1 Shelly devices do **not** support MQTT over TLS. Communication between the sensor and the MQTT broker is unencrypted (plain MQTT, port 1883). Ensure the MQTT broker is on a trusted local network.

## How to install in TapHome

### Prerequisites

- Shelly device connected to Wi-Fi (2.4 GHz), e.g. through the Shelly app or the device's own access point
- MQTT broker running on your local network (e.g., Mosquitto, Home Assistant, or TapHome built-in broker)
- TapHome CCU on the same network as the broker

> [!WARNING]
> On **Gen1** devices, enabling MQTT **disables Shelly Cloud**. Both cannot run simultaneously. On Gen2/Plus devices this limitation does not apply.

### Step 1 — Enable MQTT on the Shelly device

#### Gen1 devices (Shelly 1, 1PM, 2.5, EM, 3EM, Plug S, RGBW2, Dimmer, TRV…)

1. Open the Shelly web UI: `http://<device-ip>/`
2. Navigate to **Internet & Security** → **Advanced — MQTT**
3. Enable **MQTT**
4. Set **MQTT Server**: `<broker-ip>:<port>` (e.g., `192.168.1.10:1883`)
5. Optionally set **MQTT User** and **MQTT Password** if your broker requires authentication
6. Click **Save** — the device will reboot and connect to the broker

#### Gen2 / Plus devices (Shelly Plus 1, Plus 1PM, Plus 2PM, Plus Plug S, Plus H&T, Pro 3EM…)

1. Open the Shelly web UI: `http://<device-ip>/`
2. Navigate to **Settings** → **MQTT**
3. Enable **MQTT**
4. Set **Server**: `<broker-ip>:<port>` (e.g., `192.168.1.10:1883`)
5. The **Client ID** is pre-filled with the device ID (e.g., `shellyplus1pm-AABBCCDDEE`) — leave as-is unless you have a specific reason to change it
6. Click **Save** and reboot the device

With the TapHome built-in broker, `<broker-ip>` is the IP address of the TapHome Core. Give the Core an IP reservation in the router, otherwise the device loses the broker when the router changes the address → [Network Preparation](https://taphome.com/en/docs/setup/network-preparation/).

> [!TIP]
> To verify MQTT is working, use an MQTT client (e.g., MQTT Explorer) and subscribe to `shellies/#` (Gen1) or `<device-id>/#` (Gen2). You should see status messages from the device.

### Step 2 — Find the Device ID / MQTT Client ID

Some templates require a **Device ID** or **MQTT Client ID** parameter. This is the unique identifier used in MQTT topics.

- **Gen1:** found on the label as MAC address (e.g., `AABBCCDDEE`). Device ID = `shelly<model>-<mac>`, e.g., `shelly1pm-AABBCCDDEE`
- **Gen2/Plus:** found in the Shelly web UI under **Settings** → **Device Info** → **Device ID**, or on the device label

### Step 3 — Configure in TapHome

1. In TapHome, add a new **Packet Parser (MQTT)** module
2. **IP Address:** `127.0.0.1` for the TapHome built-in broker (the control unit connects to itself, the address never changes); for an external broker its IP address (e.g., `192.168.1.10`)
3. **Port:** `1883` (default; use `8883` for TLS)
4. **Device ID / MQTT Client ID:** enter the value from Step 2 (if required by the template)
5. Import the template — TapHome will subscribe to the device topics automatically

## Available devices 5

These devices appear in TapHome after import.

Shelly Door/Window MQTT Module

Custom Variables

`sensor1_topic` (string) = shellydw2-deviceid MQTT device ID of door/window sensor 1 — format is 'shellydw2-DEVICEID' (find Device ID in Shelly web UI → Settings → Device Info)

Open http://shellyIpAddress → Settings → Device info → copy Device ID. Format: shellydw2-&lt;last6MAC&gt;

`sensor2_topic` (string) = shellydw2-deviceid MQTT device ID of door/window sensor 2 

`sensor3_topic` (string) = shellydw2-deviceid MQTT device ID of door/window sensor 3 

`sensor4_topic` (string) = shellydw2-deviceid MQTT device ID of door/window sensor 4 

`sensor5_topic` (string) = shellydw2-deviceid MQTT device ID of door/window sensor 5

Door/Window Sensor 1 Reed Contact Read-only

Open/close detection via reed switch — reports alarm when door/window is open, OK when closed

boolean

Service Attributes

|             |
|-------------|
| Battery     |
| Temperature |
| Luminance   |

Listener

```taphome
IF (INDEXOF(sensor1_topic, "shellydw2-deviceid") = 0)
    ADDERROR("Set correct 'sensor1_topic' value in module variables. Topic format is 'shellydw2-<deviceid>'. The Device ID can be found by opening url 'http://shellyIpAddress', in Settings -> Device info.");
    RETURN(-1);
END

VAR topicPrefix := "shellies/" + sensor1_topic;

IF (INDEXOF(RECEIVEDMSG.TOPIC, topicPrefix + "/sensor/state") = 0)
    VAR value := TOSTRING(RECEIVEDMSG.PAYLOAD);

    IF (COMPARE(value, "open", CompareOptions.IgnoreCase) = 0)
        Rc := 1;
    ELSEIF (COMPARE(value, "close", CompareOptions.IgnoreCase) = 0)
        Rc := 0;
    ELSE
        Rc := NaN;
    END
ELSEIF (INDEXOF(RECEIVEDMSG.TOPIC, topicPrefix + "/sensor/temperature") = 0)
    temp := TODOUBLE(TOSTRING(RECEIVEDMSG.PAYLOAD));
ELSEIF (INDEXOF(RECEIVEDMSG.TOPIC, topicPrefix + "/sensor/battery") = 0)
    battery := TODOUBLE(TOSTRING(RECEIVEDMSG.PAYLOAD));
    IF battery < 20
        ADDWARNING("Battery low (" + battery + "%)");
    END
ELSEIF (INDEXOF(RECEIVEDMSG.TOPIC, topicPrefix + "/sensor/lux") = 0)
    luminance := TODOUBLE(TOSTRING(RECEIVEDMSG.PAYLOAD));
END
```

Service Attributes

Battery

```taphome
battery + "%"
```

Temperature

```taphome
temp + "°C"
```

Luminance

```taphome
luminance + " lux"
```

Door/Window Sensor 2 Reed Contact Read-only

Open/close detection via reed switch — reports alarm when door/window is open, OK when closed

boolean

Service Attributes

|             |
|-------------|
| Battery     |
| Temperature |
| Luminance   |

Listener

```taphome
IF (INDEXOF(sensor2_topic, "shellydw2-deviceid") = 0)
    ADDERROR("Set correct 'sensor2_topic' value in module variables. Topic format is 'shellydw2-<deviceid>'. The Device ID can be found by opening url 'http://shellyIpAddress', in Settings -> Device info.");
    RETURN(-1);
END

VAR topicPrefix := "shellies/" + sensor2_topic;

IF (INDEXOF(RECEIVEDMSG.TOPIC, topicPrefix + "/sensor/state") = 0)
    VAR value := TOSTRING(RECEIVEDMSG.PAYLOAD);

    IF (COMPARE(value, "open", CompareOptions.IgnoreCase) = 0)
        Rc := 1;
    ELSEIF (COMPARE(value, "close", CompareOptions.IgnoreCase) = 0)
        Rc := 0;
    ELSE
        Rc := NaN;
    END
ELSEIF (INDEXOF(RECEIVEDMSG.TOPIC, topicPrefix + "/sensor/temperature") = 0)
    temp := TODOUBLE(TOSTRING(RECEIVEDMSG.PAYLOAD));
ELSEIF (INDEXOF(RECEIVEDMSG.TOPIC, topicPrefix + "/sensor/battery") = 0)
    battery := TODOUBLE(TOSTRING(RECEIVEDMSG.PAYLOAD));
    IF battery < 20
        ADDWARNING("Battery low (" + battery + "%)");
    END
ELSEIF (INDEXOF(RECEIVEDMSG.TOPIC, topicPrefix + "/sensor/lux") = 0)
    luminance := TODOUBLE(TOSTRING(RECEIVEDMSG.PAYLOAD));
END
```

Service Attributes

Battery

```taphome
battery + "%"
```

Temperature

```taphome
temp + "°C"
```

Luminance

```taphome
luminance + " lux"
```

Door/Window Sensor 3 Reed Contact Read-only

Open/close detection via reed switch — reports alarm when door/window is open, OK when closed

boolean

Service Attributes

|             |
|-------------|
| Battery     |
| Temperature |
| Luminance   |

Listener

```taphome
IF (INDEXOF(sensor3_topic, "shellydw2-deviceid") = 0)
    ADDERROR("Set correct 'sensor3_topic' value in module variables. Topic format is 'shellydw2-<deviceid>'. The Device ID can be found by opening url 'http://shellyIpAddress', in Settings -> Device info.");
    RETURN(-1);
END

VAR topicPrefix := "shellies/" + sensor3_topic;

IF (INDEXOF(RECEIVEDMSG.TOPIC, topicPrefix + "/sensor/state") = 0)
    VAR value := TOSTRING(RECEIVEDMSG.PAYLOAD);

    IF (COMPARE(value, "open", CompareOptions.IgnoreCase) = 0)
        Rc := 1;
    ELSEIF (COMPARE(value, "close", CompareOptions.IgnoreCase) = 0)
        Rc := 0;
    ELSE
        Rc := NaN;
    END
ELSEIF (INDEXOF(RECEIVEDMSG.TOPIC, topicPrefix + "/sensor/temperature") = 0)
    temp := TODOUBLE(TOSTRING(RECEIVEDMSG.PAYLOAD));
ELSEIF (INDEXOF(RECEIVEDMSG.TOPIC, topicPrefix + "/sensor/battery") = 0)
    battery := TODOUBLE(TOSTRING(RECEIVEDMSG.PAYLOAD));
    IF battery < 20
        ADDWARNING("Battery low (" + battery + "%)");
    END
ELSEIF (INDEXOF(RECEIVEDMSG.TOPIC, topicPrefix + "/sensor/lux") = 0)
    luminance := TODOUBLE(TOSTRING(RECEIVEDMSG.PAYLOAD));
END
```

Service Attributes

Battery

```taphome
battery + "%"
```

Temperature

```taphome
temp + "°C"
```

Luminance

```taphome
luminance + " lux"
```

Door/Window Sensor 4 Reed Contact Read-only

Open/close detection via reed switch — reports alarm when door/window is open, OK when closed

boolean

Service Attributes

|             |
|-------------|
| Battery     |
| Temperature |
| Luminance   |

Listener

```taphome
IF (INDEXOF(sensor4_topic, "shellydw2-deviceid") = 0)
    ADDERROR("Set correct 'sensor4_topic' value in module variables. Topic format is 'shellydw2-<deviceid>'. The Device ID can be found by opening url 'http://shellyIpAddress', in Settings -> Device info.");
    RETURN(-1);
END

VAR topicPrefix := "shellies/" + sensor4_topic;

IF (INDEXOF(RECEIVEDMSG.TOPIC, topicPrefix + "/sensor/state") = 0)
    VAR value := TOSTRING(RECEIVEDMSG.PAYLOAD);

    IF (COMPARE(value, "open", CompareOptions.IgnoreCase) = 0)
        Rc := 1;
    ELSEIF (COMPARE(value, "close", CompareOptions.IgnoreCase) = 0)
        Rc := 0;
    ELSE
        Rc := NaN;
    END
ELSEIF (INDEXOF(RECEIVEDMSG.TOPIC, topicPrefix + "/sensor/temperature") = 0)
    temp := TODOUBLE(TOSTRING(RECEIVEDMSG.PAYLOAD));
ELSEIF (INDEXOF(RECEIVEDMSG.TOPIC, topicPrefix + "/sensor/battery") = 0)
    battery := TODOUBLE(TOSTRING(RECEIVEDMSG.PAYLOAD));
    IF battery < 20
        ADDWARNING("Battery low (" + battery + "%)");
    END
ELSEIF (INDEXOF(RECEIVEDMSG.TOPIC, topicPrefix + "/sensor/lux") = 0)
    luminance := TODOUBLE(TOSTRING(RECEIVEDMSG.PAYLOAD));
END
```

Service Attributes

Battery

```taphome
battery + "%"
```

Temperature

```taphome
temp + "°C"
```

Luminance

```taphome
luminance + " lux"
```

Door/Window Sensor 5 Reed Contact Read-only

Open/close detection via reed switch — reports alarm when door/window is open, OK when closed

boolean

Service Attributes

|             |
|-------------|
| Battery     |
| Temperature |
| Luminance   |

Listener

```taphome
IF (INDEXOF(sensor5_topic, "shellydw2-deviceid") = 0)
    ADDERROR("Set correct 'sensor5_topic' value in module variables. Topic format is 'shellydw2-<deviceid>'. The Device ID can be found by opening url 'http://shellyIpAddress', in Settings -> Device info.");
    RETURN(-1);
END

VAR topicPrefix := "shellies/" + sensor5_topic;

IF (INDEXOF(RECEIVEDMSG.TOPIC, topicPrefix + "/sensor/state") = 0)
    VAR value := TOSTRING(RECEIVEDMSG.PAYLOAD);

    IF (COMPARE(value, "open", CompareOptions.IgnoreCase) = 0)
        Rc := 1;
    ELSEIF (COMPARE(value, "close", CompareOptions.IgnoreCase) = 0)
        Rc := 0;
    ELSE
        Rc := NaN;
    END
ELSEIF (INDEXOF(RECEIVEDMSG.TOPIC, topicPrefix + "/sensor/temperature") = 0)
    temp := TODOUBLE(TOSTRING(RECEIVEDMSG.PAYLOAD));
ELSEIF (INDEXOF(RECEIVEDMSG.TOPIC, topicPrefix + "/sensor/battery") = 0)
    battery := TODOUBLE(TOSTRING(RECEIVEDMSG.PAYLOAD));
    IF battery < 20
        ADDWARNING("Battery low (" + battery + "%)");
    END
ELSEIF (INDEXOF(RECEIVEDMSG.TOPIC, topicPrefix + "/sensor/lux") = 0)
    luminance := TODOUBLE(TOSTRING(RECEIVEDMSG.PAYLOAD));
END
```

Service Attributes

Battery

```taphome
battery + "%"
```

Temperature

```taphome
temp + "°C"
```

Luminance

```taphome
luminance + " lux"
```

**Connection:** Packet Parser → MQTT • Hostname: `shellydw2-{MAC}.local`

Possible improvements (4)

- Tilt Angle — Tilt angle in degrees (0-180), DW2 only. Available in MQTT but not mapped in the TapHome listener script.
- Vibration Detection — Vibration detection (0=none, 1=detected). Configurable sensitivity (high/medium/low). Available in MQTT but not mapped in the TapHome listener script.
- Connection Status — LWT topic — true on connect, false on disconnect. Could detect offline sensors.
- Full Status JSON — Complete /status as JSON (fw &gt;=1.8.0). Could parse wifi\_sta.rssi, battery, act\_reasons.

## Import parameters

Enter these values when you import the XML.

| Asked in the app       | Default       |
|------------------------|---------------|
| Mqtt Broker IP address | `192.168.0.1` |
| Mqtt Broker port       | `1883`        |

## Sources

- Shelly Gen1 API Reference
  
  [shelly-api-docs.shelly.cloud](https://shelly-api-docs.shelly.cloud/gen1/) 2026-03-12
- Home Assistant Shelly Integration
  
  [www.home-assistant.io](https://www.home-assistant.io/integrations/shelly/) 2026-03-12
- Shelly Door/Window 2 — Device-Specific API
  
  [shelly-api-docs.shelly.cloud](https://shelly-api-docs.shelly.cloud/gen1/#shelly-door-window-1-2) 2026-03-30
- Shelly Door/Window 2 — Knowledge Base
  
  [kb.shelly.cloud](https://kb.shelly.cloud/knowledge-base/shelly-door-window-2) 2026-03-30

## Related Documentation

- [MQTT Integration](https://taphome.com/en/compatibility/mqtt-guide/)
- [Packet Parser (HTTP, TCP, UDP, FTP, MQTT)](https://taphome.com/en/docs/integration/packet-parser/)
- [Packet parser functions](https://taphome.com/en/docs/script/packet-parser/)
- [Parsing functions](https://taphome.com/en/docs/script/parsing-functions/)
- [MQTT Broker](https://taphome.com/en/docs/integration/packet-parser/mqtt-broker/)
- [Listener scripts](https://taphome.com/en/docs/script/listener-scripts/)

