Xiaomi Air Purifier 2
Posted: Wednesday 18 January 2017 18:55
The Xiaomi Air Purifier 2 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
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 Air Purifier: miio --discover --sync
airpurifier.js:
Example usage of the node script (new example per line):
node airpurifier.js 192.168.0.240 power true
node airpurifier.js 192.168.0.240 power false
node airpurifier.js 192.168.0.240 status
node airpurifier.js 192.168.0.240 led off
node airpurifier.js 192.168.0.240 buzzer true
node airpurifier.js 192.168.0.240 fanmode auto
node airpurifier.js 192.168.0.240 fanmode favorite
node airpurifier.js 192.168.0.240 fanspeed 16
node airpurifier.js 192.168.0.240 listfanmodes
Below is a template DEVICE lua event script for sending commands to the Air Purifier from Domoticz
8) For those who want to load the Temperature, Humidity and Aqi (pm2.5 estimate) into Domoticz sensors, below is a updatedomoticz.sh bash script:
Domoticz sensor types to be used are:
Temperature and Humidity: Temp+Hum
Aqi (pm2.5 estimate): custom sensor
startup.sh script:
I use an additional script called airpurifiercontrol.sh which runs a series of commands to power on / off the purifier with exactly the settings and fan speed I like to use, usage: ./airpurifiercontrol.sh 192.168.0.93 on 4
My own Lua device control script for the "Kitty Air Purifier" on my devices tab is:
1) Requirements: apt-get install npm sudo screen
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 Air Purifier: miio --discover --sync
airpurifier.js:
Code: Select all
#!/usr/bin/node
/* eslint-disable */
// Set path to node modules miio
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 true or false)
if ( secondarg === "power" ) {
setTimeout(exit, 7000);
console.log('Sending Power', thirdarg, 'command');
miio.device({
address: deviceip
}).then(device => {
return device.setPower(JSON.parse(thirdarg));
})}
// Status
if ( secondarg === "status" ) {
miio.device({
address: deviceip
}).then(device => {
stats = device.getProperties([ 'power', 'mode', 'aqi', 'temperature', 'humidity' ])
console.log(stats);
process.exit();
})}
// Specify favorite manual fan speed (1 to 16) eg usage: fanspeed 16
if ( secondarg === "fanspeed" ) {
setTimeout(exit, 7000);
console.log('Setting manual fan speed to:', thirdarg);
miio.device({
address: deviceip
}).then(device => {
return device.setFavoriteLevel(parseInt(thirdarg));
})}
// Set fan mode option, specify: idle, auto, silent or favorite which needs to be set for manual speed control
if ( secondarg === "fanmode" ) {
setTimeout(exit, 7000);
console.log('Telling device to use', thirdarg, 'fan speed mode');
miio.device({
address: deviceip
}).then(device => {
return device.call('set_mode', [ thirdarg ])
})}
// Control the device led (specify as bright, dim or off)
if ( secondarg === "led" ) {
setTimeout(exit, 7000);
console.log('Setting device led to:', thirdarg);
miio.device({
address: deviceip
}).then(device => {
return device.ledBrightness(thirdarg);
})}
// Switch the device buzzer on or off (specify as true or false)
if ( secondarg === "buzzer" ) {
setTimeout(exit, 7000);
console.log('Setting device buzzer to:', thirdarg);
miio.device({
address: deviceip
}).then(device => {
return device.setBuzzer(JSON.parse(thirdarg));
})}
node airpurifier.js 192.168.0.240 power true
node airpurifier.js 192.168.0.240 power false
node airpurifier.js 192.168.0.240 status
node airpurifier.js 192.168.0.240 led off
node airpurifier.js 192.168.0.240 buzzer true
node airpurifier.js 192.168.0.240 fanmode auto
node airpurifier.js 192.168.0.240 fanmode favorite
node airpurifier.js 192.168.0.240 fanspeed 16
node airpurifier.js 192.168.0.240 listfanmodes
Below is a template DEVICE lua event script for sending commands to the Air Purifier from Domoticz
Code: Select all
commandArray = {}
if devicechanged['Air Purifier'] == 'On' then
os.execute ('sudo screen -S airpurifiertemp -d -m node /root/xiaomi/airpurifier.js 192.168.0.240 power true')
end
return commandArray
Code: Select all
#!/bin/bash
while true; do
# Get the data
data=$(node airpurifier.js 192.168.0.240 status)
# Sort it
temperature=$(echo "$data" | grep "temperature" | sed -e s/[^0-9.]//g)
humidity=$(echo "$data" | grep "humidity" | sed -e s/[^0-9.%]//g)
aqi=$(echo "$data" | grep "aqi" | sed -e s/[^0-9.]//g)
# Load it into Domoticz
curl -s "http://USERNAME:[email protected]/json.htm?type=command¶m=udevice&idx=607&nvalue=0&svalue=${temperature};${humidity};0"
curl -s "http://USERNAME:[email protected]/json.htm?type=command¶m=udevice&idx=599&svalue=${aqi}"
sleep 60
done
Temperature and Humidity: Temp+Hum
Aqi (pm2.5 estimate): custom sensor
startup.sh script:
Code: Select all
# Start airpurifiermonitor
/usr/bin/screen -S airpurifiermonitor -d -m /root/xiaomi/updatedomoticz.sh
Code: Select all
#!/bin/bash
cd /root/xiaomi/airpurifier2
if [ “$2” == “on” ]; then
node airpurifier.js $1 buzzer false
node airpurifier.js $1 fanmode $3
if [ "$4" != "" ]; then
node airpurifier.js $1 fanspeed $4
fi
node airpurifier.js $1 power true
node airpurifier.js $1 led off
fi
if [ “$2” == “off” ]; then
node airpurifier.js $1 power false
fi
Code: Select all
commnadArray = {}
-- Kitty Air Purifier
if devicechanged['Kitty Air Purifier'] == 'Silent' then
os.execute ('sudo screen -S airpurifieron -d -m bash /root/xiaomi/airpurifier2/airpurifiercontrol.sh 192.168.0.93 on silent')
end
if devicechanged['Kitty Air Purifier'] == 'Max' then
os.execute ('sudo screen -S airpurifieron -d -m bash /root/xiaomi/airpurifier2/airpurifiercontrol.sh 192.168.0.93 on favorite 16')
end
if devicechanged['Kitty Air Purifier'] == 'Medium' then
os.execute ('sudo screen -S airpurifieron -d -m bash /root/xiaomi/airpurifier2/airpurifiercontrol.sh 192.168.0.93 on favorite 8')
end
if devicechanged['Kitty Air Purifier'] == 'Low' then
os.execute ('sudo screen -S airpurifieron -d -m bash /root/xiaomi/airpurifier2/airpurifiercontrol.sh 192.168.0.93 on favorite 4')
end
if devicechanged['Kitty Air Purifier'] == 'Auto' then
os.execute ('sudo screen -S airpurifieron -d -m bash /root/xiaomi/airpurifier2/airpurifiercontrol.sh 192.168.0.93 on auto')
end
if devicechanged['Kitty Air Purifier'] == 'Off' then
os.execute ('sudo screen -S airpurifieroff -d -m bash /root/xiaomi/airpurifier2/airpurifiercontrol.sh 192.168.0.93 off')
end
return commandArray