Linptech/Xiaomi BT Water Leak/Rain Sensor RS1BB using Passive BLE and ESPHome

Just wanted to give something back to the community which has been really helpful to me.

I’ve found cheap little rain sensor on AE and wanted to give it a go: https://www.aliexpress.com/item/1005006344050442.html

I’m hosting my HA instance on my Windows “server” PC with no direct bluetooth hardware connected to HA - just using multiple ESP32s as BT proxies around my home.

Update 07-2024: Official Xiaomi BLE integration now picks up the sensor automatically (BLE key is needed). I disabled Passive BLE monitor and automation. I left my ESP config unchanged.

Steps:

  1. Connecting the sensor to Xiaomi Home app (using Chinese server).
  2. Getting encryption key and MAC for the device.
  3. Installing Passive BLE Monitor custom (HACS) integration.
  4. Configuring ESP32 as BLE Gateway in ESPHome.
  5. Configuring the device in Passive BLE Monitor.
  6. Created automation to listen to events.

Instructions:

  1. I first went and connected rain sensor to the Xiaomi Home app using Chinese server (I’ve created dual app on my phone a while back as I have some devices that can only be added to the app using CN server).

  2. I extracted encryption key (BLE KEY) and MAC address from my Xiaomi Home account using this tool. More info and help is available here.

  3. Then I installed Passive BLE monitor from HACS - I did not yet configure it at this point.

  4. I configured my ESP32 using this config (first I installed external_components and after that I added and installed esp32_ble_tracker and ble_gateway config):

external_components:
  - source: github://myhomeiot/esphome-components
  - source: github://pr#2854
    components: [esp32_ble_tracker]

esp32_ble_tracker:

ble_gateway:
  devices:
    - mac_address: AA:BB:CC:DD:EE:FF
  on_ble_advertise:
    then:
      homeassistant.event:
        event: esphome.on_ble_advertise
        data:
          packet: !lambda return packet;

I have a dedicated ESP32 for this sensor, so I don’t know how it’s going to behave if there are some additional sensors/components added (beside basic config).

  1. Then I configured Passive BLE Monitor in Home Assistant:

  1. Then I created automation as it says in readme from BLE Gateway:
automation:
  - alias: ESPHome BLE Advertise
    mode: queued
    trigger:
      - platform: event
        event_type: esphome.on_ble_advertise
    action:
      - service: ble_monitor.parse_data
        data:
          packet: "{{ trigger.event.data.packet }}"

I had to wait for about 15 minutes to get the data to HA. Eventually, I could see a device and sensor in Passive BLE Monitor integration in HA:

I know the guide is not detailed, but it’ll maybe save some time to someone.

P.S. At one time, after I rebooted Home Assistant, I noticed that the sensor was not reporting any states back (not even RSSI and battery) - in the logs, I’ve found:

WARNING (ImportExecutor_0) [custom_components.ble_monitor.bt_helpers] There are no BT controllers present in the system!

Reload of Passive BLE monitor integration solved the issue (and this occured only once for me). I’m not sure why it happened - I assume that the ESPHome integration loaded after Passive BLE Monitor one. This can be solved with automation that reloads the integration after HA boots - more info here.
Also, after HA reboot, the sensor “moisture” state is unknown until it detects “moisture” or “no moisture” again. It does report RSSI and battery after a few minutes.

1 Like

Thanks for adding this tutorial. I was wondering why it was not initially connecting to Xiaomi app, then i saw this small, but VERY important comment - Chinese servers. Switched to China and immidiately it was discovered and managed to extract the token. This evening i will try to continue when i get back home.
Thanks again.
P.S. Put the chinese servers in bold - really important part :smiley:

1 Like

Can this also work directly with a Pi 4’s Bluetooth radio?
I am only asking because I have tried to the past 2 days to make it work without any success.

I assume you have to configure your Home Assistant instance following first steps here (instead of step no. 4 in my guide): Configuration parameters | Passive BLE Monitor integration

And if the device is in range of your Pi’s Bluetooth, it should work.

Whenever I restart the HA server, the sensor shows an “unknown” state, and this state remains unchanged until the next rain. Signal and battery levels are reported every 50 minutes. Should this be fixed on the server side or with a firmware update for the device?

Moreover, the device is far from reliable. All night it was heavy raining and in the middle of the night it changed the state from wet to dry. Maybe rain water too clean to conduct? Can we change the sensitivity somehow?

1 Like

Update: Official Xiaomi BLE integration now picks up the sensor automatically (BLE key is needed). I disabled Passive BLE monitor and automation. I left my ESP config unchanged.

HI! That’s great! Can you explain how you did it? I add mine via Xiaomi integration, but just show the signal stranght, not the flood sensor. Thanks

It should show the moisture sensor the first time it changes from wet/dry to dry/wet. This goes for everytime HA reboots.

1 Like

Do you need a xiaomi hub to get the key and MAC, or simply a cell phone with Bluetooth?

Just an app with device added and Xiaomi account, no hub/gw needed.

Hi there, my HA just found the sensor but unfortunately it doesn’t show me anything, even with the Xiaomi Home App I can’t connect the sensor. What im doing wrong? Any idea?

Is these everything to put onto the ESP32?

external_components:
  - source: github://myhomeiot/esphome-components
  - source: github://pr#2854
    components: [esp32_ble_tracker]

esp32_ble_tracker:

ble_gateway:
  devices:
    - mac_address: AA:BB:CC:DD:EE:FF
  on_ble_advertise:
    then:
      homeassistant.event:
        event: esphome.on_ble_advertise
        data:
          packet: !lambda return packet;

Or should I append them to the original stuff like these?

substitutions:
  name: esphome-web-f95d0c
  friendly_name: M5Stack-Atom-Lite-BLE-Gateway

esphome:
  name: ${name}
  friendly_name: ${friendly_name}
  min_version: 2024.6.0
  name_add_mac_suffix: false
  project:
    name: esphome.web
    version: dev

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:

# Allow Over-The-Air updates
ota:
- platform: esphome

# Allow provisioning Wi-Fi via serial
improv_serial:

wifi:
  # Set up a wifi access point
  ap: {}

# In combination with the `ap` this allows the user
# to provision wifi credentials to the device via WiFi AP.
captive_portal:

dashboard_import:
  package_import_url: github://esphome/example-configs/esphome-web/esp32.yaml@main
  import_full_config: true

# Sets up Bluetooth LE (Only on ESP32) to allow the user
# to provision wifi credentials to the device.
esp32_improv:
  authorizer: none

# To have a "next url" for improv serial
web_server:

external_components:
  - source: github://myhomeiot/esphome-components
  - source: github://pr#2854
    components: [esp32_ble_tracker]

esp32_ble_tracker:

ble_gateway:
  devices:
    - mac_address: BB:EE:BB:EE:BB:EE
  on_ble_advertise:
    then:
      homeassistant.event:
        event: esphome.on_ble_advertise
        data:
          packet: !lambda return packet;

This is my complete esp conf:

esphome:
  name: esp32-name
  friendly_name: esp32-name

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "key"

ota:
  - platform: esphome
    password: "password"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp32 Fallback Hotspot"
    password: "password"

captive_portal:

external_components:
  - source: github://myhomeiot/esphome-components
  - source: github://pr#2854
    components: [esp32_ble_tracker]

esp32_ble_tracker:

ble_gateway:
  devices:
    - mac_address: XX:XX:XX:XX:XX:XX
  on_ble_advertise:
    then:
      homeassistant.event:
        event: esphome.on_ble_advertise
        data:
          packet: !lambda return packet;

Thanks for reply.

After flashing BLE-Gateway onto ESPHome, we should add the ESPHome device onto HA, right?

And “allow the device to perform HA actions” should be checked?

image

It seems mine is working now.
I have added Linptech PS1BB.

substitutions:
  name: m5stack-atom-lite-ble-gateway
  friendly_name: M5Stack-Atom-Lite-BLE-Gateway

esphome:
  name: ${name}
  friendly_name: ${friendly_name}
  min_version: 2024.6.0
  name_add_mac_suffix: false
  project:
    name: esphome.web
    version: dev

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: ""

# Allow Over-The-Air updates
ota:
- platform: esphome

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  ap:
    ssid: "M5Stack-Atom-Lite-BLE-Gateway"
    password: !secret wifi_password

captive_portal:

external_components:
  - source: github://myhomeiot/esphome-components

esp32_ble_tracker:

ble_gateway:
  id: blegateway
  devices:
    - mac_address: XX:XX:XX:XX:XX:XX # of Linptech PS1BB
  on_ble_advertise:
    then:
      homeassistant.event:
        event: esphome.on_ble_advertise
        data:
          packet: !lambda return packet;
          gateway_id: blegateway