Python Script or plugin for Xiaomi Mi Temp and Humidity Sensor

Python and python framework

Moderator: leecollings

Post Reply
Auflauf
Posts: 1
Joined: Wednesday 19 February 2020 16:04
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: CH
Contact:

Python Script or plugin for Xiaomi Mi Temp and Humidity Sensor

Post by Auflauf »

Hi Everybody,

i have four Xiaomi Mi Temperature and Humidity Sensors (lLYWSDCGQ/01ZM https://www.aliexpress.com/i/32849595532.html) and i'm searching for a way to integrate them into domoticz. I found a library with examplescript to poll them directly from the raspi without gateway (https://zsiti.eu/xiaomi-mijia-hygrother ... pberry-pi/). Is there a way to parse the output directly into domoticz as sensor?

thanx for any help in advance
Regards
Mike70
Posts: 21
Joined: Thursday 22 October 2015 19:46
Target OS: Raspberry Pi / ODroid
Domoticz version: bèta
Location: Netherlands
Contact:

Re: Python Script or plugin for Xiaomi Mi Temp and Humidity Sensor

Post by Mike70 »

Here you can find a script and a manual for use with Domoticz.
https://github.com/pFenners/mijia-sensor-domoticz

Use

Code: Select all

sudo hcitool lescan
to find the sensor and note the MAC address of the device.

After downloading the code from GitHub, you must make 3 changes to the file
domoticz_mijia.py
At the first line type:

Code: Select all

#! /usr/bin/python3
And line 25 you must change the word:

Code: Select all

base64.encodestring
into

Code: Select all

base64.encodebytes
At the end of the script there are 3 examples of the function update.
Eg.

Code: Select all

update("58:2D:34:39:18:66","120")
Remove the # of one of the functions and replace the MAC address with the one you noted.
The number after the comma, "120" is the IDX of the sensor in Domoticz.
To test it you can now run the command:

Code: Select all

./domoticz_mijia.py
Last edited by Mike70 on Thursday 09 April 2020 21:08, edited 1 time in total.
User avatar
Hesitated
Posts: 42
Joined: Monday 04 June 2018 21:40
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.11665
Location: Ede
Contact:

Re: Python Script or plugin for Xiaomi Mi Temp and Humidity Sensor

Post by Hesitated »

Mike70 wrote: Saturday 04 April 2020 14:48 After downloading the code from GitHub, you must make 3 changes to the file
domoticz_mijia.py
Where do i find this file? It's not in the repository...
Domoticz 2020.2 - RPi3B+ - RFXtrx433XL - P1 - CC2531 - Somfy - Xiaomi - Hue - Omron - MQTT
Mike70
Posts: 21
Joined: Thursday 22 October 2015 19:46
Target OS: Raspberry Pi / ODroid
Domoticz version: bèta
Location: Netherlands
Contact:

Re: Python Script or plugin for Xiaomi Mi Temp and Humidity Sensor

Post by Mike70 »

Sorry.
I used the wrong github address.
You should use this one: https://github.com/pFenners/mijia-sensor-domoticz
User avatar
Hesitated
Posts: 42
Joined: Monday 04 June 2018 21:40
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.11665
Location: Ede
Contact:

Re: Python Script or plugin for Xiaomi Mi Temp and Humidity Sensor

Post by Hesitated »

Minor detail... :D

First upgrade to Buster this weekend, then I'll look into it.

Thanks!
Domoticz 2020.2 - RPi3B+ - RFXtrx433XL - P1 - CC2531 - Somfy - Xiaomi - Hue - Omron - MQTT
snellejellep
Posts: 241
Joined: Tuesday 16 May 2017 13:05
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Neterlands
Contact:

Re: Python Script or plugin for Xiaomi Mi Temp and Humidity Sensor

Post by snellejellep »

how does this work? using a esp32 of some kind with bluetooth? i could not find which device to use for bluetooth connection. i do not want to use the bluetooth on my pi since it does not cover the whole house. with multiple esp32 it would be possible but it is not listed how to do that.
raspberry pi | xiaomi vacuum | yeelight | philips hue | zwave | ubiquiti unifi | harmony | sonoff | zigbee2mqtt | https://www.youtube.com/channel/UC2Zidl ... m1OLuNldfQ
Mike70
Posts: 21
Joined: Thursday 22 October 2015 19:46
Target OS: Raspberry Pi / ODroid
Domoticz version: bèta
Location: Netherlands
Contact:

Re: Python Script or plugin for Xiaomi Mi Temp and Humidity Sensor

Post by Mike70 »

I noticed that 1 Rpi does not receive all the units in our house. You could use an ESP32 on each floor.
I have a Rpi on each floor. That is a choice you can make.
For use with an ESP32, see this video: https://www.youtube.com/watch?v=RtJpx8BZvMw
Menno
Posts: 35
Joined: Wednesday 24 July 2019 13:23
Target OS: Linux
Domoticz version:
Contact:

Re: Python Script or plugin for Xiaomi Mi Temp and Humidity Sensor

Post by Menno »

I adjusted the script as per instructions.
When I run the script I get:

Traceback (most recent call last):
File "./domoticz_mijia.py", line 5, in <module>
from mijia.mijia_poller import MijiaPoller, \
ModuleNotFoundError: No module named 'mijia'

I also installed the script mija_poller, made it executable and added python3 to it.
Any ideas?
henrybcn
Posts: 31
Joined: Wednesday 31 August 2016 11:17
Target OS: OS X
Domoticz version:
Contact:

Re: Python Script or plugin for Xiaomi Mi Temp and Humidity Sensor

Post by henrybcn »

Hi All, could anyone solve tis?
I am also stuck after following the instructions above:

Traceback (most recent call last):
File "domoticz_mijia.py", line 5, in <module>
from mijia.mijia_poller import MijiaPoller, \
ModuleNotFoundError: No module named 'mijia'


Any help? :D
User avatar
waltervl
Posts: 5149
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Python Script or plugin for Xiaomi Mi Temp and Humidity Sensor

Post by waltervl »

Perhaps easier to use a Python plugin like viewtopic.php?f=65&t=22281
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
henrybcn
Posts: 31
Joined: Wednesday 31 August 2016 11:17
Target OS: OS X
Domoticz version:
Contact:

Re: Python Script or plugin for Xiaomi Mi Temp and Humidity Sensor

Post by henrybcn »

In case some of you are interested, here the solution for the Mijia V2 (
https://github.com/erdose/xiaomi-mi-lywsd03mmc
model LYWSD03MMC
I am getting sometimes an error "Failed to connect to peripheral", but it works from time to time and I can see the data in my domoticz
Menno
Posts: 35
Joined: Wednesday 24 July 2019 13:23
Target OS: Linux
Domoticz version:
Contact:

Re: Python Script or plugin for Xiaomi Mi Temp and Humidity Sensor

Post by Menno »

Hey, thanks for that!
Getting somewhat further now.
Just like you I get the occasional "Failed to connect", but also " Bluetooth command failed (code: 1, error: Invalid handle)".
And when it is working, I don't see the "INFO The request is http://192.168.178.23:8080/json.htm?typ ... battery=74" line in my log file.
No data appearing in Domoticz.
When I use curl to post the data myself, I see it appear in Domoticz, so the error seems to be on the side of the script, not on the Domoticz side.

menno@quokka:~$ curl 'http://192.168.178.23:8080/json.htm?typ ... battery=74'
{
"status" : "OK",
"title" : "Update Device"
}
Menno
Posts: 35
Joined: Wednesday 24 July 2019 13:23
Target OS: Linux
Domoticz version:
Contact:

Re: Python Script or plugin for Xiaomi Mi Temp and Humidity Sensor

Post by Menno »

waltervl wrote: Tuesday 10 August 2021 17:27 Perhaps easier to use a Python plugin like viewtopic.php?f=65&t=22281
That's for the Flower Mate, I don't think it works for these devices.
I also own a Flower Mate and the scripts works for it, but does not pick up my three Xiaomi Temperature and Humidity Sensors.
Meanwhile, back to getting the module not found error.
User avatar
waltervl
Posts: 5149
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Python Script or plugin for Xiaomi Mi Temp and Humidity Sensor

Post by waltervl »

For Python module load errors check the last paragraph of the wiki https://www.domoticz.com/wiki/Using_Python_plugins
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Lacman
Posts: 4
Joined: Sunday 27 February 2022 20:04
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Hungary
Contact:

Re: Python Script or plugin for Xiaomi Mi Temp and Humidity Sensor

Post by Lacman »

Menno wrote: Saturday 09 October 2021 15:28 Hey, thanks for that!
Getting somewhat further now.
Just like you I get the occasional "Failed to connect", but also " Bluetooth command failed (code: 1, error: Invalid handle)".
And when it is working, I don't see the "INFO The request is http:// 192 .168 .178 .23:8080/json .htm?type=command&param=udevice&idx=1053&nvalue=0&svalue=28 .72;66;1&battery=74" line in my log file.
No data appearing in Domoticz.
When I use curl to post the data myself, I see it appear in Domoticz, so the error seems to be on the side of the script, not on the Domoticz side.

menno@quokka:~$ curl 'http:// 192 .168 .178 .23 :8080/json .htm?type=command&param=udevice&idx=1053&nvalue=0&svalue=20;66;1&battery=74'
{
"status" : "OK",
"title" : "Update Device"
}
Hi Menno,

Can I ask you to come over to the thread of the Temp & Hum Sensor 2, and give me some hints to move forward making it working for me, please?
For me the script is not even getting to this part, only prints the following lines:

Code: Select all

2023-08-06 17:20:02 INFO     ***************************************
2023-08-06 17:20:02 INFO     Start script...
2023-08-06 17:20:02 INFO     Input parameters:
Domoticz Server IP: 127 .0 .0 .1
Domoticz Server Port: 8080
Domoticz Server User: <myuserhere>
Domoticz Server Password: <mypasshere>
I'd appreciate your, or anyone's help.
Lacman
Posts: 4
Joined: Sunday 27 February 2022 20:04
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Hungary
Contact:

Re: Python Script or plugin for Xiaomi Mi Temp and Humidity Sensor

Post by Lacman »

Lacman wrote: Sunday 06 August 2023 17:22
Menno wrote: Saturday 09 October 2021 15:28 Hey, thanks for that!
Getting somewhat further now.
Just like you I get the occasional "Failed to connect", but also " Bluetooth command failed (code: 1, error: Invalid handle)".
And when it is working, I don't see the "INFO The request is http:// 192 .168 .178 .23:8080/json .htm?type=command&param=udevice&idx=1053&nvalue=0&svalue=28 .72;66;1&battery=74" line in my log file.
No data appearing in Domoticz.
When I use curl to post the data myself, I see it appear in Domoticz, so the error seems to be on the side of the script, not on the Domoticz side.

menno@quokka:~$ curl 'http:// 192 .168 .178 .23 :8080/json .htm?type=command&param=udevice&idx=1053&nvalue=0&svalue=20;66;1&battery=74'
{
"status" : "OK",
"title" : "Update Device"
}
Hi Menno,

Can I ask you to come over to the thread of the Temp & Hum Sensor 2, and give me some hints to move forward making it working for me, please?
For me the script is not even getting to this part, only prints the following lines:

Code: Select all

2023-08-06 17:20:02 INFO     ***************************************
2023-08-06 17:20:02 INFO     Start script...
2023-08-06 17:20:02 INFO     Input parameters:
Domoticz Server IP: 127 .0 .0 .1
Domoticz Server Port: 8080
Domoticz Server User: <myuserhere>
Domoticz Server Password: <mypasshere>
I'd appreciate your, or anyone's help.
I started to play with the script, and eventually changing the scanner.scan first param from 10 to 15 made it work:

Code: Select all

try:
        scanner = Scanner().withDelegate(TempHumDelegate())
        scanner.scan(15.0, passive=True)
except Exception as e:
        logger.error(str(e))
        pass
I have 2 sensors to scan and read their data.

BR,
Lacman
Menno
Posts: 35
Joined: Wednesday 24 July 2019 13:23
Target OS: Linux
Domoticz version:
Contact:

Re: Python Script or plugin for Xiaomi Mi Temp and Humidity Sensor

Post by Menno »

Lacman wrote: Sunday 13 August 2023 21:10

Can I ask you to come over to the thread of the Temp & Hum Sensor 2,
BR,
Lacman
Nothing there at that link, but did you get it working?
Menno
Posts: 35
Joined: Wednesday 24 July 2019 13:23
Target OS: Linux
Domoticz version:
Contact:

Re: Python Script or plugin for Xiaomi Mi Temp and Humidity Sensor

Post by Menno »

The script no longer works.
I get 401 errors, despite having allowed basic-auth and having the proper IP-ranges as trusted networks.
Anyone else experiencing this and more importantly, having it fixed?
Havym
Posts: 29
Joined: Tuesday 08 October 2019 12:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Enschede (NL)
Contact:

Re: Python Script or plugin for Xiaomi Mi Temp and Humidity Sensor

Post by Havym »

Yup, i'm also having troubles..... also with this plugin... viewtopic.php?t=34348
https://github.com/erdose/xiaomi-mi-lywsd03mmc
It's since the domoticz update 2023.2
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest