ThomDietrich / miflora-mqtt-daemon

Linux service to collect and transfer Xiaomi Mi Flora plant sensor data via MQTT to your smart home system, with cluster support 🌱🌼πŸ₯€πŸ‘🌳
MIT License
607 stars 139 forks source link

Support for Xiaomi Mijia Bluetooth Temperature #51

Open robotsflo opened 6 years ago

robotsflo commented 6 years ago

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

0xgrm commented 6 years ago

Please add it as a feature, my dear friend.

tunip commented 6 years ago

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.

khzd commented 6 years ago

+1

cropab commented 5 years ago

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

sendorm commented 5 years ago

+1 to this too.

aqualx commented 5 years ago

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

sendorm commented 5 years ago

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.

aqualx commented 5 years ago

_mitempbt 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

sendorm commented 5 years ago

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.

aqualx commented 5 years ago

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 ...?

sendorm commented 5 years ago

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 :)

aqualx commented 5 years ago

But bluepy is already in requirements.txt

bluepy==1.3.0
mitemp-bt==0.0.1
sendorm commented 5 years ago

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

0xgrm commented 5 years ago

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?

aqualx commented 5 years ago

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.

0xgrm commented 5 years ago

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

sendorm commented 5 years ago

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

aqualx commented 5 years ago

New commit:

sendorm commented 5 years ago

It's working, thank you @aqualx.

lionhe1966 commented 5 years ago

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

aqualx commented 5 years ago

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 commented 5 years ago

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

igorbljahhin commented 5 years ago

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 commented 5 years ago

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.

Paul-B commented 5 years ago

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.

SumitBajoria commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

@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 mqttclient.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 commented 5 years ago

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.

jsponz commented 5 years ago

@SumitBajoria Could you share the code? Thanks!

SumitBajoria commented 5 years ago

@jsponz Which code do you want me to share? Also, I managed to fix the miflora.service, albeit, in a very crude way.

jsponz commented 5 years ago

@SumitBajoria you wrote: "I modified the miflora-mqtt-daemon.py ..."

Last available version of the file is 6 months old...

SumitBajoria commented 5 years ago

`# 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 = "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)

`

CWempe commented 5 years ago

I enhanced the regexp and created a pull request.

https://github.com/aqualx/miflora-mqtt-daemon/pull/3

wojciej commented 4 years ago

Hello, I just have purchased the new version of Xiaomi Temperature and humidity sensor IMG_3648

It is detectable over bluetooth its Mac start with A4:C1:38.

I have tried simple approach by changing the regex at aqualux fork but this does not seems to work.

If you could point me out to the right direction, or what kind of data should I provide to make this work that would be great.

Thanks you all who contribute to this great tool.

BR Jarek

AndreB81 commented 4 years ago

Hi Jarek,

issue is that this sensor is decrypted via a token. Binding the token to the miflora-mqtt-daemon is not supported by now.

BR Andre