Xiaomi Humidifier

Others (MiLight, Hue, Toon etc...)

Moderator: leecollings

ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Xiaomi Humidifier

Post 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 9148 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 9148 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 9143 times
Last edited by ben53252642 on Thursday 18 May 2017 13:16, edited 1 time in total.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
deennoo
Posts: 784
Joined: Wednesday 10 December 2014 13:06
Target OS: Linux
Domoticz version: beta
Location: Bordeaux France
Contact:

Re: Xiaomi Humidifier

Post by deennoo »

Great ! Same source for all other wifi device !
Domoticz stable 3.5877 for real & Domoticz beta for test
Rfxtrxe / RFLink / Milight / Yeelight / Tasmota / MQTT / BLE / Zigate
http://domo-attitude.fr
User avatar
SimyriK
Posts: 1
Joined: Friday 25 August 2017 6:26
Target OS: Linux
Domoticz version: 3.8153
Location: Krasnoyarsk
Contact:

Re: Xiaomi Humidifier

Post 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?
bigmak
Posts: 18
Joined: Friday 08 September 2017 17:57
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10693
Location: Russia
Contact:

Re: Xiaomi Humidifier

Post 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?
bigmak
Posts: 18
Joined: Friday 08 September 2017 17:57
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10693
Location: Russia
Contact:

Re: Xiaomi Humidifier

Post 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!
bigmak
Posts: 18
Joined: Friday 08 September 2017 17:57
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10693
Location: Russia
Contact:

Re: Xiaomi Humidifier

Post 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.
bigmak
Posts: 18
Joined: Friday 08 September 2017 17:57
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10693
Location: Russia
Contact:

Re: Xiaomi Humidifier

Post 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
DAVIZINHO
Posts: 234
Joined: Sunday 27 August 2017 18:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Spain
Contact:

Re: Xiaomi Humidifier

Post by DAVIZINHO »

Great job!!!

Its posible that the humidifier send a notification via domoticz when the water is empty??
DAVIZINHO
Posts: 234
Joined: Sunday 27 August 2017 18:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Spain
Contact:

Re: Xiaomi Humidifier

Post by DAVIZINHO »

hello,
someone can configure with this method the new xiaomi humidifier (smartmi with cube form)

thanks
mgrom
Posts: 5
Joined: Saturday 07 April 2018 8:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Xiaomi Humidifier

Post 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!
DAVIZINHO
Posts: 234
Joined: Sunday 27 August 2017 18:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Spain
Contact:

Re: Xiaomi Humidifier

Post 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
molnaratti
Posts: 34
Joined: Friday 02 February 2018 16:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Xiaomi Humidifier

Post 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}
Stimpy68
Posts: 40
Joined: Wednesday 09 May 2018 16:37
Target OS: Linux
Domoticz version:
Location: The Hague, The Netherlands
Contact:

Re: Xiaomi Humidifier

Post by Stimpy68 »

Any way to incorporate the water level in the tank? (depth attribute)
molnaratti
Posts: 34
Joined: Friday 02 February 2018 16:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Xiaomi Humidifier

Post 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
Stimpy68
Posts: 40
Joined: Wednesday 09 May 2018 16:37
Target OS: Linux
Domoticz version:
Location: The Hague, The Netherlands
Contact:

Re: Xiaomi Humidifier

Post 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?
ElPhilou
Posts: 8
Joined: Thursday 25 January 2018 17:27
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Xiaomi Humidifier

Post 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 :|
ElPhilou
Posts: 8
Joined: Thursday 25 January 2018 17:27
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Xiaomi Humidifier

Post 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-)
Grinders
Posts: 7
Joined: Tuesday 28 August 2018 10:25
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Xiaomi Humidifier

Post 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?
ElPhilou
Posts: 8
Joined: Thursday 25 January 2018 17:27
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Xiaomi Humidifier

Post by ElPhilou »

Click on the device and you can see remaining water :D
Grinders
Posts: 7
Joined: Tuesday 28 August 2018 10:25
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Xiaomi Humidifier

Post 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.
Attachments
IMG_7952.pdf
(286.88 KiB) Downloaded 120 times
IMG_7951.pdf
(232.59 KiB) Downloaded 77 times
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest