Using Switchbot Mini Plug with Tasmota32 and MQTT as BLE Gateway for Xiaomi LYWSD03MMC Temp Sensors

Hi Everyone,
Recently I got several of the inexpensive ($5 from AliExpress), but accurate Xiaomi LYWSD03MMC Bluetooth Low Energy (BLE) Temperature-Humidity sensors and set out to integrate them into HA.

image

I flashed the sensors with the ATC custom firmware from PVVX that allows greater flexibility in setting BLE advertising intervals, removes the encryption key etc.

I run HA 2022.7.7 on Docker on a Ubuntu Linux 20.04 host. I initially purchased a couple of the HA supported long range BLE adapters and used the excellent Passive BLE Monitor custom component, that supports the Xiaomi LYWSD03MMC very well. The problem I had was Bluetooth coverage across my home which was spotty given the location of my HA host in the basement, and unreliable even with the Long Range capability of the adapters.

After a lot of reading, I decided to change course and use the Switchbot Mini Plug (W1901400) (based on the ESP32-C3) as a BLE Gateway using the Tasmota32 bluetooth firmware ( tasmota32c3-bluetooth.bin ).

The Switchbot Mini Plug (Original Firmware 1.3 or below) should first be flashed with Tasmota32 over the air (OTA) with the switchbota method. The process is demonstrated on this YouTube video by digiblur
After flashing Tasmota32 on the device, the Tasmota32 bluetooth firmware ( tasmota32c3-bluetooth.bin ) can be flashed OTA with the Tasmota web UI.
After flashing the Tasmota32 bluetooth firmware make sure you enable Bluetooth under Configure BLE. Don’t Enable Active Scan, since it will shorten the battery life of the the Xiaomi LYWSD03MMC

I noticed that the Switchbot Mini Plug had much better BLE coverage for the Xiaomi LYWSD03MMC evidenced by the excellent signal strength (RSSI). I placed one of these Mini Plugs on each floor (3 in total) and could get continuous updates from the Xiaomi LYWSD03MMC devices.

Suggested Tasmota Configuration:
Tasmota sends periodic telemetry MQTT topics for the temperature, humidity and battery values sent by the devices. Here’s the list of Tasmota commands for the Xiaomi LYWSD03MMC and other supported sensors

  1. To get individual MQTT topics for each Xiaomi LYWSD03MMC sensor, use the “MI32Option6 1” command in the Tasmota console. Each sensor gets a unique tele topic and that topic is the same on all the Switchbot Mini Plugs that recieve the sensor BLE updates.The MQTT tele topics will show as below. Note that each sensor in the topic is identified by a unique string beginning with “ATC”
20:19:27.823 MQT: tele/tasmota_ble/ATC58c885 = {"Time":"2022-08-29T20:19:27","mac":"a4c13858c885","Temperature":25.5,"Humidity":62.8,"DewPoint":17.9,"Btn":1,"Battery":59,"RSSI":-54,"TempUnit":"C"}
20:19:28.821 MQT: tele/tasmota_ble/ATCa79000 = {"Time":"2022-08-29T20:19:28","mac":"a4c138a79000","Temperature":23.5,"Humidity":54.4,"DewPoint":13.7,"Btn":1,"Battery":79,"RSSI":-57,"TempUnit":"C"}
20:19:29.825 MQT: tele/tasmota_ble/ATCe8dc5a = {"Time":"2022-08-29T20:19:29","mac":"a4c138e8dc5a","Temperature":23.6,"Humidity":54.6,"DewPoint":13.9,"Btn":1,"Battery":88,"RSSI":-46,"TempUnit":"C"}
20:19:30.827 MQT: tele/tasmota_ble/ATC8dd2c5 = {"Time":"2022-08-29T20:19:30","mac":"a4c1388dd2c5","Temperature":22.8,"Humidity":55.5,"DewPoint":13.4,"Btn":1,"Battery":86,"RSSI":-62,"TempUnit":"C"}
20:19:31.832 MQT: tele/tasmota_ble/ATC7c4a2b = {"Time":"2022-08-29T20:19:31","mac":"a4c1387c4a2b","Temperature":24.6,"Humidity":51.3,"DewPoint":13.9,"Btn":1,"Battery":89,"RSSI":-72,"TempUnit":"C"}
20:19:32.837 MQT: tele/tasmota_ble/ATC123338 = {"Time":"2022-08-29T20:19:32","mac":"a4c138123338","Temperature":23.6,"Humidity":52.8,"DewPoint":13.4,"Btn":1,"Battery":87,"RSSI":-67,"TempUnit":"C"}
20:19:33.838 MQT: tele/tasmota_ble/ATC110d3b = {"Time":"2022-08-29T20:19:33","mac":"a4c138110d3b","Temperature":24.1,"Humidity":52.5,"DewPoint":13.8,"Btn":1,"Battery":87,"RSSI":-58,"TempUnit":"C"}
20:19:34.838 MQT: tele/tasmota_ble/ATCda6aa1 = {"Time":"2022-08-29T20:19:34","mac":"a4c138da6aa1","Temperature":22.6,"Humidity":57.2,"DewPoint":13.7,"Btn":1,"Battery":84,"RSSI":-76,"TempUnit":"C"}
  1. By default, the tele topic update period is set for 300 seconds (5 minutes). I changed this setting to 60 seconds. Set the tele topic update period to the interval you want with the “MI32Period n” command where n = number of seconds.
    Optionally, you can also configure Tasmota to send out the tele topics as they are recieved from the sensors. The Tasmota command “MI32Option2 1” allows this.

  2. I found that these commands did not stick after a restart of the Switchbot Mini Plug, so, I used a Tasmota rule to set these commands on system boot and enabled the rule:

rule1 on system#boot do backlog MI32Option6 1; MI32Period 60 endon

rule1 1

Configuring MQTT Sensors in HA

We now have the temperature, humidity and battery measurements updating the MQTT broker with unique tele topics for each sensor. Here’s a look at how MQTT Explorer sees the updates to the broker:

We can now create individual MQTT sensors for each measurement from each sensor in the mqtt.yaml file pointed to by the entry in the configuration.yaml.

configuration.yaml:

mqtt: !include mqtt.yaml

mqtt.yaml

sensor:
# Living Room Temp
  - name: "Living Room Temp"
    force_update: true
    state_topic: "tele/tasmota_ble/your-sensor-id"
    device_class: temperature
    unit_of_measurement: '°C'
    value_template: '{{ value_json.Temperature }}'
    icon: mdi:thermometer

# Living Room Humidity
  - name: "Living Room Humidity"
    force_update: true
    state_topic: "tele/tasmota_ble/your-sensor-id"
    device_class: humidity
    unit_of_measurement: '%'
    value_template: '{{ value_json.Humidity }}'
    icon: mdi:water-percent

# Living Room Climate Battery
  - name: "Living Room Climate Battery"
    force_update: true
    state_topic: "tele/tasmota_ble/your-sensor-id"
    device_class: battery
    unit_of_measurement: '%'
    value_template: '{{ value_json.Battery }}'
    icon: mdi:battery    

The sensor entities should show up like so:

image

image

image

My climate dashboard shows the sensors:

To summarize, I’m very happy with the capabilities of the Switchbot Mini Plug and Tasmota32 Bluetooth to be able to extend BLE coverage and function as a BLE gateway to integrate with HA with MQTT.
At a cost of $7.50 per plug (as of 8/29/22), the Switchbot Mini Plug provides great value not only as a distributed BLE Gateway but also as a regular smart plug with energy monitoring. I presume that the capability to flash these OTA with switchbota will be blocked eventually, so, grab them while you can. :wink:

3 Likes

Great write up! Really nice to see all the parts covered in a single place.

I’m not familiar with the Tasmota BLE config (I use ESPHome for my listeners). Does it send all BLE packets it receives to MQTT or only ones that come from the temp sensors? If it sends them all how are you dealing with MQTT bloat from things like iPhones always changing their MACs?

Thanks for the comments.
This link answers your questions in detail about how the MQTT messages are sent - but in short , MQTT tele messages are sent for every supported sensor’s BLE advertisement.
iBeacons from iPhones, iPads and other devices are not supported in the precompiled binaries of Tasmota32 Bluetooth - so the Switchbot does not see these advertisements.
Also, you can block the sensors you don’t want processed with the MI32Block command:
image