Adding the Xiaomi Mii Robot Vacuum into Domoticz

Xiaomi Robot Vacuum in Domoticz

In order to control our Xiaomi Mii Robot Vacuum, the app of Xiaomi (iOS and Android) does a good job. But when you have a Domoticz setup present at home, it would be a shame not to integrate the vacuum into this system. In this post I will explain how to set up the Xiaomi Mii Robot Vacuum into Domoticz using Amazon Alexa and leverage the Domoticz platform to control the Vacuum in a smarter way.

What can you expect?

A bunch of new devices in Domoticz to control the vacuum manually, blocky or scripting. As you can see in the screenshot below there are several new devices after adding.

Robot Vacuum in Domotica as devices

A few examples of what you can do with the Xiaomi Mii Robot Vacuum after it is added in Domoticz:

  • Start the cleaning process with your voice (Alexa or Google Home)
  • Start cleaning when Domoticz knows you are away from home
  • Start cleaning if you have dust sensors that report that cleaning is needed
  • Stop cleaning if TV turns on

 Prerequisites

  • Your Robot Vacuum needs to have a fixed IP address (remember this IP). This can be done in your router.
  • Ensure you can use the Robot Vacuum in your app of xiaomi (Mi Home).
  • Ensure you have Java on your computer. Check this in your overview of installed software or download it at http://java.com.
  • Have the latest Domoticz version running. Check the latest version on the Domoticz site.

In order to configure the Robot Vacuum in Domoticz you need to know the IP address and its token. The token is needed for the communication between the Robot and an external app.

Obtain the token from the Vacuum Robot

There are different methods to get the token. In this tutorial, I use an Android tablet and Windows 10 computer to do this.

The most simple method is to download the old version of MiHome on an Android device that is not decoded.
My Samsung Android phone is decoded and making a backup is only possible when using a password. The tool we use to get the token out of the backup doesn’t like a password, so use a device that is not decoded.

Starting from version 5.0.31,  the MiHome app no longer stores tokens locally but on the Xiaomi servers so we need an older version of the app. So uninstall the newer version (or use another Android device) and install the older version of the app and login with your Xiaomi account to get access to all of your devices (including the Robot Vacuum) again.

  • Download version 5.0.0 here and install it on your android.
  • Enable developer mode and USB debugging on the Android device and connect it to the computer with a USB cable.
  • Download and extract the MiToolKit.zip on the computer.
  • Open de MiToolKit (it opens in German language but you can change it to English)
  • Click “Extract Token” in the toolkit.
  • On your Android device, you will see a screen that asks you to confirm a backup. Confirm the, but Don’t use a password. (as mentioned before that was not possible on my Samsung so I needed to switch to an old tablet for that)
  • Once confirmed you can check the toolkit on the computer to see your token
  • That’s all!! More methods for obtaining a token on Android and IOS are available on GitHub.

Setup plugin in Domoticz

There is a Python plugin manager under development (Linux only) that enables a simple and easy to use plugin manager in Domoticz. With this PP-manager users can install plugins from there without the need of SSH, GitHub etc. But because it’s still in development we are focusing on the manual procedure.

SSH into your Domoticz and fire:

1
sudo apt-get install python3 python3-dev python3-pip libffi-dev libssl-dev
1
sudo pip3 install -U setuptools
.
1
sudo pip3 install -U virtualenv
.
1
2
3
4
5
6
7
cd domoticz/plugins
git clone https://github.com/mrin/domoticz-mirobot-plugin.git xiaomi-mirobot
cd xiaomi-mirobot
virtualenv -p python3 .env
source .env/bin/activate

pip3 install -r pip_req.txt
  1. Open and edit miio_server.sh by vi/nano:
    1
    nano miio_server.sh
    1
    # 1. Check and update absolute path to miio_server.py

    # 2. Update IP and TOKEN for robot# 3. Optional. Change MIIO server host-port bindings if need it# file miio_server.sh
    DAEMON_USER=root
    DAEMON=/home/pi/domoticz/plugins/xiaomi-mirobot/miio_server.py
    DAEMON_ARGS=”192.168.1.12 476e6b70343055483230644c53707a12″
    DAEMON_ARGS=”$DAEMON_ARGS –host 127.0.0.1 –port 22222″

  2. Check path to python3 
    1
    which python3
    . By default is 
    1
    /usr/bin/python3
    . If your path different than default, update miio_server.py first line with your path.
    1
    #!/usr/bin/python3
  3. For run as system service:
    1
    sudo chmod +x miio_server.py
    1
    sudo chmod +x miio_server.sh
    1
    # check your path here:
    1
    sudo ln -s /home/pi/domoticz/plugins/xiaomi-mirobot/miio_server.sh /etc/init.d/miio_server
    1
    # add to startup
    1
    sudo update-rc.d miio_server defaults
    1
    sudo systemctl daemon-reload
    1
    # if you want to remove from startup
    1
    sudo update-rc.d -f miio_server remove
  4. Run server and test script:
    1
    sudo service miio_server start
    1
    sudo chmod +x test.py
    1
    sudo ./test.py
  5. 1
    2
    # to stop miio server service
    sudo service miio_server stop

    Also you can run MIIO Server manually and look log output:

    1
    sudo ./miio_server.py 192.168.1.12 476e6b70343055483230644c53707a12 --host 127.0.0.1 --port 22222

    # then you can run test
    sudo ./test.py

    If server and test is ok, time to restart the Domoticz:

    1
    sudo service domoticz.sh restart

The steps above are created by Mrin, and we thank him for this great work! Also the plugin is made by the user Mrin (Igor Sankovich). More info can be found on GitHub.

Once Domoticz has started you go to hardware > and you can add and enable the plugin.

Addmin Xiaomi robot as hardware
Add Xiaomi robot as hardware

After enabling the hardware you see all devices in the settings>devices and you are ready to start scripting and using them.

Control cleaning with your voice

In this example, I show you how to use the vacuum with your voice. Check out the video to hear me commanding my vacuum.

Amazon Alexa with Controlicz

When you have Google Home or Alexa you must use the plugin Controlicz.  Controlicz let you use Domoticz in your voice system. To enable Controlicz you have to use https! How to install and configure I will explain later in a new post, but you can check out Domoticz forum now for more info.

If you are able to use Domoticz in Alexa there are a few minor things:

  • Alexa doesn’t recognize selector switches, and ‘xiaomi_vacuum’ is not a nice name to speak out. So you have to create a dummy on/off switch with a nice simple name: I named it ‘Dusty’.
    Put the switch in a room plan, so you can use it in Alexa.
  • When you have the new switch in Alexa, it doesn’t do anything. That’s why we are using a dzventz script. When we switch Dusty on, the script will change the real selector switch for you.

return {
on = {
devices = {
‘Dusty’
}
},
execute = function(domoticz, device)

local vacuum = domoticz.devices(‘Dusty – Control’)
domoticz.log(‘Device ‘ .. device.name .. ‘ was changed’, domoticz.LOG_INFO)

if (device.state == ‘On’) then
domoticz.log(‘Hey! I am on!’)
vacuum.switchSelector(10) — this is clean
else
domoticz.log(‘Hey! I am off!’)
vacuum.switchSelector(20) — this is home
end

end
}

With this trick you are able to control a selector in Alexa. Maybe you want more virtual switches for thinks like: finding, speed or other, so be creative and edit my scripts.

xiaomi vacuum switches in domoticz
Xiaomi vacuum switches in Domoticz. I think it looks different on your Domoticz, but that’s because of the theme I use: Aurora.