Xiaomi Robot Vacuum Binding

Tags: #<Tag:0x00007f51e0288b90> #<Tag:0x00007f51e0288a50> #<Tag:0x00007f51e02888c0>

(marcel) #1

I’m a big fan of Xiaomi products… Recently I bought the Xiaomi Robot vacuum.
This vacuum can be controlled via openhab. .

NB. As of 5 December 2018 the binding is no longer available on the Eclipse market place as it is merged and available as regular binding.

If you have not upgraded to 2.4 M8, the way to install the binding
To enable it now, either upgrade to the snapshot release or execute in the shell
bundle:install https://openhab.jfrog.io/openhab/online-repo-snapshot/2.4/org/openhab/binding/org.openhab.binding.miio/2.4.0-SNAPSHOT/org.openhab.binding.miio-2.4.0-SNAPSHOT.jar
Than bundle:list to find the bundle nr.
Than bundle:start nr where Mr is the number of the bundle… Probably the last/highest number

To have a working binding, a little difficult step needs to be done to retrieve the vacuum token.
You could read the Token from the Mi-Home app. This can be eased with the help of the MiToolKit
In short you have to:

  • Connect the Mi-Home App with the Robot
  • Change Wlan an Settings
  • Enable Developer Options in Android
  • Download the MiToolKit
  • Connect your Phone to your PC and start the MiToolKit
  • Read the new Token with the MiToolKit

Alternatively, e.g. if you have root access to your phone you can read the token from the mio2.db database file using a sqlite reader.

Note: latest Android MiHome no longer has the tokens in the database. Use 5.0.19 version, see link in the post earlier
You must delete the app data when downgrading to 5.0.19 from a higher version, otherwise the token will still not be visible! (… and you will be wasting hours installing different versions as I did :frowning: )

The binding is still very much in development, but I think already quite usable. Main functionality, like getting the status, switching it on & off etc are working.

Sending custom commands
To send custom commands, enable the cmd channel in paperUI (using the ‘show more’ in the channel list as it is a channel marked as advanced) and link a String item to the channel.

You can than send the command to the created string item. It will send the command to the Xiaomi device and update the channel with the response.

Commands for the vacuum are described here https://github.com/marcelrv/XiaomiRobotVacuumProtocol
Note that the binding takes care of parts of the increasing the id and the 'making a json message, e.g to send {'method': 'set_custom_mode', 'params': [40],'id': 17694} to the device, send set_custom_mode [40] to the cmd channel.

Item files
Example item files and channels for each device are now in the readme file

Planned improvements

  • Allow updates on the dnd timings
  • Show last few history items

Note, unfortunately the maps that are displayed in the app are not directly coming from the vacuum, instead they are coming from the Xiaomi cloud. Much more discovery is needed to find out exactly how to connect to the Xiaomi cloud and get this details.
note that direct cleaning map display is possible using dustcloud with hacked firmware

9 July - preserve id when connection failed
17 July Update with various fixes and improvements

  • Increase timeouts to avoid offline for devices with slow response
  • Change consumables logic to remaining instead of used time
  • Reuse port for connectivity
  • Cleanup channels, e.g. message version channel
  • Update Readme wrt iphone token
  • Simplify the channels. Single command channel

21 July Add fan control, fix bugs

  • Fix wrong total cleaning time, change format total cleaning time to minutes
  • Allowed control of vacuum fan level

31 July More logging wrt connectivity

  • Set online if at least 1 message works
  • Add the network parameters (e.g. signal strenght)
  • Add the firmware version
  • Allow for other MiIo devices to be added (manually) and send commands to.

14 Aug Renamed binding

  • Renamed the binding to miio binding to reflect the possibility to control many other Xiaomi Wifi devices. This means manual item files need to be updated to use it. (updated file in 2nd post)
  • Many small improvements to the connectivity, starting sequence etc
  • Refresh after sending each command (issue from @elias_gabrielsson)
  • The discovery sequnce has changed. Your device is discovered as a xiaomi generic device. Once the tolken is there and communication is successful, the type is changed to the vacuum and the vacuum related channels become available. For other Xiaomi devices, it will either change to the appropriate type (basic) or change to unsupported.
    11 Sept. Improve discovery speed & 2nd method
  • Added new way of discovering
  • Reworked the existing discovery with faster responses

16 Sept:

  • Changed the communication from synchronous to async
  • Command queuing, ensuring all commands are executed
  • Improved online/offline detection. Note, if your device can be pinged, but does not accept commands it assumes a token issue and goes to offline with configuration error.
  • Support for IOS encrypted tokens
  • 2nd discovery method (using mDNS), which may support discovery passing subnet boundary
  • Check for checksum errors
  • Added Xiaomi Philips Led Bulb support

5 Oct 2017:

  • Added support for several air purifiers, control favorite level on air purifiers

15 Oct 2017

  • Added channels for last cleaning details
  • Allowed to send on & off to the vacuum to start & stop vacuum to allow to used with Alexa voice control
  • configurable timeout in case of poor connection

22 Oct Release

  • Vacuum - new switch channel for on/off to allow alexa integration @Multisaft7
    note this new channel (actions#vacuum) is marked advanced, hence hidden in paperUI by default.
    Note, this will start vacuum when switched on, goes to dock on switching off. For status updates, it will also switch to on when doing spot cleaning
    Note: I removed the on/off values from the command channel again, as there is now a proper switch channel
  • vacuum - reset consumables channel. This new channel allows you to reset your consumables (e.g. the sensor usage time)
  • airpurifiers - fix temperature reading (/10) @BOFH90 @shorty707 @akhilmagotra

23 December release

  • Updated yeelight database
  • Change type from string to number for several properties of Philips bulb & yeelight @OliM
  • Add new Robot vacuum 2 support
  • Updated to 2.3 snapshot
  • Fix discovery
  • Fix several code warnings
  • Update documentation (a bit…)
  • Change log level to avoid flooding log
  • Colors for yeelight color bulb(untested, experimental)
  • removes NPE is specific cases during change of type
  • Removing unavailable properties from plug m1
  • Several cleanups to prepare for including it in the regular builds

24-03 Small update to the binding

  • Added AirPurifier 2S detected as zhimi.airpurifier.ma2 to the database
  • Added Philips Eyecare 2 & improve Yeelight 650 (thanks to @dimalo)

25-08 Small update to the binding

  • Added Xiaomi Mi Smart Pedestal Fan (zhimi.fan.sa1)

05-12 Binding is merged into the regular branche

Known issues

  • Yeelights dimming will not trigger on/off. It purely reports what the device is returning as value.

To update to the latest version uninstall the market binding, than install again.
to know the installed version, use bundle:list | grep Xiaomi in the openhab commandline

If you have issues, please include your firmware version (can be seen in mihome app) and the log. Depending on the issue this may be the debug or the trace log.

log:set debug org.openhab.binding.miio
log:display org.openhab.binding.miio

If the binding works for you let us know as well :slight_smile:

Note: for discussion around Air purifiers check topic : Xiaomi Mi Air Purifier (Xiaomi Mi IO)

Xiaomi Mi Air Purifier (Xiaomi Mi IO)
Xiaomi Mi Robot
Xiaomi Philips Wifi Bulb Binding
Mi(Xiaomi) Smart home bindings?
Xiaomi Mi Air Purifier - solution
Yeelight binding?
Xiaomi Mi Infrared Remote Control
[SOLVED] Xiaomi Philips Wifi Lamp
Xiaomi Yeelight Strip & LED Bulb
Yeelight binding?
Best way to control yeelight
[SOLVED] Xiaomi Philips Wifi Lamp
Yeelight binding?
Xiaomi Smart Ceiling LED Light
Mi(Xiaomi) Smart home bindings?
[SOLVED] Installation of miio (not mihome) binding, help
(marcel) #2

Some example screens

Sample item text

Group  gVac     "Xiaomi Robot Vacuum"      <fan>
Group  gVacStat "Status Details"           <status> (gVac)
Group  gVacCons "Consumables Usage"        <line-increase> (gVac)
Group  gVacDND  "Do Not Disturb Settings"  <moon> (gVac)
Group  gVacHist "Cleaning History"         <calendar> (gVac)
Group  gVacNetwork "Network Details"       <network> (gVac)

String actionControl  "Vacuum Control"     (gVac)     {channel="miio:generic:034F0E45:actions#control" }
Number actionFan      "Vacuum Fan"         (gVac)   {channel="miio:generic:034F0E45:actions#fan" }
String actionCommand  "Vacuum Command"          {channel="miio:generic:034F0E45:actions#commands" }

Number statusBat    "Battery Level [%1.0f%%]" <battery>   (gVac,gVacStat) {channel="miio:generic:034F0E45:status#battery" }
Number statusArea    "Cleaned Area [%1.0fm²]" <zoom>   (gVac,gVacStat) {channel="miio:generic:034F0E45:status#clean_area" }
Number statusTime    "Cleaning Time [%1.0f']" <clock>   (gVac,gVacStat) {channel="miio:generic:034F0E45:status#clean_time" }
String  statusError    "Error [%s]"  <error>  (gVac,gVacStat) {channel="miio:generic:034F0E45:status#error_code" }
Number statusFanPow    "Fan Power [%1.0f %%]"  <signal>   (gVacStat) {channel="miio:generic:034F0E45:status#fan_power" } 
Number statusClean    "In Cleaning Status [%1.0f]"   <switch>  (gVacStat) {channel="miio:generic:034F0E45:status#in_cleaning" }
Switch statusDND    "DND Activated"    (gVacStat) {channel="miio:generic:034F0E45:status#dnd_enabled" }
String statusStatus    "Status [%s]"  <status>  (gVacStat) {channel="miio:generic:034F0E45:status#state"} 

Number consumableMainT    "Main Brush [%1.0f]"    (gVacCons) {channel="miio:generic:034F0E45:consumables#main_brush_time"}
Number consumableMainP    "Main Brush [%1.0f%%]"    (gVacCons) {channel="miio:generic:034F0E45:consumables#main_brush_percent"}
Number consumableSideT    "Side Brush [%1.0f]"    (gVacCons) {channel="miio:generic:034F0E45:consumables#side_brush_time"}
Number consumableSideP    "Side Brush [%1.0f%%]"    (gVacCons) {channel="miio:generic:034F0E45:consumables#side_brush_percent"}
Number consumableFilterT    "Filter Time[%1.0f]"    (gVacCons) {channel="miio:generic:034F0E45:consumables#filter_time" }
Number consumableFilterP    "Filter Time[%1.0f%%]"    (gVacCons) {channel="miio:generic:034F0E45:consumables#filter_percent" }
Number consumableSensorT    "Sensor [%1.0f]"    (gVacCons) {channel="miio:generic:034F0E45:consumables#sensor_dirt_time"}
Number consumableSensorP    "Sensor [%1.0f%%]"    (gVacCons) {channel="miio:generic:034F0E45:consumables#sensor_dirt_percent"}

Switch dndFunction   "DND Function" <moon>   (gVacDND) {channel="miio:generic:034F0E45:dnd#dnd_function"}
String dndStart   "DND Start Time [%s]" <clock>   (gVacDND) {channel="miio:generic:034F0E45:dnd#dnd_start"}
String dndEnd   "DND End Time [%s]"   <clock-on>  (gVacDND) {channel="miio:generic:034F0E45:dnd#dnd_end"}

Number historyArea    "Total Cleaned Area [%1.0fm²]" <zoom>    (gVacHist) {channel="miio:generic:034F0E45:history#total_clean_area"}
Number historyTime    "Total Clean Time [%1.0f']"   <clock>     (gVacHist) {channel="miio:generic:034F0E45:history#total_clean_time"}
Number historyCount    "Total # Cleanings [%1.0f]"  <office>  (gVacHist) {channel="miio:generic:034F0E45:history#total_clean_count"}

String  miNetSSID    "Network SSID [%s]"  <network>  (gVac,gVacNetwork) {channel="miio:generic:034F0E45:network#ssid" }
String  miNetBSSID    "Network BSSID [%s]"  <network>  (gVac,gVacNetwork) {channel="miio:generic:034F0E45:network#bssid" }
Number  miNetRSSI    "Network RSSI [%1.0f]"  <network>  (gVac,gVacNetwork) {channel="miio:generic:034F0E45:network#rssi" }
Number  miNetLife    "Uptime [%1.0f]"  <clock>  (gVac,gVacNetwork) {channel="miio:generic:034F0E45:network#life" }

in the Sitemap

Group item=gVac {
		Switch item=actionControl mappings=[vacuum="Vacuum", pause="Pause",spot="Spot", dock="Dock"]
 		Switch item=actionFan mappings=[38="Silent", 60="Normal", 77="Power",90="Full", -1="Custom"]
		Default item=statusBat 
		Default item=statusArea 
		Default item=statusTime 
		Group  item=gVacStat
		Group  item=gVacCons
		Group  item=gVacDND
		Group  item=gVacHist
		Group  item=gVacNetwork

Matrix Theme for HABPanel
(marcel) #3

Binding is updated today

Differences between previous version

  • Increase timeouts to avoid offline for devices with slow response @sasha_jpr @BOFH90
  • Change consumables logic to remaining instead of used time
  • Reuse port for connectivity
  • Cleanup channels, e.g. message version channel
  • Update Readme wrt iphone token
  • Simplify the channels. Single command channel @kaufsim

NB, as channels changed, in you need to delete your thing and add it again to have all the channels properly available.

(Sasha) #4

Thanks for the binding and the update! Works much better now!
Great work!!

(Oliver Libutzki) #5

Why is history#total_clean_time a String? Doesn’t it have to ba a Number like status#clean_time?

Mi(Xiaomi) Smart home bindings?
(marcel) #6

@oliver it is because I prefer to have it in the hh:mm format rather than total time in minutes, or total time in like 10.5 hours, which is not possible when I define it as a decimal.

In the status#clean_time it is displayed in minutes, hence the string formatting was not needed.

(Oliver Libutzki) #7

I understand, but it might be better to user a number whenever applicable as you can evaluate it more easily in rules without parsing a string. I don’t think it’s a good idea to derive the datatype from the ui representation.

However, there seems to be a bug as the official Mi app states that my robot 145 minutes, but your binding returns “05:00”.

(Oliver Libutzki) #8

@marcel_verpaalen, currently my robot is idle, but the battery level decreases constantly. From 90% to 75% in the last 2 hours. Is it possible that the receivement of the status information (every 30 seconds) causes this decrease?

(marcel) #9

I confirm that is a issue. I’ll fix it with the next release

I would consider that unlikely… robot has a quite significant accu, and the messages send are simple, not data intensive. If so I would consider it more of a firmware bug.
Anyhow, I would encourage you to test it. The refresh interval can be changed in the configuration, it is marked as advanced, so you need to select show more to see it

(Rohnny Swennen) #10

@marcel_verpaalen quick question, any idea how to get the token with an IOS device?

(marcel) #11

I don’t have any IOS devices, but this is what I read on one of the other forums

I setup my iOS device with the Mi-Home app.
Created an unencrypted backup of the device on my laptop using itunes.
Installed iBackup Viewer from here: http://www.imactools.com/iphonebackupviewer/
Extracted this file /raw data/com.xiami.mihome/_mihome.sqlite to my computer
Open the file extracted using notepad. You will then see the list of all the device in your account with their token

(marcel) #12

Binding was updated today and loaded to the market place.

Changed to prior version:

  • Fix wrong total cleaning time, change format total cleaning time to minutes @oliver
  • Allowed control of vacuum fan level

NB, as channels changed. You need to delete your thing and add it again to have all the channels properly available.

(Oliver Libutzki) #13

Works like a charm. Thanks a lot for this amazing binding!

Do you think it’s possible to integrate the cleaning map in openHAB?

(marcel) #14

Once day maybe…:slight_smile: I would love to have it as well.
It is actually quite complicated as far as I can see. The maps are not coming from the device itself, instead from the Xiaomi cloud. I sniff the network traffic, setting up MITM proxy to workaount the ssl encryption, but even than, there is quite some crypto stuff done. So the data can’t be easy read.
There is some documentation how to logon to the cloud & sdk, but it requires you to sign up and provide copies of my passport, which I’m not willing to do.Besides if I remember well, it was all in Chinese.
Anyway, I guess in the long run we will figure out how, as that would also solve the token retrieval, but don’t count on it anytime soon.

Xiaomi Mi Air Purifier - solution
(Oliver Libutzki) #15

@marcel_verpaalen, my log file is full of these messages:

13:45:51.056 [INFO ] [me.event.ThingStatusInfoChangedEvent] - 'xiaomivacuum:vacuum:Hugo' changed from ONLINE to OFFLINE (COMMUNICATION_ERROR): No valid status response
13:46:21.096 [INFO ] [me.event.ThingStatusInfoChangedEvent] - 'xiaomivacuum:vacuum:Hugo' changed from OFFLINE (COMMUNICATION_ERROR): No valid status response to ONLINE
13:46:31.115 [INFO ] [me.event.ThingStatusInfoChangedEvent] - 'xiaomivacuum:vacuum:Hugo' changed from ONLINE to OFFLINE (COMMUNICATION_ERROR): No valid status response
13:47:01.142 [INFO ] [me.event.ThingStatusInfoChangedEvent] - 'xiaomivacuum:vacuum:Hugo' changed from OFFLINE (COMMUNICATION_ERROR): No valid status response to ONLINE
13:47:11.171 [INFO ] [me.event.ThingStatusInfoChangedEvent] - 'xiaomivacuum:vacuum:Hugo' changed from ONLINE to OFFLINE (COMMUNICATION_ERROR): No valid status response
13:48:21.189 [INFO ] [me.event.ThingStatusInfoChangedEvent] - 'xiaomivacuum:vacuum:Hugo' changed from OFFLINE (COMMUNICATION_ERROR): No valid status response to ONLINE
13:48:31.317 [INFO ] [me.event.ThingStatusInfoChangedEvent] - 'xiaomivacuum:vacuum:Hugo' changed from ONLINE to OFFLINE (COMMUNICATION_ERROR): No valid consumables response
13:49:01.328 [INFO ] [me.event.ThingStatusInfoChangedEvent] - 'xiaomivacuum:vacuum:Hugo' changed from OFFLINE (COMMUNICATION_ERROR): No valid consumables response to ONLINE
13:49:11.421 [INFO ] [me.event.ThingStatusInfoChangedEvent] - 'xiaomivacuum:vacuum:Hugo' changed from ONLINE to OFFLINE (COMMUNICATION_ERROR): No valid status response

(Carsten) #16

Maybe I am just bad…but i have no idea to install this…can you quickly help me?!

(marcel) #17

Hi @Multisaft7

First install the market place binding. See Distributing bindings through the IoT Marketplace for details.
Once that is activated you can go to the addons, and select the Xiaomi Vacuum binding (see screenie).
Once installed, read the first topic on how to get the tolken, as that is the tricky piece.

Hope this helps you getting started.

(Carsten) #18

But for this market installation i need a snapshot version of openhab? right?

(marcel) #19

I think release 2.1 already has the market place binding. Released versions before 2.1 won’t have it indeed.

(Carsten) #20

Same here…

Status: OFFLINE - COMMUNICATION_ERROR No valid Network response