Esphome and xiaomi LYWSD03MMC sensor

I have HA and ESPHOME using esp32 motherboard
i have 2 xiaomi LYWSD03MMC sensors.
the one i flashed the custom firmware and the other is with default firmware.
i got the bindkeys for both and made the following script in esphome


esphome:
  name: esp32_black
  platform: ESP32
  board: esp-wrover-kit

wifi:
  ssid: "-------"
  password: "---------"

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

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:


# Enable Bluetooth scanning for this ESP32
esp32_ble_tracker:


sensor:
  - platform: xiaomi_lywsd03mmc
    mac_address: --------
    bindkey: "a6effb15461e7f6b991fa623cssssssss"
    temperature:
      name: "LCDSensor1 Temperature"
    humidity:
      name: "LCDSensor1 Humidity"
    battery_level:
      name: "LCDSensor1 Battery Level"
    
  - platform: xiaomi_lywsd03mmc
    mac_address: ---------
    bindkey: "4d419bdd7ac0d2f4e680d5e89sssssssss"
    temperature:
      name: "Room2 Temperature"
    humidity:
      name: "Room2 Humidity"
    battery_level:
      name: "Room2 Battery Level"
    

i’ve uploaded the script to esp32 and after that i got “Unknown” for both the sensors.

what is wrong?

Are you obscuring the MAC addresses here or does your script have ------'s?

I found i had to run the custom firmware and set the advertising flag to “mi like”. Works a treat and I just use a random bind key the same for each of my sensors.

I’m’ hiding the mac address with ------
i also installed the custom firmware to one of the sensors with advertising flag to mi like.

can you please give your configuration so that i can check ?

For your info i’m using 5 LYWSD03MMC thermometers with custom firmware and an esp32 with openmqttgateway.
It was very easy to setup everything

custom firmware (i suppose same as your)

openmqttgateway (very easy to setup on esp32 with integrated bluetooth and wifi)
https://docs.openmqttgateway.com/

thanks for the info,

i will try with openmqttgateway

Here you go…

# Enable Bluetooth scanning for this ESP32
esp32_ble_tracker:

switch:
  - platform: restart
    name: 'Xiaomi BLE Hub REBOOT'

sensor:
  - platform: xiaomi_lywsd03mmc
    mac_address: A4:C1:38:D7:08:EA
    bindkey: "58e551981b02b42e0067cc07aede0a00"
    temperature:
      name: "Bathroom Temperature"
    humidity:
      name: "Bathroom Humidity"
    battery_level:
      name: "Bathroom Battery Level"

i managed to get connected to the sensors and read data.
but i get disconnects from esp32. is it normal?
the connection between esp32 and the sensors is not stable.
i have read that esp32 is not stable yet with ble sensors. but i don’t know if this is the case.

I have two ESP32’s doing BLE gateway duty. One is flakey as anything…falls over all the time for no obvious reason. The other has been running for weeks with no glitches. I think it just comes down to sometimes getting a crappy board.

If it can be of any help I have this ESP32

I have 6 Temperature units spread out over 3 floors. The furthest one is in the basement and the ESP32 is 2 floors up.

And this is my(code from the web) code:

substitutions:
  esphome_name: nodetemp

esphome:
  name: ${esphome_name}
  platform: ESP32
  # board: mhetesp32devkit
  board: esp-wrover-kit

wifi:
  ssid: "---"
  password: "---"

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

captive_portal:

# Enable logging
logger:
  level: none

# Enable Home Assistant API
api:
  reboot_timeout: 60min
  password: "---"

ota:
  password: "---"

# Enable Bluetooth scanning for this ESP32
esp32_ble_tracker:

time:
  - platform: homeassistant
    on_time:
      - seconds: 0
        minutes: 0
        hours: 4
        days_of_week: MON-SUN
        then:
           - switch.toggle: restart_switch
switch:
  - platform: gpio
    name: "${esphome_name}-Onboard-LED"
    pin: 2
    inverted: True
  - platform: restart
    name: ${esphome_name}-restart
    id: restart_switch
  
sensor:  
  - platform: uptime
    name: "${esphome_name}_Uptime Sensor"
    
  - platform: wifi_signal
    name: "${esphome_name} WiFi Signal"
    update_interval: 60s
    
  - platform: xiaomi_lywsd03mmc
    bindkey: "3a8e210a28b9d0b6d8aaf8862e9c81a1"
    mac_address: A4:C1:38:xx:xx:xx
    temperature:
      name: "Tempunit1"
    humidity:
      name: "Humidunit1"
    battery_level:
      name: "Battunit1"

  - platform: xiaomi_lywsd03mmc
    bindkey: "5b27cf5b4afc233956982d1114ec2bb2"
    mac_address: A4:C1:38:xx:xx:xx
    temperature:
      name: "Tempunit2"
    humidity:
      name: "Humidunit2"
    battery_level:
      name: "Battunit2"

  - platform: xiaomi_lywsd03mmc
    bindkey: "3a8e210a28b9d0b6d8aaf8862e9c81a3"
    mac_address: A4:C1:38:xx:xx:xx
    temperature:
      name: "Tempunit3"
    humidity:
      name: "Humidunit3"
    battery_level:
      name: "Battunit3"

  - platform: xiaomi_lywsd03mmc
    bindkey: "954a69b010fe81642ddc98b54286cda4"
    mac_address: A4:C1:38:xx:xx:xx
    temperature:
      name: "Tempunit4"
    humidity:
      name: "Humidunit4"
    battery_level:
      name: "Battunit4"
  
  - platform: xiaomi_lywsd03mmc
    bindkey: "954a69b010fe81642ddc98b54286cda4"
    mac_address: A4:C1:38:xx:xx:xx
    temperature:
      name: "Tempunit5"
    humidity:
      name: "Humidunit5"
    battery_level:
      name: "Battunit5"
  
  - platform: xiaomi_lywsd03mmc
    bindkey: "954a69b010fe81642ddc98b54286cda4"
    mac_address: A4:C1:38:xx:xx:xx
    temperature:
      name: "Tempunit6"
    humidity:
      name: "Humidunit6"
    battery_level:
      name: "Battunit6"

This code restarts the ESP32 every day so it is always fresh. Its been going good for months.
I did have some issues as I was not patient enough and I kept removing the battery from the modules to reboot them before they had time to registered themself. Power cycling wipes out the parameters including “my like”

1 Like

What did you do to get these sensors working? I am in the same position where the sensors report “unknown” in HA.

I have two devices, one with the custom firmware and one without just to experiment.

Here is my configuration:

sensor:
  - platform: xiaomi_lywsd03mmc
    mac_address: "mac_addr"
    bindkey: "bind_key_string"
    temperature:
      name: "LYWSD03MMC Temperature"
    humidity:
      name: "LYWSD03MMC Humidity"
    battery_level:
      name: "LYWSD03MMC Battery Level"

  - platform: xiaomi_lywsd03mmc
    mac_address: "mac_addr"
    bindkey: "bind_key_string"
    temperature:
      name: "Test Temperature"
    humidity:
      name: "Test Humidity"
    battery_level:
      name: "Test Battery Level"

Figured out what was happening. It seems that my Mac was connecting to the Xiaomi devices after I flashed the custom firmware onto them. This prevented the esp32 from connecting to the Xiaomi. When I disconnected the Mac from the Xiaomi it came right up in esphome.

Hello,
I have been using the sensors flashed with the ATC firmware together with ESP32 devices that read the infos and then forward via WiFi to my HA.
But after I replaced the batteries I can’t see them anymore, in the esphome Logs I see that their bindkey is unknown.
I tried to reactivate them from the page where I flashed the firmware but I can’t find them.
I tried to add them as Bluetooth devices and this works, I can see their MAC addresses.
Any idea what I need to do to make them work with my esphome devices again?

Do you just have the one ESP32? I’m wondering how one ESP32 can cover a whole house with BLE?

Depends more on the house than on the esp :wink:

These days I think the best option for using these sensors is to use the ble_monitor in HA (via HACS). Then you can configure as many esp32s you want around the house as listeners using their ble_gateway in ESPHome. I have 4 esp32s around my house listening to broadcasts from 20+ sensors and relaying what they hear back to HA. That way you don’t have to assign a sensor to any one esp32.

3 Likes

Hi
I have come across the same problem. The custom firmware wont allow it to boot after battery change.
The only way I could get the display to work was to reflash the original firmware to get the display up and running again and the put the custom firmware back on.

How are you reflashing the original firmware?

Same way I put the custom firmware on. Its available online.

Screen came to life as soon as it finished.

I took the battery out again to prove a point and the same happened again.

I cant believe nobody else hasn’t experienced this?

I’ve experienced this same behavior, hence my question. I flashed them OTA but after putting the battery back in and getting the blank screen I can’t flash them over Bluetooth. Are you soldering to pins on the pcb?

No I’m using the Telink flasher for MI Thermostat.

Even though the screen blank they still show up under blutooth.

Bit of a performance every time you want to change the battery

1 Like