Page 1 of 1

Python Script or plugin for Xiaomi Mi Temp and Humidity Sensor

Posted: Wednesday 19 February 2020 16:22
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

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

Posted: Saturday 04 April 2020 14:48
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

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

Posted: Saturday 04 April 2020 15:31
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...

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

Posted: Thursday 09 April 2020 21:09
by Mike70
Sorry.
I used the wrong github address.
You should use this one: https://github.com/pFenners/mijia-sensor-domoticz

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

Posted: Thursday 09 April 2020 21:51
by Hesitated
Minor detail... :D

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

Thanks!

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

Posted: Friday 10 April 2020 13:22
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.

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

Posted: Friday 10 April 2020 15:19
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

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

Posted: Saturday 13 March 2021 14:24
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?

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

Posted: Tuesday 10 August 2021 17:02
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

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

Posted: Tuesday 10 August 2021 17:27
by waltervl
Perhaps easier to use a Python plugin like viewtopic.php?f=65&t=22281

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

Posted: Monday 16 August 2021 15:10
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

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

Posted: Saturday 09 October 2021 15:28
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"
}

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

Posted: Wednesday 23 November 2022 15:46
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.

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

Posted: Wednesday 23 November 2022 17:53
by waltervl
For Python module load errors check the last paragraph of the wiki https://www.domoticz.com/wiki/Using_Python_plugins

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

Posted: Sunday 06 August 2023 17:22
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.

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

Posted: Sunday 13 August 2023 21:10
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

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

Posted: Saturday 19 August 2023 14:53
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?

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

Posted: Sunday 20 August 2023 17:11
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?

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

Posted: Sunday 20 August 2023 21:55
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