Home Asssistant Xiaomi Mijia LYWSD03MMC Temperature and Humidity Sensor Tutorial September 2022

Thanks for your reply.

I tried another sensor to experiment with the various protocols.
HA picked it up as a Xiaomi BLE even with the custom firmware,
but only found one entity inside (power level).

With the previous devices I did try the guide’s advice of using
BTHome or pvvx protocols but had trouble maintaining connection.
After quite a lot of reading I opted for the device’s native protocol (MIJA) for those.
My point is that HA’s Xiaomi BLE integration does seem to support that well.

With this third device I started the pvvx protocol.
Left the Advertising Interval and Measurement Interval at their defaults
(2500ms, 4x) to make sure there was plenty of communication going on.
I did not enable the BT5+PHY or LE Long Range options.
The device showed up from time to time in the output of

bluetoothctl devices                                                            

run in the HA Terminal, but it showed only one entity (power level) in the HA devices page.
But I could not get HA to see any data from the device.
Tried the power cycle trick, did not help.

Next I tried BThome protocol. That worked well; the BThome integration
noticed the device after less than a minute and configured it properly.
Clicking through to the device via the Xiaomi BLE integration’s sub-panel
showed up the same information as going via the BThome sub-panel.
I still had trouble maintaining connection when changing the Advertising
Interval - I tried 5000ms (left the Measurement Interval setting at 4x)
and it worked initially but failed after a couple of hours.
I went back to the default of 1500ms and the connection was solid.

Finally I tried the MIJA protocol. I disabled the BTHome device,
changed protocols on the device and waited for HA to notice it,
which it did after a minute or two. It has stayed solidly connected
for half a day now so I might stick with that. It does not display all
the data entities that BThome does, but I only need temp & humidity.

To sum up I have working devices and can proceed with the projects
I want to use them for; I’m most grateful for your guide and
all the useful contributions people have made to it.

You seem to be having more trouble than average. Those sensors are quite fiddly. Good to hear you got things going ok. I know Bluetooth in Home Assistant is changing quite quickly, I haven’t updated HA for a few months as things seem to break, but I’ll do that soon enough. BTHome might be good enough to replace the extension I mention above at some point, but last time I checked it just didn’t quite do everything, from memory battery level or something was missing.

I recently bought 2 of these sensors. They were working fine for the last couple of days. But when I added 2 more, things started to crash. They randomly became unavailable. They have close range to the bluetooth proxies but this happens. I also deleted one to let HA rediscver the device but unfortunately that was not possible. Since I deleted it I can not add it back to bte home.

Have you found any solution to your problem ?

Is there also a way to figure out which bluetooth sensor connects to which proxy device ?

Try putting the sensors all close to the master device with Bluetooth rather than a proxy, to simplify the situation.

You will be best off with a supported Bluetooth receiver - see the recommended hardware on this page. I link to the model I use on the first post of this guide. I’ve also got a Sena UD100-G03 and a 10db gain external antenna on the way, which is meant to be a high performance Bluetooth device, but it hasn’t arrived yet so I can’t vouch for it.

Hi all
I have 3 devices with custom firmaware. Since 2 of them are far away from Bluetooth receiver I flashed an esp32 as bluetooth proxy. First of all I am not sure if the esp32 is installed correct. I see the following.

image

but I can not adopt it…
because the adopt option is gray.

image

in devices I see the following which indicates that there is available the device but something is wrong with the firmware. ?

In practice I placed the esp32 in the bedroom (which is a little far from the Bluetooth receiver and after some time the xiaomi mijia became unavailable in Home Assistant, which I think means that it connected to the esp32 but something is wrong.

I searched and I think that probably I have to reflash the thermometer with the original firmware. Is that right? (but I don’t know how to do it)

Can someone understand what I have to do?
Right now I am not sure if the problem is at the firmware of the xiaomi mijia or at the esp32 and I am not sure how to continiue.

Thanks

@Makis I suggest you start a new thread with your question, it’s somewhat different from the topic of this guide.

1 Like

Hello,

I’m having 4 LYWSD03MMC (original firmware), one RPI4 with BT integration, 2 ESP BT proxy (one per floor). I’m using Xiaomi BLE - Home Assistant where I received the data.
All was working since months but suddenly one of them was not connected, my asumption was the battery starting to die. I replaced the battery but this device don’t want to connect anymore.

This one is normaly connected to rpi4 where the other devices are supposed to connect to the ESP BT proxy depending of the floor.

I tried a lof of things but nothing work.
Remove BT integration from RPI4 , remove device from Xiaomi device.
I have one more esp proxy for testing and I put the LYWSD03MMC at 5cm from that proxy → I don’t see any notification with new device detected.

Using application on smartphone nRF connect give me the possibility to see and connect to that LYWSD03MMC where the battery indication is 99%. So the device is working but why HA don’t see it is the question :wink:

[edit]
I go to integration and add Xiaomi BLE (even if this already present) and then the device was recognize.
I got this message : “There hasn’t been a broadcast from this device in the last minute so we aren’t sure if this device uses encryption or not. This may be because the device uses a slow broadcast interval. Confirm to add this device anyway, then the next time a broadcast is received you will be prompted to enter its bindkey if it’s needed.”

From now, I just got the signal strengh and no entities for temp & humidity, battery voltage are missing

I’m waiting now to put back the bindkey.

I saw that bindkey is store in .storage/core.config_entries

like this example:

        "title": "Temperature/Humidity Sensor Cave",
        "data": {
          "bindkey": "your key"
        },
        "options": {},
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "bluetooth",

where a discovered device without bind key is

        "title": "Temperature/Humidity Sensor (LYWSD03MMC)",
        "data": {},
        "options": {},
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "user",

if nothing is coming, I will add myself and change the source.

Then afterwards, I checked if the Xiaomi app is still getting a value → no.

I reset the device with (ground / reset) , delete the device on Xiaomi and repairing + get the key.
I redo the HA process with the device near the RPI4, delete / add and I got quickly the device + the bind request.

The bindkey is now in core.config_entries and source is still user.

I’m waiting to get entities :wink:

5min later, I got humidity. Seems I do what is needed to have back my device OK in HA. Temp and other entities will come :wink:

Since you’re using the original firmware, if you have trouble you could try writing the custom firmware like this tutorial suggests.

I upgraded my LYWSD03MMC sensors from the 3.8 firmware to 4.3 firmware, which has more options.

As an experiment to see if I could get longer range I disabled “BT5+ PHY” and enabled “LE Long Range” on one of my sensors (see image below - different options selected but it shows the options). I can’t connect to that thermometer any more, with the XDO BT5 adapter (which supports BT5 and LE), Sena Bluetooth 4.0 adapter, Samsung S22 that supports BT5 inc BT5.1 LE, or with a Google Pixel 4a that also supports Bluetooth 5 LE.

The sensors are cheap enough that they can be replaced easily, but I’d like to see if I can get it working again. I’m not interested in cutting up a USB cable and soldering to get a physical serial interface though. Can anyone help? Or have I ticked options in a way that mean it’s basically inaccessible using Bluetooth? @Ernst you’re an expert in this area and you posted above, any thoughts on this?

I noticed that the default after the upgrade was the “BT5+ PHY” box was checked, and even with that box checked my Bluetooth 4.0 adapter can receive transmissions from the sensors.

Here’s the details of my Bluetooth adapter and a le scan

hciconfig -a
hci0:	Type: Primary  Bus: USB
	BD Address: 00:E0:42:8B:xx:xx  ACL MTU: 1021:6  SCO MTU: 255:12
	UP RUNNING PSCAN ISCAN 
	RX bytes:5737 acl:0 sco:0 events:341 errors:0
	TX bytes:32992 acl:0 sco:0 commands:303 errors:0
	Features: 0xff 0xff 0xff 0xfe 0xdb 0xfd 0x7b 0x87
	Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3 
	Link policy: RSWITCH HOLD SNIFF PARK 
	Link mode: PERIPHERAL ACCEPT 
	Name: 'RTK_BT_5.0'
	Class: 0x000000
	Service Classes: Unspecified
	Device Class: Miscellaneous, 
	HCI Version: 5.1 (0xa)  Revision: 0xdfc6
	LMP Version: 5.1 (0xa)  Subversion: 0xd922
	Manufacturer: Realtek Semiconductor Corporation (93)
> hcitool lescan
LE Scan ...
A4:C1:38:D4:xx:xx OfficeSensor

NB: the “hcitool lescan” does not show the sensor that I changed the setting on, but does pick up the other sensor in my office.

Here’s the option in the 4.3 firmware. In the sensor I can’t access I unticked the “BT5+ PHY” checkbox and checked the “LE Long Range” checkbox.

I wish I was an expert. Pvvx is the real expert on this subject, but from what I understand from him, long range isn’t supported in Bluez, which is used in HA. Check this issue with some more background. I sometimes have a hard time to understand what he is trying to explain, but it might help you.

Thanks Ernst. That seems pretty complicated!

Do you have any suggestions how to rescue that sensor? Based on that thread it might need to be done in Windows, but my adapter doesn’t work well in Windows. I’ll try the Bluetooth built into my Gigabyte B550 motherboard again, see if I can get it into 5 LE mode. Initial reading suggests could be difficult and may not be possible.

Given how much effort this could take I’d probably be better off just throwing it away and buying another for $10.

I have no idea how to do that. One other tip, enable the option “send flags” when you use BTHome format. This helps recognizing the sensor by HA

Thanks Ernst. I’ve ordered a few new sensors, I’ll toss the old one out. It’s not worth spending a lot of time on it. I’ll make sure to put a note in the tutorial not to tick that box :slight_smile:

I’ve found the answer to my question. Question: If I enable “LE Long Range” on my LYWSD03MMC and I can no longer access it, how do I restore access?

On this page PVVX goes through a lot of warnings. One of them says “If you accidentally set the ‘LE Long Range’ on the thermometer, and your device does not support BT5.0, remove and insert the battery - the thermometer will switch to BT4.2 support mode.”. I can confirm that this is correct, I pulled the battery out for an hour, put it back in, and I could access the sensor again.

There are a lot of other useful warnings and notes, the page is definitely worth a read.

Since I am running Home Assistant on a Raspberry Pi 3, does anyone know how much these sensors write to the SD Card? I want to minimize the chance of corrupting the card with too many write cycles as I experiment with everything.

It depends how you set them up, frequency of measurements for example. In general though the messages are a couple of kilobytes per message, each measurement has a message for temperature, humidity, plus there are messages for battery state. My database on postgres is around 100 MB, I have a few dozen sensors of different types.

If you are not going to use sensors because of card life, there’s not much point using home assistant. Every sensor writes to the disk, you can minimize some things like system logging, but you have to accept that disk writes are necessary to use home assistant.

I started with a high durability memory card which worked well, but after a year or so I got a pi case that included an interface for an nvme disk and got the smallest disk available, which was 500 gigabytes. That should be both faster and more durable.

BTW, there is a new firmware for the LYWSD03MMC that use Zigbee
and works with Homeassistant :wink:

1 Like

Interesting! I wonder how battery life would compare with the BLE firmware :thinking:

Hi! I had this working flawlessly for months but recently something broke and I the sensors don’t get recognized at all by HA. I haven’t changed anything other than update HA and ESP as required.

My setup is quite simple: 5 sensors connected through 2 BLE Proxies using ESP. I’m attaching the ESP config at then end of the post.

Troubleshooting steps I’ve tried:

  1. Change some batteries
  2. Update firmware on the Xiaomi Temp sensors from 3.8 to 4.4 (latest stable)
  3. Reflash my two ESP proxies and change the config from arduino to esp-idf

So far nothing has helped. The ESP reports “starting scan” but nothing is ever found. Any aditional ideas on what to try? Thanks in advace.

esphome:
  name: "esp32-ble-proxy-1"
  name_add_mac_suffix: true

esp32:
  board: esp32dev
  framework:
    type: esp-idf

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: *redacted*

ota:
  password: *redacted*

wifi:
  ssid: *redacted*
  password: *redacted*
  manual_ip:
    static_ip: *redacted*
    gateway: *redacted*
    subnet: 255.255.255.0
    dns1: 1.1.1.1
    dns2: 1.0.0.1

esp32_ble_tracker:
  scan_parameters:
    active: true

bluetooth_proxy:
  active: true

Try connecting without a proxy, see if it works.

Are you using Passive BLE Monitor or the built in BTHome functionality? You could always try the one you’re not using.