Xiaomi Humidifier

Others (MiLight, Hue, Toon etc...)
Post Reply
ben53252642
Posts: 540
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Xiaomi Humidifier

Post by ben53252642 » Wednesday 10 May 2017 11:59

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:PASSWORD@192.168.0.5/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 3746 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 3746 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 3741 times
Last edited by ben53252642 on Thursday 18 May 2017 13:16, edited 1 time in total.

deennoo
Posts: 942
Joined: Wednesday 10 December 2014 14:06
Target OS: Linux
Domoticz version: beta
Location: Bordeaux France
Contact:

Re: Xiaomi Humidifier

Post by deennoo » Wednesday 10 May 2017 21:17

Great ! Same source for all other wifi device !
Domoticz stable 3.5877 for real & Domoticz beta for test
Rfxtrxe / RFLink / Milight / Yeelight / ESP8266 / MQTT / BLE
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 » 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?

bigmak
Posts: 18
Joined: Friday 08 September 2017 17:57
Target OS: Raspberry Pi
Domoticz version: 3.8390
Location: Russia
Contact:

Re: Xiaomi Humidifier

Post by bigmak » Saturday 09 September 2017 13:27

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
Domoticz version: 3.8390
Location: Russia
Contact:

Re: Xiaomi Humidifier

Post by bigmak » Monday 11 September 2017 12:23

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
Domoticz version: 3.8390
Location: Russia
Contact:

Re: Xiaomi Humidifier

Post by bigmak » Monday 11 September 2017 15:47

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
Domoticz version: 3.8390
Location: Russia
Contact:

Re: Xiaomi Humidifier

Post by bigmak » Friday 15 September 2017 9:50

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:PASSWORD@192.168.0.5:PORT/json.htm?type=command&param=udevice&idx=617&nvalue=0&svalue=${temperature};${humidity};0"

sleep 60
done

DAVIZINHO
Posts: 206
Joined: Sunday 27 August 2017 18:00
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Spain
Contact:

Re: Xiaomi Humidifier

Post by DAVIZINHO » Saturday 30 September 2017 22:14

Great job!!!

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

DAVIZINHO
Posts: 206
Joined: Sunday 27 August 2017 18:00
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Spain
Contact:

Re: Xiaomi Humidifier

Post by DAVIZINHO » Saturday 28 April 2018 13:04

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

thanks

mgrom
Posts: 6
Joined: Saturday 07 April 2018 8:11
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Xiaomi Humidifier

Post by mgrom » 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!

DAVIZINHO
Posts: 206
Joined: Sunday 27 August 2017 18:00
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Spain
Contact:

Re: Xiaomi Humidifier

Post by DAVIZINHO » Tuesday 26 June 2018 16:29

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: 15
Joined: Friday 02 February 2018 17:21
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Xiaomi Humidifier

Post by molnaratti » Friday 28 September 2018 20:12

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

Re: Xiaomi Humidifier

Post by Stimpy68 » Sunday 02 December 2018 13:44

Any way to incorporate the water level in the tank? (depth attribute)

molnaratti
Posts: 15
Joined: Friday 02 February 2018 17:21
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Xiaomi Humidifier

Post by molnaratti » Tuesday 11 December 2018 20:24

Stimpy68 wrote:
Sunday 02 December 2018 13: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: 16
Joined: Wednesday 09 May 2018 16:37
Target OS: Linux
Domoticz version:
Location: The Hague, The Netherlands
Contact:

Re: Xiaomi Humidifier

Post by Stimpy68 » Monday 17 December 2018 19:01

molnaratti wrote:
Tuesday 11 December 2018 20:24
Stimpy68 wrote:
Sunday 02 December 2018 13: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?

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests