miflora-mqtt-daemon icon indicating copy to clipboard operation
miflora-mqtt-daemon copied to clipboard

Support for Xiaomi Mijia Bluetooth Temperature

Open robotsflo opened this issue 7 years ago β€’ 36 comments

Hi, Can you please add support for Xiaomi Mijia Temperature ? Or create a new " mitemp-mqtt-daemon " repository. Thanks a lot for your good job !

https://de.aliexpress.com/store/product/Neue-Original-Xiaomi-Mijia-Bluetooth-Temperatur-Luftfeuchtigkeit-Digitale-Thermometer-Feuchtigkeit-Meter-Sensor-Lcd-bildschirm-Smart-Mi/2953070_32844484376.html?spm=a2g0x.12010615.8148356.12.22227ed6B3WpL1

robotsflo avatar Aug 01 '18 10:08 robotsflo

Please add it as a feature, my dear friend.

0xgrm avatar Aug 03 '18 17:08 0xgrm

And how do we add this as a feature? I like this project for my Mi Floras and have 6 of this MiTemp devices in the house.

tunip avatar Aug 07 '18 11:08 tunip

+1

khzd avatar Aug 20 '18 05:08 khzd

+1 there is no mijia mqtt solution for the raspberry pi.

cropab avatar Dec 18 '18 09:12 cropab

+1 to this too.

sendorm avatar Jan 28 '19 19:01 sendorm

You can test this https://github.com/aqualx/miflora-mqtt-daemon/tree/mitempbt If test succeed I'll make PR.

aqualx avatar Jan 28 '19 22:01 aqualx

You can test this https://github.com/aqualx/miflora-mqtt-daemon/tree/mitempbt If test succeed I'll make PR.

I am getting this error with your code: ImportError: No module named 'mitemp_bt' which is fixed with sudo pip3 install mitemp_bt :)

But keep getting: The MAC address "xx:xx:xx:xx:xx:xx" seems to be in the wrong format. Which is the mac address of the mitemp sensor.

sendorm avatar Jan 29 '19 18:01 sendorm

mitemp_bt added to requirements.txt. So this will install all needed packages:

sudo pip3 install -r requirements.txt

Which MAC address has your sensor? You have sensor with screen or without? I have two of them and both start from "4C:65:A8:DB:XX:XX". In any case this is checked in miflora-mqtt-daemon.py line 136...

PS: section names in config.ini also changed. See example at config.ini.dist

aqualx avatar Jan 29 '19 19:01 aqualx

Ok I see the problem, mine is '4C:65:A8:D4:XX:XX'. I'll try with that. I've also changed the poll rates to 180 times less for the mitemp while maintaining the miflora sleep times and also set the mqtt retain to true for homeassistant case. I wonder why the default is false.

sendorm avatar Jan 29 '19 19:01 sendorm

Ok, changed RegExp for MAC address. What do you mean by "changed the poll rates to 180 times less for the mitemp"? Check more frequently than miflora or ...?

aqualx avatar Jan 29 '19 20:01 aqualx

Yes exactly. As miflora's are for plants which don't often need an update. I use the data to auto water the plants. But with these cold days, the plants are only needed to be watered each 10 days or so. So I use a sensor refresh for 180 minutes. But with the mitemp sensors which are indoors I use a sensor refresh time of 1 minute each.

Another thing; bluepy is also needed for the mitemp.

One last thing to say, the code is working perfectly (It gets autodiscovered by homeassistant). Thank you :)

sendorm avatar Jan 29 '19 20:01 sendorm

But bluepy is already in requirements.txt

bluepy==1.3.0
mitemp-bt==0.0.1

aqualx avatar Jan 29 '19 20:01 aqualx

Yes you are right. I didn't check your tree's requirements.

sendorm avatar Jan 29 '19 22:01 sendorm

Ok I see the problem, mine is '4C:65:A8:D4:XX:XX'. I'll try with that. I've also changed the poll rates to 180 times less for the mitemp while maintaining the miflora sleep times and also set the mqtt retain to true for homeassistant case. I wonder why the default is false.

Is there a possibility for the user to modify the polling rate in the config file?

0xgrm avatar Jan 30 '19 10:01 0xgrm

In config.ini there is setting:

[Daemon]
# The period between two measurements in seconds (Default: 300)
#period = 300

But for now it is used for both "Mi Flora" and "Xiaomi Mijia Bluetooth Temperature" type of devices.

aqualx avatar Jan 30 '19 10:01 aqualx

I see. Maybe could you add the possibility to have 2 different polling rates for both Mi Flora and Xiaomi Mijia Bluetooth Temperature devices?

0xgrm avatar Jan 30 '19 10:01 0xgrm

You can check here for different polling rates. https://github.com/sendorm/miflora-mqtt-daemon I copied the code from @aqualx

sendorm avatar Jan 30 '19 19:01 sendorm

New commit:

  • added separate setting "period" per device type in config.ini
  • switched library for 'Mijia Bluetooth Temperature Smart Humidity' from mitemp_bt to mithermometer
  • fixed schema generation for openHAB
  • set 'retain=True' for mqtt messages for homeassistant-mqtt
  • other minor fixes

aqualx avatar Jan 30 '19 22:01 aqualx

It's working, thank you @aqualx.

sendorm avatar Feb 02 '19 14:02 sendorm

I am using the version by Aqualx, but after a few hours I get this error

xception in thread Thread-3: Traceback (most recent call last): File "/usr/local/lib/python3.5/dist-packages/btlewrap/bluepy.py", line 26, in _func_wrapper return func(*args, **kwargs) File "/usr/local/lib/python3.5/dist-packages/btlewrap/bluepy.py", line 55, in connect self._peripheral = Peripheral(mac, iface=iface) File "/usr/local/lib/python3.5/dist-packages/bluepy/btle.py", line 391, in init self._connect(deviceAddr, addrType, iface) File "/usr/local/lib/python3.5/dist-packages/bluepy/btle.py", line 439, in _connect raise BTLEDisconnectError("Failed to connect to peripheral %s, addr type: %s" % (addr, addrType), rsp) bluepy.btle.BTLEDisconnectError: Failed to connect to peripheral 4C:65:A8:D0:5E:CE, addr type: public

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/usr/local/lib/python3.5/dist-packages/btlewrap/base.py", line 44, in enter self._backend.connect(self._mac) File "/usr/local/lib/python3.5/dist-packages/btlewrap/bluepy.py", line 32, in _func_wrapper raise BluetoothBackendException() from last_error btlewrap.base.BluetoothBackendException

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner self.run() File "miflora-mqtt-daemon.py", line 508, in run pool_sensors(self.sensor_type, self.sensors, self.sensor_parameters) File "miflora-mqtt-daemon.py", line 207, in pool_sensors sensor['poller'].fill_cache() File "/usr/local/lib/python3.5/dist-packages/mithermometer/mithermometer_poller.py", line 67, in fill_cache with self._bt_interface.connect(self._mac) as connection: File "/usr/local/lib/python3.5/dist-packages/btlewrap/base.py", line 46, in enter self._lock.release() RuntimeError: release unlocked lock

lionhe1966 avatar Feb 08 '19 20:02 lionhe1966

There are some exceptions from calling libraries. In last commit there is a guard against them... PS: Maybe this exceptions should be also addressed to owner. In this case mithermometer

aqualx avatar Feb 08 '19 20:02 aqualx

"Release unlocked lock" seems is now fixed directly in blewrap library on this change

aqualx avatar Feb 16 '19 11:02 aqualx

I tried to use latest miflora-mqtt-daemon for reading Xiaomi Mijia, but I got "seems to be in the wrong format" error. Is the change mentioned in this thread merged into the master already or where I can find it?

igorbljahhin avatar Mar 14 '19 08:03 igorbljahhin

I tried to use latest miflora-mqtt-daemon for reading Xiaomi Mijia, but I got "seems to be in the wrong format" error. Is the change mentioned in this thread merged into the master already or where I can find it?

OK, I found it, it is in https://github.com/aqualx/miflora-mqtt-daemon.

igorbljahhin avatar Mar 14 '19 08:03 igorbljahhin

Which MAC address has your sensor? You have sensor with screen or without? I have two of them and both start from "4C:65:A8:DB:XX:XX". In any case this is checked in miflora-mqtt-daemon.py line 136...

PS: section names in config.ini also changed. See example at config.ini.dist

I had the same problem, my temperature sensor mitempbt is like this: Tempe1 = 58:2D:34:32:xx:xx which is not accepted and gives an error every time. As a brute force solution I commented out the mac check in miflora-mqtt-daemon.py and now it works.

Paul-B avatar May 31 '19 09:05 Paul-B

This seems to be an epidemic. I ordered the Mijia Temperature and Humidity sensor and the MAC address is 58:2d:34:31:4b:e4. I tried commenting the mac check in miflora-mqtt-daemon.py but it did not work for me. @Paul-B can you help? What all did you comment out?

Ideally, if @ThomDietrich or @aqualx can make the necessary changes to the code so that the mac address check is either removed completely or is modified to accept 58:2D series, would be the best solution.

Thanks in advance.

SumitBajoria avatar Jul 20 '19 18:07 SumitBajoria

@aqualx Will this change be sufficient or do I need to do anything else?

Init sensors from configuration files

def init_sensors(sensor_type, sensors): sensor_type_name = sensor_type_to_name(sensor_type) if sensor_type == sensor_type_miflora: config_section = sensor_type_miflora mac_regexp = "C4:7C:8D:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}" elif sensor_type == sensor_type_mitempbt: config_section = sensor_type_mitempbt mac_regexp = "4C:65:A8:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}" elif sensor_type == sensor_type_mitempbt: config_section = sensor_type_mitempbt mac_regexp = "58:2d:34:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}" else: print_line('Unknown device type: {}'.format(sensor_type), error=True, sd_notify=True) sys.exit(1)

Help will be appreciated!

SumitBajoria avatar Jul 21 '19 02:07 SumitBajoria

@aqualx Nothing worked so I made the following change:

def init_sensors(sensor_type, sensors):
    sensor_type_name = sensor_type_to_name(sensor_type)
    if  sensor_type == sensor_type_miflora:
        config_section = sensor_type_miflora
        mac_regexp = "C4:7C:8D:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}"
    elif sensor_type == sensor_type_mitempbt:
        config_section = sensor_type_mitempbt
        mac_regexp = "58:28:34:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}"

Now when I execute the script, I get the following:

pi@raspberrypi:/opt/miflora-mqtt-daemon $ python3 /opt/miflora-mqtt-daemon/miflora-mqtt-daemon.py

Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon Source: https://github.com/ThomDietrich/miflora-mqtt-daemon

[2019-07-21 05:32:39] Connecting to MQTT broker ... [2019-07-21 05:32:39] MQTT connection established

Adding sensor to device list and testing connection ... Name: "bedroom" Internal name: "bedroom" Device name: "MJ_HT_V1" MAC address: 58:2D:34:31:4B:E4 Firmware: 00.00.66 [2019-07-21 05:32:44] Initial connection to Mijia Bluetooth Temperature Smart Humidity sensor "bedroom" (58:2D:34:31:4B:E4) successful

[2019-07-21 05:32:44] Announcing Mi Flora/Mijia Bluetooth Temperature Smart Humidity devices to MQTT broker for auto-discovery ... Traceback (most recent call last): File "/opt/miflora-mqtt-daemon/miflora-mqtt-daemon.py", line 498, in mqtt_client.publish('{}/{}_{}/config'.format(topic_path, mitempbt_name, sensor).lower(), json.dumps(payload), 1, True) File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 1134, in publish raise ValueError('Publish topic cannot contain wildcards.') ValueError: Publish topic cannot contain wildcards.

SumitBajoria avatar Jul 21 '19 04:07 SumitBajoria

Done! I modified the miflora-mqtt-daemon.py file to include the 58:2D:34 series of MAC addresses.

The "ValueError: Publish topic cannot contain wildcards" was because I did not edit the config.ini file properly. Tweaked that and now it seems to be working. Now to get Home Assistant to use and populate the data.

SumitBajoria avatar Jul 21 '19 07:07 SumitBajoria

@SumitBajoria Could you share the code? Thanks!

jsponz avatar Jul 27 '19 15:07 jsponz