* Change in LED / Screen control
* Change in favorite level API and speed range
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 */
// Location of miio node lib
const miio = require('/root/node_modules/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.led(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));
})}
// List available fan speed modes
if ( secondarg === "listfanmodes" ) {
miio.device({
address: deviceip
}).then(device => {
fanmodesare = device.modes
console.log(fanmodesare);
process.exit();
})}
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 14
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:
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/airpurifier2s
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 = {}
-- Office Air Purifier
if devicechanged['Office Air Purifier'] == 'Silent' then
os.execute ('sudo screen -S airpurifieron -d -m bash /root/scripts/xiaomi/airpurifier2s/airpurifiercontrol.sh 192.168.0.92 on silent &')
end
if devicechanged['Office Air Purifier'] == 'Max' then
os.execute ('sudo screen -S airpurifieron -d -m bash /root/scripts/xiaomi/airpurifier2s/airpurifiercontrol.sh 192.168.0.92 on favorite_level 14 &')
end
if devicechanged['Office Air Purifier'] == 'Medium' then
os.execute ('sudo screen -S airpurifieron -d -m bash /root/scripts/xiaomi/airpurifier2s/airpurifiercontrol.sh 192.168.0.92 on favorite_level 7 &')
end
if devicechanged['Office Air Purifier'] == 'Low' then
os.execute ('sudo screen -S airpurifieron -d -m bash /root/scripts/xiaomi/airpurifier2s/airpurifiercontrol.sh 192.168.0.92 on favorite_level 3 &')
end
if devicechanged['Office Air Purifier'] == 'Auto' then
os.execute ('sudo screen -S airpurifieron -d -m bash /root/scripts/xiaomi/airpurifier2s/airpurifiercontrol.sh 192.168.0.92 on auto &')
end
if devicechanged['Office Air Purifier'] == 'Off' then
os.execute ('sudo screen -S airpurifieroff -d -m bash /root/scripts/xiaomi/airpurifier2s/airpurifiercontrol.sh 192.168.0.92 off &')
end
return commandArray