@marcel_verpaalen developing the binding, what is working fine! So it is recommended:
How to add Xiaomi Mi Air Purifier to openhab2
A long time ago I waiting a solution to control my Mi Air Purifier 2 without Xiaomi cloud. A few days ago, I just found a new npm (NodeJS) package, name is miio:
And it is the GitHub link:
Unfortunately this is not in the mihome binding yet. But I hope soon will be support too.
https://github.com/openhab/openhab2-addons/pull/1660
Until this will be support, I made a solution using exec bindings.
Created 3 file to control the purifier.
root@raspberrypi:/usr/share/openhab2/miairpurifier# cat control_power_miairpurifier.js
'use strict';
const miio = require('miio');
var args = process.argv.slice(2);
var exec = require('child_process').exec;
// Create a new device over the given address
const device = miio.createDevice({
model: 'zhimi-airpurifier-m1',
address: 'PURIFIER-IP-ADDRESS'
});
function exit() {
process.exit(-1);
}
function openhab2_rest() {
exec('curl -s -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d "status" "http://localhost:8080/rest/items/MiAirPurifierPower_Input" > /dev/null');
}
if (args == "" || args == "status" || args == "Status") {
device.getProperties([ 'power' ])
.then(console.log)
setTimeout(openhab2_rest, 7000);
setTimeout(exit, 7000);
}
if (args == "on" || args == "On" || args == "ON") {
device.setPower(true);
setTimeout(openhab2_rest, 7000);
setTimeout(exit, 7000);
}
if (args == "off" || args == "Off" || args == "OFF") {
device.setPower(false);
setTimeout(openhab2_rest, 7000);
setTimeout(exit, 7000);
}
root@raspberrypi:/usr/share/openhab2/miairpurifier#
root@raspberrypi:/usr/share/openhab2/miairpurifier# cat control_mode_miairpurifier.js
'use strict';
const miio = require('miio');
var args = process.argv.slice(2);
var exec = require('child_process').exec;
// Create a new device over the given address
const device = miio.createDevice({
model: 'zhimi-airpurifier-m1',
address: 'PURIFIER-IP-ADDRESS'
});
function exit() {
process.exit(-1);
}
function openhab2_rest() {
exec('curl -s -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d "status" "http://localhost:8080/rest/items/MiAirPurifierMode_Input" > /dev/null');
}
if (args == "" || args == "status" || args == "Status") {
device.getProperties([ 'mode' ])
.then(console.log)
setTimeout(openhab2_rest, 7000);
setTimeout(exit, 7000);
}
if (args == "auto" || args == "Auto") {
device.call('set_mode', [ 'auto' ])
.then(console.log)
setTimeout(openhab2_rest, 7000);
setTimeout(exit, 7000);
}
if (args == "sleep" || args == "Sleep") {
device.call('set_mode', [ 'silent' ])
.then(console.log)
setTimeout(openhab2_rest, 7000);
setTimeout(exit, 7000);
}
if (args == "favorite" || args == "Favorite") {
device.call('set_mode', [ 'favorite' ])
.then(console.log)
setTimeout(openhab2_rest, 7000);
setTimeout(exit, 7000);
}
root@raspberrypi:/usr/share/openhab2/miairpurifier#
root@raspberrypi:/usr/share/openhab2/miairpurifier# cat monitoring_miairpurifier.js
'use strict';
const miio = require('miio');
// Create a new device over the given address
const device = miio.createDevice({
model: 'zhimi-airpurifier-m1',
address: 'PURIFIER-IP-ADDRESS'
});
function exit() {
process.exit(-1);
}
const value = device.getProperties([ 'aqi', 'temp_dec', 'humidity' ])
.then(console.log)
setTimeout(exit, 3000);
root@raspberrypi:/usr/share/openhab2/miairpurifier#
Example rules:
rule "Aveo Ignition OFF && Mi Air Purifier ON && E-Plug ON (work/living) When MailBox button long clicked"
when
Channel "mihome:sensor_switch:158d00010a9812:button" triggered LONG_CLICK_PRESS
then
if (MiAirPurifierMode_Output.state == "{ power: false }") {
sendCommand(MiAirPurifierPower_Input, "on")
}
if (DSPW215WorkRoom_Output.state == "OFF") {
logInfo("RULES", "Work Room E-Plug ON")
sendCommand(DSPW215WorkRoom_Input, "on")
}
if (DSPW215LivingRoom_Output.state == "OFF") {
logInfo("RULES", "Living Room E-Plug ON")
sendCommand(DSPW215LivingRoom_Input, "on")
}
if (AveoStatus_Output.state == "Ignition On") {
logInfo("RULES", "Aveo Ignition OFF When MailBox button long clicked")
sendCommand(AveoStatus_Input, "Stop")
}
end
rule "Change Mi Air Purifier mode to sleep"
when
Time cron "0 0 23 ? * MON-SUN" //Everyday at 11pm
then
if (MiAirPurifierMode_Output.state == "{ mode: auto }" && MiAirPurifierPower_Output.state == "{ power: true }") {
sendCommand(MiAirPurifierMode_Input, "sleep")
}
end
rule "Change Mi Air Purifier mode to auto"
when
Time cron "0 0 7 ? * MON-SUN" //Every weekday at 7am
then
if (MiAirPurifierMode_Output.state == "{ mode: silent }" && MiAirPurifierPower_Output.state == "{ power: true }") {
sendCommand(MiAirPurifierMode_Input, "auto")
}
end
rule "Leave Home"
when
Channel "mihome:sensor_switch:158d00010d19de:button" triggered LONG_CLICK_PRESS
then
var Number hour = now.getHourOfDay
if ((hour > 7) || (hour < 9)){
if (DSPW215LivingRoom_Output.state == "ON") {
logInfo("RULES", "Living Room E-Plug OFF")
sendCommand(DSPW215LivingRoom_Input, "off")
}
if (DSPW215WorkRoom_Output.state == "ON") {
logInfo("RULES", "Work Room E-Plug OFF")
sendCommand(DSPW215WorkRoom_Input, "off")
}
}
if (AveoStatus_Output.state == "Ignition Off") {
logInfo("RULES", "Aveo Ignition ON When Leave Home")
sendCommand(AveoStatus_Input, "Start")
}
if (MiAirPurifierPower_Output.state == "{ power: true }") {
sendCommand(MiAirPurifierPower_Input, "off")
}
end
All credits to aholstenson
Note: This npm package is tested on node 5.12.0 (on 6 and 7 not worked well)