Xiaomi Mijia bluetooth temperature & humidity sensor compatibility

@mr-varga
I had the same issue with bluetooth range and could not sort it. I’m now using a second RPI3 (works with Pi Zero as well) with HASS.io to improve the coverage. The 2nd RPI is sending the data via MQTT to the main RPI. I’m currently using this for device_tracker but it should work for other bluetooth solutions as well.

I have te Xiaomi Temp sensor as well.

  • Can you explain how you connected the BT sensor with RPI?
  • Can you share your config ?

Dear xx_Nexus_xx,

I followed this homeassistant guide:

2 Likes

Thanks a lot , I was thinking too complicated :wink: … not aware its already sorted

Hi there.

I use mitemp_bt with the Xiaomi sensor. It works well, WHEN the temperature is 10 degrees and above. As soon as it goes under 10, temperature reading is locked on 10 or what ever the last reading was, and humidity lock’s up as well. Battery looks similar.
When the temperature rises above 10, readings resume. Looks the same in the HA overview.

I tried this with 2 different HA setups, and 2 different sensors. Same problem every time.

Am I the only one?

Tried with the official docker image, and Hass.io on a RPI 3
Tried median: 1 & 3

sensor:
  - platform: mitemp_bt
        mac: 'XXX'
        name: Room
        force_update: true
        median: 1
        monitored_conditions:
          - temperature
          - humidity
          - battery
1 Like

I’m having the same issue with no reading below 10 degress :unamused:

I’m having the same issue as @thesailor. I checked with the Mi Home app and the sensor is connected and operating as expected (it was reading 8.0degC and 71.6% humidity), but with the HomeAssistant app the values are frozen at temps below 10degC. I even tried moving my Pi3 next to the sensor to test if the range was affected at low temperatures but the values still won’t read. Suspect there’s a single digit temp issue but not even sure where the code base is to have a look - hopefully I can find it later tonight.

Hi @randomdisco & @xx_Nexus_xx.

Looks like we now have a solid case :wink:
My educated guess is that this is a relatively simple parsing problem. I don’t know what the sensor returns as a value when under 10 - the digital screen shows “09.3”. That starting ‘0’ could be the problem.

@randomdisco, you can find the codebase here: https://github.com/flavio20002/mitemp_bt

Cheers.

Awesome - thanks. I’m not a Python coder (yet - if only I had time) but it looks like this might be the culprit:

    # Parse the output
    res = re.search("( [0-9a-fA-F][0-9a-fA-F])+", result)

which is in
def write_readnotif_ble(mac, handle, value, retries=3, timeout=20, adapter=‘hci0’):
and
def read_ble(mac, handle, retries=3, timeout=20, adapter=‘hci0’):

Appears as if it needs to have 2 values for both… But if the display is returning a 0 in front this should work. Wondering if the parse of the handle from the sensor (in format 0xXX) is only returning 1 rather than 2 values…

Will try and setup a debug environment tonight to figure it out. Any tips on setting such an environment up on a PI3 would be appreciated (or I’ll just google later) :slight_smile:

@thesailor opened an issue with the developer too. Suspect he/she might be able to debug this quicker than me (if they have time): https://github.com/flavio20002/mitemp_bt/issues/2

Disaster @thesailor @xx_Nexus_xx ; that was the wrong code base. I fixed it though and updated the GitHub issue. Found the real code and posted another issue with where the error is. I can’t figure out what type the data is for the code to work though so I’m struggling to work out a fix. Watch this space… https://github.com/ratcashdev/mitemp/issues/2

Update: worked out a fix (just needed a break for a coffee) Will test overnight…

@xx_Nexus_xx @thesailor

This is working for me.

First stop the Home Assistant service. For me its: sudo systemctl stop home-assistant@homeassistant

I took a backup:
sudo cp /srv/homeassistant/lib/python3.5/site-packages/mitemp_bt/mitemp_bt_poller.py /srv/homeassistant/lib/python3.5/site-packages/mitemp_bt/mitemp_bt_poller_original.py

Then edited
sudo nano /srv/homeassistant/lib/python3.5/site-packages/mitemp_bt/mitemp_bt_poller.py

Change _parse_data function (it should be obvious but it’s 1 new line and 2 changed lines):

data = self._cache

temp,humidity  = data.replace("T=", "").replace("H=", "").rstrip(' \t\r\n\0').split(" ")

res = dict()
res[MI_HUMIDITY] = float(humidity)
res[MI_TEMPERATURE] = float(temp)
return res

Save, exit, and restart home assistant: sudo systemctl start home-assistant@homeassistant

Or just wait until a fix gets pushed out. I’ve no idea how to release as a patch properly…

Attached is a pic of my sensor in the fridge - I needed to test somehow :slight_smile:

Enjoy!!
46|356x212

1 Like

WOW! Awesome work @randomdisco! Thanks for putting the time into it!
That was a fast turnaround and a great contribution to @flavio20002 's code (or ratcash ?). Definitely thx to both of you.

I will try to squeeze some time in to test, maybe tonight (Melbourne time).

Cheers.

Well,

I don’t have access to the RPI which is running at a remote site, so trying with Docker on my laptop and the spare sensor.

I don’t know how to tell HA how to load the custom mitemp_bt_poller.py source file, or edit it. I believe the later isn’t possible when using Docker.
I have tried by placing the edited file under “custom_components”.

I will have to wait a few more days before being able test - unless I can be pointed towards how to do the above, with Docker.

I noticed that someone else submitted a potential fix on github.

Cheers.

1 Like

Sorry I can’t help more. I don’t have Docker experience and I can’t create a branch to the original code to push a change / make a pull request. Not sure how to get the fix propagated to the Home Assistant :frowning:

The other fix on GitHub is another way of doing the same thing. Either should work so I don’t care which one gets put in. I only tested mine with single digit temp - I don’t know how to test with single digit humidity and I didn’t leave my sensor in the fridge long enough to test negative temps (I’m in Brisbane so negative unlikely to occur).

Does anyone else know how to propagate one of the fixes to Home Assistant?

1 Like

I tried the custom components from flavio2002. But i can’t get my Temperature Sensor to work.
I only get the following Messages:
2018-07-28 17:21:30 WARNING (MainThread) [homeassistant.helpers.entity] Update of sensor.mijia_batteria is taking over 10 seconds
connect: Connection refused (111)
connect: Connection refused (111)
2018-07-28 17:21:30 INFO (SyncWorker_11) [custom_components.sensor.mitemp_bt] Polling error [Errno Could not read data from Mi sensor %s] 4C:65:A8:D8:AA:E1
2018-07-28 17:21:41 INFO (SyncWorker_12) [custom_components.sensor.mitemp_bt] Polling error [Errno Could not read data from Mi sensor %s] 4C:65:A8:D8:AA:E1
2018-07-28 17:21:41 INFO (SyncWorker_13) [custom_components.sensor.mitemp_bt] Polling error [Errno Could not read data from Mi sensor %s] 4C:65:A8:D8:AA:E1
2018-07-28 17:22:12 INFO (SyncWorker_15) [custom_components.sensor.mitemp_bt] Polling error [Errno Could not read data from Mi sensor %s] 4C:65:A8:D8:AA:E1
2018-07-28 17:22:12 INFO (SyncWorker_4) [custom_components.sensor.mitemp_bt] Polling error [Errno Could not read data from Mi sensor %s] 4C:65:A8:D8:AA:E1

Can you help me?

Hi sorry to ask here @dolezsa
I want to use this xiaomi BLE temp and humid sensor… However I’m running Hassio in Ubuntu VM under Win 10 pc without any integrated Bluetooth adapter. I’m planning to buy a Bluetooth adapter. However in my local area I can’t seem to find an adapter which is compatible with this xiaomi. The one avalaible is USB Bluetooth dongle compatible with Bluetooth version 4.0 for about $5.

Do you think this kind of dongle will be compatible and able to found the xiaomi sensor? Could you please post a pic or link of your Bluetooth dongle?

hi, you can buy from aliexpress or from something similiar one piece like this:
image
it costs around 3 USD. the requirement is that it supports BT 4.0.

Ah perfect! Yes this is the kind I’m looking at in my local place… Will order it soon while waiting for the xiaomi gateway and sensors to arrive… Thank you very much for the confirmation…

Did this get pushed through to Home Assistant in the end?
Last night it got below 10 degrees for the first time and sure enough I seen the same thing on my graphs.

I’m running HassOS 0.75.3 so wondering if I update will it fix this?

This cheap BLE Dongles have one disadvantage - very low range/radio coverage. One wall and 5-6 meters makes connection unreliable (RSSI -90dBi) with sensor, a lot of read timeouts in HASS logs.
I’m looking for better dongle, with good (maybe external?) antenna.