Page 1 of 2

Xiaomi Humidifier

Posted: Wednesday 10 May 2017 11:59
by ben53252642
The Xiaomi Humidifier now works with Domoticz for Linux (Ubuntu, Debian Raspberry Pi). Below is the setup, requires moderate Linux knowledge.

1) Requirements: apt-get install npm sudo screen jq

2) Install miIO Device Library: npm install --save miio

3) Install miio command line utility: npm install -g miio

4) Run this command to discover and sync the key from your Humidifier: miio --discover --sync

humidifier.js

Code: Select all

#!/usr/bin/node
/* eslint-disable */

// Location of miio node lib
const miio = require('miio');

// No need to change any lines in this section
var deviceip = process.argv[2];
var secondarg = process.argv[3];
var thirdarg = process.argv[4];
function exit() {
process.exit(-1);
}

// Power On (on / off specified as on or off)
if ( secondarg === "power" ) {
        setTimeout(exit, 7000);
        miio.device({
        address: deviceip
}).then(device => {
                return device.call('set_power', [thirdarg])
                .then(console.log)
                .catch(console.error);
})}

// Status
if ( secondarg === "status" ) {
        setTimeout(exit, 7000);
        miio.device({
        address: deviceip
}).then(device => {
                return device.call('get_prop', ["humidity","temp_dec","power","mode","led_b","buzzer","child_lock","limit_hum","trans_level"])
                .then(console.log)
                .catch(console.error);
})}

// Status temperature and humidity only
if ( secondarg === "status2" ) {
        setTimeout(exit, 7000);
        miio.device({
        address: deviceip
}).then(device => {
                return device.call('get_prop', ["temp_dec","humidity"])
                .then(console.log)
                .catch(console.error);
})}

// Set mode (silent, medium or high)
if ( secondarg === "mode" ) {
        setTimeout(exit, 7000);
        miio.device({
        address: deviceip
}).then(device => {
                return device.call('set_mode', [thirdarg])
                .then(console.log)
                .catch(console.error);
})}

// Set buzzer (on or off)
if ( secondarg === "buzzer" ) {
        setTimeout(exit, 7000);
        miio.device({
        address: deviceip
}).then(device => {
                return device.call('set_buzzer', [thirdarg])
                .then(console.log)
                .catch(console.error);
})}

// led
if ( secondarg === "led" ) {
        setTimeout(exit, 7000);
        miio.device({
        address: deviceip
}).then(device => {
                return device.call('set_led_b', [JSON.parse(thirdarg)])
                .then(console.log)
                .catch(console.error);
})}

// Humidity limit percent (specified as 40, 50, 60, 70 or 80)
if ( secondarg === "humiditylimit" ) {
        setTimeout(exit, 7000);
        miio.device({
        address: deviceip
}).then(device => {
                return device.call('set_limit_hum', [JSON.parse(thirdarg)])
                .then(console.log)
                .catch(console.error);
})}
Example use:
node humidifier.js 192.168.0.95 power off
node humidifier.js 192.168.0.95 status
node humidifier.js 192.168.0.95 status2
node humidifier.js 192.168.0.95 mode medium
node humidifier.js 192.168.0.95 buzzer off
node humidifier.js 192.168.0.95 led 2
node humidifier.js 192.168.0.95 humiditylimit 50

domoticzupdate.sh

Code: Select all

#!/bin/bash
cd /root/xiaomi/humidifier
while true; do

# Main Bedroom Humidifier
# Get the data
data=$(node humidifier.js 192.168.0.95 status2)
# Sort it
temperature=$(echo "$data" | jq '.[0]' | sed 's/.\{2\}/&./g')
humidity=$(echo "$data" | jq '.[1]')

# Load it into Domoticz
curl -s "http://USERNAME:[email protected]/json.htm?type=command&param=udevice&idx=617&nvalue=0&svalue=${temperature};${humidity};0"

sleep 60
done
humidifiercontrol.sh

Code: Select all

#!/bin/bash
cd /root/xiaomi/humidifier

if [ “$2” == “on” ]; then
node humidifier.js $1 buzzer off
node humidifier.js $1 mode $3
node humidifier.js $1 power on
node humidifier.js $1 led 2
fi

if [ “$2” == “off” ]; then
node humidifier.js $1 power off
fi
startup.sh

Code: Select all

# Start humidifiermonitor
cd /root/xiaomi/humidifier
/usr/bin/screen -S humidifiermonitor -d -m /root/xiaomi/humidifier/domoticzupdate.sh
Lua Domoticz event control script:

Code: Select all

commnadArray = {}
-- Main B Humidifier
if devicechanged['Main B Humidifier'] == 'Silent' then
    os.execute ('sudo screen -S humidifiertemp -d -m bash /root/xiaomi/humidifier/humidifiercontrol.sh 192.168.0.95 on silent')
end
if devicechanged['Main B Humidifier'] == 'Medium' then
    os.execute ('sudo screen -S humidifiertemp -d -m bash /root/xiaomi/humidifier/humidifiercontrol.sh 192.168.0.95 on medium')
end
if devicechanged['Main B Humidifier'] == 'High' then
    os.execute ('sudo screen -S humidifiertemp -d -m bash /root/xiaomi/humidifier/humidifiercontrol.sh 192.168.0.95 on high')
end
if devicechanged['Main B Humidifier'] == 'Off' then
    os.execute ('sudo screen -S humidifiertemp -d -m bash /root/xiaomi/humidifier/humidifiercontrol.sh 192.168.0.95 off')
end
return commandArray
Screenshots:
Screen Shot 2017-05-10 at 7.57.53 pm.png
Screen Shot 2017-05-10 at 7.57.53 pm.png (92.43 KiB) Viewed 9451 times
Screen Shot 2017-05-10 at 7.59.07 pm.png
Screen Shot 2017-05-10 at 7.59.07 pm.png (92.16 KiB) Viewed 9451 times
Screen Shot 2017-05-10 at 8.03.13 pm.png
Screen Shot 2017-05-10 at 8.03.13 pm.png (368.6 KiB) Viewed 9446 times

Re: Xiaomi Humidifier

Posted: Wednesday 10 May 2017 21:17
by deennoo
Great ! Same source for all other wifi device !

Re: Xiaomi Humidifier

Posted: Friday 25 August 2017 6:29
by SimyriK
Hi. A lot of messages like "Error: EventSystem: Lua script /home/user/domoticz/scripts/lua/script_device_BedroomHumidifier.lua did not return a commandArray". Does anyone know how to solve?

Re: Xiaomi Humidifier

Posted: Saturday 09 September 2017 13:27
by bigmak
SimyriK wrote: Friday 25 August 2017 6:29 Hi. A lot of messages like "Error: EventSystem: Lua script /home/user/domoticz/scripts/lua/script_device_BedroomHumidifier.lua did not return a commandArray". Does anyone know how to solve?
I have the same error.
Did you solve this problem?

Re: Xiaomi Humidifier

Posted: Monday 11 September 2017 12:23
by bigmak
Maybe it will help to someone like me.

I did step-by-step p.1-4.

Create humidifier.js with code shown above by path "/home/pi/Xiaomi"

After that you can try make commands and check that your Xiaomi Humidifier working from command line:
node humidifier.js 192.168.0.95 power off
node humidifier.js 192.168.0.95 status
node humidifier.js 192.168.0.95 status2
node humidifier.js 192.168.0.95 mode medium

Then you need to know full path to "node".
Print in command line

Code: Select all

pi@raspberrypi ~ $ which node
/home/pi/.nvm/versions/node/v6.0.0/bin/node
Save this path to notepad.

Then I created 4 files in /home/pi/Xiaomi:

humidifier_power_off.sh

Code: Select all

#!/bin/sh

# Humidifier
# Set Power off
sudo /home/pi/.nvm/versions/node/v6.0.0/bin/node /home/pi/Xiaomi/humidifier.js 192.168.0.95 power off /dev/null

exit 0
humidifier_level_1.sh

Code: Select all

#!/bin/sh
# Humidifier
# Set Silent mode
sudo /home/pi/.nvm/versions/node/v6.0.0/bin/node /home/pi/Xiaomi/humidifier.js 192.168.0.95 mode silent /dev/null
exit 0
humidifier_level_2.sh

Code: Select all

#!/bin/sh
# Humidifier
# Set Medium mode
sudo /home/pi/.nvm/versions/node/v6.0.0/bin/node /home/pi/Xiaomi/humidifier.js 192.168.0.95 mode medium /dev/null
exit 0
humidifier_level_3.sh

Code: Select all

#!/bin/sh
# Humidifier
# Set High mode
sudo /home/pi/.nvm/versions/node/v6.0.0/bin/node /home/pi/Xiaomi/humidifier.js 192.168.0.95 mode high /dev/null
exit 0
Make "chmod +x" for each file, for example:

Code: Select all

$ chmod +x humidifier_level_3.sh
Let's go to Domoticz 3.8390, Menu->Setup->Hardware
Add Dummy virtual switch with name Humidifier
On this page press "Create virtual Sensors" and choose "Selector Switch" type.
Go to "Switches" and find your "Humidifier".
Press "Edit" button and rename Selector Levels - Off, Silent, Medium, High (or whatever you want).
In selector actions print path to your scripts with tripple slash, like this:

Off - script:///home/pi/Xiaomi/humidifier_power_off.sh
Silent - script:///home/pi/Xiaomi/humidifier_level_1.sh
Medium - script:///home/pi/Xiaomi/humidifier_level_2.sh
High - script:///home/pi/Xiaomi/humidifier_level_3.sh

That's it.

PS. It looks not very attractive, but it works! I am not programmer and if someone find better way - please write it!

Re: Xiaomi Humidifier

Posted: Monday 11 September 2017 15:47
by bigmak
One more update!

I checked code and now it works from Events (LUA).

1. My variant of humidifiercontrol.sh
I placed it on /home/pi/domoticz/scripts/humidifiercontrol.sh

Code: Select all

#!/bin/bash

if [ “$2” == “on” ]; then
/home/pi/.nvm/versions/node/v6.0.0/bin/node /home/pi/Xiaomi/humidifier.js $1 mode $3
/home/pi/.nvm/versions/node/v6.0.0/bin/node /home/pi/Xiaomi/humidifier.js $1 power on
fi

if [ “$2” == “off” ]; then
/home/pi/.nvm/versions/node/v6.0.0/bin/node /home/pi/Xiaomi/humidifier.js $1 power off
fi

2. Domoticz->Menu->More Options->Events
Insert code:

Code: Select all

commandArray = {}

-- Humidifier - this is the name of your dummy selector

if devicechanged['Humidifier'] == 'Silent' then
    os.execute ('sudo screen -S humidifiertemp -d -m /bin/bash /home/pi/domoticz/scripts/humidifiercontrol.sh 192.168.0.95 on silent')
end

if devicechanged['Humidifier'] == 'Medium' then
    os.execute ('sudo screen -S humidifiertemp -d -m /bin/bash /home/pi/domoticz/scripts/humidifiercontrol.sh 192.168.0.95 on medium')
end

if devicechanged['Humidifier'] == 'High' then
    os.execute ('sudo screen -S humidifiertemp -d -m /bin/bash /home/pi/domoticz/scripts/humidifiercontrol.sh 192.168.0.95 on high')
end

if devicechanged['Humidifier'] == 'Off' then
    os.execute ('sudo screen -S humidifiertemp -d -m /bin/bash /home/pi/domoticz/scripts/humidifiercontrol.sh 192.168.0.95 off /dev/null')
end

return commandArray
3. Don't forget change "All" to "Device" on the right upper corner of internal editor.

4. Name this event and save.

That's it! Now it works through LUA Event.

Re: Xiaomi Humidifier

Posted: Friday 15 September 2017 9:50
by bigmak
Corrective in domoticzupdate.sh.
Need to add port in command.

Code: Select all

#!/bin/bash
cd /root/xiaomi/humidifier
while true; do

# Main Bedroom Humidifier
# Get the data
data=$(node humidifier.js 192.168.0.95 status2)
# Sort it
temperature=$(echo "$data" | jq '.[0]' | sed 's/.\{2\}/&./g')
humidity=$(echo "$data" | jq '.[1]')

# Load it into Domoticz
curl -s "http://USERNAME:[email protected]:PORT/json.htm?type=command&param=udevice&idx=617&nvalue=0&svalue=${temperature};${humidity};0"

sleep 60
done

Re: Xiaomi Humidifier

Posted: Saturday 30 September 2017 22:14
by DAVIZINHO
Great job!!!

Its posible that the humidifier send a notification via domoticz when the water is empty??

Re: Xiaomi Humidifier

Posted: Saturday 28 April 2018 13:04
by DAVIZINHO
hello,
someone can configure with this method the new xiaomi humidifier (smartmi with cube form)

thanks

Re: Xiaomi Humidifier

Posted: Tuesday 12 June 2018 9:03
by mgrom
I've modified python plugin for Xiaomi Air Purifier, and created plugin for Xiaomi Air Humidifier:
https://github.com/mgrom/domoticz-AirHumidifier

Have fun!

Re: Xiaomi Humidifier

Posted: Tuesday 26 June 2018 16:29
by DAVIZINHO
hello
This plugins are compatible with the 2 models of xiaomi humidifier (smartmi)?? or only with the first (round form, not cube)

thanks

Re: Xiaomi Humidifier

Posted: Friday 28 September 2018 20:12
by molnaratti
mgrom wrote: Tuesday 12 June 2018 9:03 I've modified python plugin for Xiaomi Air Purifier, and created plugin for Xiaomi Air Humidifier:
https://github.com/mgrom/domoticz-AirHumidifier

Have fun!
What could be the problem?

Code: Select all

pi@raspberrypi:~/domoticz/plugins/domoticz-AirHumidifier $ ./MyHumidify.py 192.168.1.191 581386dbf6457cdf84aef497e6a167fd --debug
Namespace(IPaddress='192.168.1.191', debug=True, mode=None, power=None, targetLevel=None, token='581386dbf6457cdf84aef497e6a167fd')
Traceback (most recent call last):
  File "./MyHumidify.py", line 59, in <module>
    print(MyHumidifier.status())
  File "/usr/local/lib/python3.5/dist-packages/miio/airhumidifier.py", line 254, in status
    properties
  File "/usr/local/lib/python3.5/dist-packages/miio/device.py", line 270, in send
    raise DeviceError(m.data.value["error"])
miio.exceptions.DeviceError: {'message': 'UART timeout', 'code': -9999}

Re: Xiaomi Humidifier

Posted: Sunday 02 December 2018 12:44
by Stimpy68
Any way to incorporate the water level in the tank? (depth attribute)

Re: Xiaomi Humidifier

Posted: Tuesday 11 December 2018 19:24
by molnaratti
Stimpy68 wrote: Sunday 02 December 2018 12:44 Any way to incorporate the water level in the tank? (depth attribute)
/usr/local/bin/miiocli airhumidifierca1 --ip --token status
Power: on
Mode: OperationMode.Auto
Temperature: 20.2 °C
Humidity: 47 %
LED brightness: LedBrightness.Dim
Buzzer: False
Child lock: False
Target humidity: 70 %
Trans level: None
Speed: 824
Depth: 77
Dry: False
Use time: -2147483384
Hardware version: 0001
Button pressed: None

Re: Xiaomi Humidifier

Posted: Monday 17 December 2018 18:01
by Stimpy68
molnaratti wrote: Tuesday 11 December 2018 19:24
Stimpy68 wrote: Sunday 02 December 2018 12:44 Any way to incorporate the water level in the tank? (depth attribute)
/usr/local/bin/miiocli airhumidifierca1 --ip --token status
Power: on
Mode: OperationMode.Auto
Temperature: 20.2 °C
Humidity: 47 %
LED brightness: LedBrightness.Dim
Buzzer: False
Child lock: False
Target humidity: 70 %
Trans level: None
Speed: 824
Depth: 77
Dry: False
Use time: -2147483384
Hardware version: 0001
Button pressed: None
Thanks, but how do I get that visible in Domoticz?

Re: Xiaomi Humidifier

Posted: Saturday 16 February 2019 13:12
by ElPhilou
Stimpy68 wrote: Monday 17 December 2018 18:01
molnaratti wrote: Tuesday 11 December 2018 19:24
Stimpy68 wrote: Sunday 02 December 2018 12:44 Any way to incorporate the water level in the tank? (depth attribute)
/usr/local/bin/miiocli airhumidifierca1 --ip --token status
Power: on
Mode: OperationMode.Auto
Temperature: 20.2 °C
Humidity: 47 %
LED brightness: LedBrightness.Dim
Buzzer: False
Child lock: False
Target humidity: 70 %
Trans level: None
Speed: 824
Depth: 77
Dry: False
Use time: -2147483384
Hardware version: 0001
Button pressed: None
Thanks, but how do I get that visible in Domoticz?
Sure, the water level is important, notification, etc...

You can add more informations in humidifier.js in status section, for exmple:

Code: Select all

// Status
if ( secondarg === "status" ) {
        setTimeout(exit, 7000);
        miio.device({
        address: deviceip
}).then(device => {
                return device.call('get_prop', ["power", "mode", "temp_dec", "humidity", "buzzer", "led_b", "limit_hum", "trans_level", "depth", "dry" ])
                .then(console.log)
                .catch(console.error);
})}
All I found : 'power', 'mode', 'temp_dec', 'humidity', 'buzzer', 'led_b', 'child_lock', 'limit_hum', 'trans_level', 'speed', 'depth', 'dry', 'use_time', 'button_pressed', 'hw_version'

If you find more, you are welcome :mrgreen:

Some don't work, like trans_level, always NUL and I don't know that is it... :roll:

After you can try to extract variables, like in, domoticzupdate.sh ;)

Something is strange, because with this script, for example now , depth give me 87, and MiHome give me 72% of Water Level :|

Re: Xiaomi Humidifier

Posted: Sunday 17 February 2019 17:40
by ElPhilou
ElPhilou wrote: Saturday 16 February 2019 13:12
Something is strange, because with this script, for example now , depth give me 87, and MiHome give me 72% of Water Level :|
Found :) in fact the reference level is based on 120, so to have in percent you must divide by 1.2

Example : 87/1.2 = 72.5
so rnd = 72 % 8-)

Re: Xiaomi Humidifier

Posted: Thursday 21 March 2019 15:06
by Grinders
ElPhilou wrote: Saturday 16 February 2019 13:12 Something is strange, because with this script, for example now , depth give me 87, and MiHome give me 72% of Water Level :|
I apologize for the stupid question, but I have no where information about the water level. Where can I see this in MIHOME?

Re: Xiaomi Humidifier

Posted: Thursday 21 March 2019 16:35
by ElPhilou
Click on the device and you can see remaining water :D

Re: Xiaomi Humidifier

Posted: Monday 25 March 2019 10:53
by Grinders
ElPhilou wrote: Thursday 21 March 2019 16:35 Click on the device and you can see remaining water :D
Unfortunately, there is no data on how full the humidifier is.