Xiaomi Mijia bluetooth temperature & humidity sensor compatibility

working great! i have only one issue, log file updates constantly with the following errors:

2019-08-19 18:16:47 ERROR (MainThread) [googledevices] ClientError contacting http://192.168.0.196:8008/setup/bluetooth/scan_results - 0, message='Attempt to decode JSON with unexpected mimetype: '
2019-08-19 18:16:58 ERROR (MainThread) [googledevices] ClientError contacting http://192.168.0.196:8008/setup/bluetooth/scan_results - 0, message='Attempt to decode JSON with unexpected mimetype: '
2019-08-19 18:17:09 ERROR (MainThread) [googledevices] ClientError contacting http://192.168.0.196:8008/setup/bluetooth/scan_results - 0, message='Attempt to decode JSON with unexpected mimetype: '
2019-08-19 18:17:20 ERROR (MainThread) [googledevices] ClientError contacting http://192.168.0.196:8008/setup/bluetooth/scan_results - 0, message='Attempt to decode JSON with unexpected mimetype: '
2019-08-19 18:17:30 ERROR (MainThread) [googledevices] ClientError contacting http://192.168.0.196:8008/setup/bluetooth/scan_results - 0, message='Attempt to decode JSON with unexpected mimetype: '
2019-08-19 18:17:41 ERROR (MainThread) [googledevices] ClientError contacting http://192.168.0.196:8008/setup/bluetooth/scan_results - 0, message='Attempt to decode JSON with unexpected mimetype: '
2019-08-19 18:17:52 ERROR (MainThread) [googledevices] ClientError contacting http://192.168.0.196:8008/setup/bluetooth/scan_results - 0, message='Attempt to decode JSON with unexpected mimetype: '
2019-08-19 18:18:03 ERROR (MainThread) [googledevices] ClientError contacting http://192.168.0.196:8008/setup/bluetooth/scan_results - 0, message='Attempt to decode JSON with unexpected mimetype: '
2019-08-19 18:18:14 ERROR (MainThread) [googledevices] ClientError contacting http://192.168.0.196:8008/setup/bluetooth/scan_results - 0, message='Attempt to decode JSON with unexpected mimetype: '
2019-08-19 18:18:25 ERROR (MainThread) [googledevices] ClientError contacting http://192.168.0.196:8008/setup/bluetooth/scan_results - 0, message='Attempt to decode JSON with unexpected mimetype: '
2019-08-19 18:18:36 ERROR (MainThread) [googledevices] ClientError contacting http://192.168.0.196:8008/setup/bluetooth/scan_results - 0, message='Attempt to decode JSON with unexpected mimetype: '
2019-08-19 18:18:47 ERROR (MainThread) [googledevices] ClientError contacting http://192.168.0.196:8008/setup/bluetooth/scan_results - 0, message='Attempt to decode JSON with unexpected mimetype: '
2019-08-19 18:18:58 ERROR (MainThread) [googledevices] ClientError contacting http://192.168.0.196:8008/setup/bluetooth/scan_results - 0, message='Attempt to decode JSON with unexpected mimetype: '
2019-08-19 18:19:09 ERROR (MainThread) [googledevices] ClientError contacting http://192.168.0.196:8008/setup/bluetooth/scan_results - 0, message='Attempt to decode JSON with unexpected mimetype: '
2019-08-19 18:19:20 ERROR (MainThread) [googledevices] ClientError contacting http://192.168.0.196:8008/setup/bluetooth/scan_results - 0, message='Attempt to decode JSON with unexpected mimetype: '
2019-08-19 18:19:31 ERROR (MainThread) [googledevices] ClientError contacting http://192.168.0.196:8008/setup/bluetooth/scan_results - 0, message='Attempt to decode JSON with unexpected mimetype: '
2019-08-19 18:19:42 ERROR (MainThread) [googledevices] ClientError contacting http://192.168.0.196:8008/setup/bluetooth/scan_results - 0, message='Attempt to decode JSON with unexpected mimetype: '
2019-08-19 18:19:53 ERROR (MainThread) [googledevices] ClientError contacting http://192.168.0.196:8008/setup/bluetooth/scan_results - 0, message='Attempt to decode JSON with unexpected mimetype: '
2019-08-19 18:20:04 ERROR (MainThread) [googledevices] ClientError contacting http://192.168.0.196:8008/setup/bluetooth/scan_results - 0, message='Attempt to decode JSON with unexpected mimetype: '
2019-08-19 18:20:14 ERROR (MainThread) [googledevices] ClientError contacting http://192.168.0.196:8008/setup/bluetooth/scan_results - 0, message='Attempt to decode JSON with unexpected mimetype: '
2019-08-19 18:20:25 ERROR (MainThread) [googledevices] ClientError contacting http://192.168.0.196:8008/setup/bluetooth/scan_results - 0, message='Attempt to decode JSON with unexpected mimetype: '
2019-08-19 18:20:36 ERROR (MainThread) [googledevices] ClientError contacting http://192.168.0.196:8008/setup/bluetooth/scan_results - 0, message='Attempt to decode JSON with unexpected mimetype: '
2019-08-19 18:20:47 ERROR (MainThread) [googledevices] ClientError contacting http://192.168.0.196:8008/setup/bluetooth/scan_results - 0, message='Attempt to decode JSON with unexpected mimetype: '

Thank you for your help

Seems like these errors are not related to the work of the discussed integration… I suspect this is something from the google home integration… Presence detection, maybe?

How can I set the interval scan time? In my graph it seems to be a point every minute (sometimes 2-3 points in 1 minute). Is there a way to have a point plotted every 5 minutes (for example)?

Unfortunately, I am completely newbie to the Home assistant, and do not have (yet) sufficient knowledge to make proper integration with the options. I look forward to when @tsymbaliuk has time for us)
I don’t have time yet to study the issue, so for now we can only change in my sensor.py in line 221 the text seconds = 60 to the text seconds = 300 (“minutes = 5” should also work, as it seems to me). And restart HA.

There is a minor issue with your averaging. If the sensors are far away from the receiver and reception is not very good, you may not acquire a single data point within a minute. The division by zero for the averaging makes the code fails and it does not recover until you restart home assistant.
There should be a check on the number of acquired points before doing the averaging.

1 Like

Thanks. It is strange, because if you do not receive any data, then the case should not come to the averaging… Anyway, I made an edit to my gist.

The other stuff it used, e.g. the address of the device, is no longer needed?

The second one is the mitemp_bt, how can be shown only 1 digit after the point, like on first sensor?
image

Yes. Find this line

sensors[1]._state = round(hum_m_sum[mac] / hum_m_count[mac], 2)

and change 2 at the end to the desired number of decimal places.

Thank you! :slight_smile:

1 Like

I haven’t checked the details of the code but here is the error message in my log :

>  Error doing job: Future exception was never retrieved
> Traceback (most recent call last):
>   File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
>     result = self.fn(*self.args, **self.kwargs)
>   File "/config/custom_components/mitemp_bt/sensor.py", line 214, in update_ble
>     devs = discover_ble_devices()
>   File "/config/custom_components/mitemp_bt/sensor.py", line 191, in discover_ble_devices
>     sensors[0]._state = round(temp_m_sum[mac] / temp_m_count[mac], 2)
> ZeroDivisionError: division by zero

Thank you. As I said, I added handling of this error and updated my gist. Now with this error, the data update should not stop. I can’t figure it out yet. Over the weekend, I’ll move my sensor to the reception border to try to reproduce the problem.

No, it’s no longer needed :grinning:

I have been testing your updated code with the error handling for about 2 days now, and it seems to work very well ! I had 2 occurence of “0 data” but it kept working.
Thank you for your work ! It should be the default platform for these sensors.

1 Like

I previously tried to get a USB dongle (5 of them actually) to work directly connected to HA but failed miserably, I ended up using a Raspberry Pi Zero to act as a BTLE to MQTT bridge which worked fine for for months, but I’ve since switched to using ESPHome and an ESP32 and it seems to work pretty well so far and ESPHome’s MQTT/Api is solid so it just appears in integrations with zero configuration needing to be done, just add it in LoveLace.

It is a little bit off-topic, but now that the component is working fine again, I am willing to purchase a few more sensors for my house. I have found an interesting offer on AliExpress : https://www.aliexpress.com/item/32843489009.html. They are cheaper than all other sellers I have found (~$10 per unit, compared to $15-17 on other stores on Bangood, Gearbest etc.). Do you have experience in buying on AliExpress, and maybe with this specific vendor ? Is there any risk of getting counterfeited products ?

Nice cards @krovachek, mind sharing the config for them?

Thank you! Yes I will. You can find some older config here:

Thanks for the custom component ! Seems to work constantly but im having some spikes… any ideas how to fix ?

Working like a charm for me.
However I didn’t implement @Magalex fix to avoid flooding my database with those sensors info.

I wish we could specify in the recorder how much data we want to save for each specific entity or to just hold it in memory for use in graphs.

I have tons of graphs that I only need to display 1-6 hours of data, not more, after that it can be discarded. I wouldn’t not even have it to write to the database.
But I have sensors for which I require longer periods of time 1 day, 1 week or even 1 month history. Since it cannot be managed individually it’s a month for all entities in the recorder