Skip to content

Instantly share code, notes, and snippets.

@ophilips
Last active June 1, 2023 21:34
Show Gist options
  • Save ophilips/116a2cda6d4e10825826af9640a92199 to your computer and use it in GitHub Desktop.
Save ophilips/116a2cda6d4e10825826af9640a92199 to your computer and use it in GitHub Desktop.
Xiaomi Vacuum as "template fan" for Home Assistant/HomeKit. The fan slider (0-100) in HomeKit will be translated to the the corresponding vacuum presets or speeds (off, Silent, Standard, Medium, Turbo).
# Xiaomi HomeKit
fan:
- platform: template
fans:
xiaomi_fan:
friendly_name: "Xiaomi Vacuum"
value_template: "{%if states('vacuum.xiaomi_vacuum_cleaner') == 'cleaning' %}on{%elif states('vacuum.xiaomi_vacuum_cleaner') == 'paused' %}on{%else %}off{% endif %}"
speed_template: "{{ state_attr('vacuum.xiaomi_vacuum_cleaner', 'fan_speed') }}"
turn_on:
service: vacuum.start
entity_id: vacuum.xiaomi_vacuum_cleaner
# turn_on:
# service: xiaomi_miio.vacuum_clean_zone
# data_template:
# entity_id: vacuum.xiaomi_vacuum_cleaner
# repeats: 1
# zone: [[18724,22506,25440,24620],[23758,20813,24906,22506]]
turn_off:
service: vacuum.return_to_base
entity_id: vacuum.xiaomi_vacuum_cleaner
set_speed:
service: vacuum.set_fan_speed
data_template:
fan_speed: "{{ speed }}"
entity_id: vacuum.xiaomi_vacuum_cleaner
speeds:
- 'off'
- 'Silent'
- 'Standard'
- 'Medium'
- 'Turbo'
# - 'Gentle'
sensor:
- platform: template
sensors:
xiaomi_fan:
unit_of_measurement: '%'
value_template: "{{ state_attr('vacuum.xiaomi_vacuum_cleaner', 'battery_level') }}"
homekit:
entity_config:
fan.xiaomi_fan:
linked_battery_sensor: sensor.xiaomi_fan
@lasry1
Copy link

lasry1 commented Aug 2, 2019

i don't undestand this, where do i place the "xiaomi_fan_configuration.yaml" file?

@ophilips
Copy link
Author

ophilips commented Aug 2, 2019

Hey @lasry1, you have to copy the code above to your home assistant configuration.yaml file. The vacuum itself should already be configured in home assistant.

@ophilips
Copy link
Author

ophilips commented Aug 2, 2019

@slwl, I think the vacuum integration is already well documented at the home assistant website. I had that code in an earlier version but decided to leave it out for better readability.

@lasry1
Copy link

lasry1 commented Aug 2, 2019

thanks a lot! i did it however can't see the presets on homekit but at least i can control it! thanks again

@ophilips
Copy link
Author

ophilips commented Aug 2, 2019

The fan slider (0-100) in HomeKit will be translated to the the corresponding vacuum presets or speeds (off, Quiet, Balanced, Turbo, Max). HomeKit doesn’t support steps or drop down menus. If you are interested how the values are calculated check the fan section here: https://www.home-assistant.io/components/homekit/#supported-components

@lvdfrnt
Copy link

lvdfrnt commented Nov 21, 2019

How can I locate the vacuum? or show battery level? With your code, all the other stuff worked but wanted to see if it was possible to show battery level and locate.

@ophilips
Copy link
Author

Hey @lvdfrnt, Thanks for your suggestions. It could be possible to create a dummy sensor and connect it using linked_battery_sensor within the homekit: configuration. I will check that later when I will be back at home.

@ophilips
Copy link
Author

@lvdfrnt, I got the battery sensor running and updated the config, but you have to reset the device in your homekit: section. To reset your device you have to insert

filter:
  exclude_entities:
    - fan.xiaomi_fan

in your homekit: config and restart home assistant so that it gets shown as a new device. After that you have to delete the filter.

You'll then find the battery status by clicking on the device settings symbol:
IMG_3928

@lvdfrnt
Copy link

lvdfrnt commented Nov 24, 2019

@lvdfrnt, I got the battery sensor running and updated the config, but you have to reset the device in your homekit: section. To reset your device you have to insert

filter:
  exclude_entities:
    - fan.xiaomi_fan

in your homekit: config and restart home assistant so that it gets shown as a new device. After that you have to delete the filter.

You'll then find the battery status by clicking on the device settings symbol:
IMG_3928

Finally got around to doing it and works flawlessly! thank you so much man. The instructions definitely helped!

@ThomasBoettner
Copy link

Hello, is it possible to configurate more than one cleaner?

@ophilips
Copy link
Author

Hey @ThomasBoettner, You just have to use different names for each vacuum, fan and sensor like xiaomi_fan_01 instead of xiaomi_fan. You could also use one cleaner and configure zones so that every room has its own virtual vacuum.

@cobch
Copy link

cobch commented Feb 4, 2020

@ophilips Great work! Do you know if I can integrate Zones into the templates? I would like to get a vacuum for differents zones of my flats... I use homebridge for that but I will rather use Home assitant instead directly which will help to get a fre raspberry Pi to use for something else (-:

@ophilips
Copy link
Author

ophilips commented Feb 4, 2020

Hey @cobch, thanks for your great feedback; I will check how to configure zones in the next days. In theory it should work but it may be necessary to add "dummy" vacuums.

@cobch
Copy link

cobch commented Feb 4, 2020

@ophilips I will be happy to test it. Keep in touch!

@cobch
Copy link

cobch commented Feb 19, 2020

@ophilips Any news about the test with zones?

@ophilips
Copy link
Author

Hey @cobch, I had to generate all the coordinates for my rooms in the last days but zoned cleaning should work now by changing the code to:

        turn_on:
          service: xiaomi_miio.vacuum_clean_zone
          data_template:
            entity_id: vacuum.xiaomi_vacuum_cleaner
            repeats: 1
            zone: [[18724,22506,25440,24620],[23758,20813,24906,22506]]

You can add as many zones into the array as you like. If you want to have one vacuum for each room you just have to copy the whole config within fans: like xiaomi_fan_01:,xiaomi_fan_02: etc. but with different names. One thing you should also change are the speed presets as the names changed in one of the last home assistant updates:

        speeds:
          - 'off'
          - 'Silent'
          - 'Standard'
          - 'Medium'
          - 'Turbo'

I hope that you can get it running with this "preview".

@cobch
Copy link

cobch commented Feb 26, 2020

Hey @ophilips,

I have tested the zones configuration and the config above is working great. To make the mopping function of the S5 working I had to change the fan speed according to the following:
- 'Gentle'
- 'Silent'
- 'Standard'
- 'Medium'
- 'Turbo'

What I do not managed to make working is:

  • the "battery status" is always set to 0% even after resetting the device as you mentioned above.
  • Charging is always showing "No"

Any tips that will help me?

Further ideas: A play/pause function and a sensor to know whether the vacuum is docked or not. See picture below:
Screenshot 2020-02-26 at 11 56 18

@ophilips
Copy link
Author

@cobch, About the battery status: Do you get a reading for your battery sensor in home assistant if you add the new sensor entity?

I just checked the home assistant documentation about your speed settings. You should always add an off value to your list. Check this link and search for fan: https://www.home-assistant.io/integrations/homekit/

So you should change your list to the following when using the S5:

      speeds:
         - 'off'
         - 'Silent'
         - 'Standard'
         - 'Medium'
         - 'Turbo'
         - 'Gentle'

@ophilips
Copy link
Author

@cobch. I tested it with a pause function before but it is extremely difficult to control because there are so many different resume modes. Resume going back to dock, resume cleaning, etc.

@cobch
Copy link

cobch commented Feb 26, 2020

@ophilips I find a mistake in my code in the battery section. The battery sensor is working now (-: The charging is nevertheless still always to "No".

@cobch
Copy link

cobch commented Feb 26, 2020

Could you send the code for the pause that I try?

@ophilips
Copy link
Author

@ophilips I find a mistake in my code in the battery section. The battery sensor is working now (-: The charging is nevertheless still always to "No".

The charging status is not supported at the moment in home assistant. It just gets added when a battery value is linked to your device. So i couldn‘t use that feature.

I will check the pause function again because there were so many changes in the xiaomi integration like the speed settings.

@cobch
Copy link

cobch commented Feb 26, 2020

Ok, thanks and keep in touch!

@cobch
Copy link

cobch commented Mar 13, 2020

Hi again,

the followings speed settings are not working for me. I only manage to make the vacuum use Gentle or standard mode with these settings. Did anybody managed?

    speeds:
      - 'off'
      - 'Silent'
      - 'Standard'
      - 'Medium'
      - 'Turbo'

- 'Gentle'

@cobch
Copy link

cobch commented Mar 13, 2020

    speeds:
      - 'off'
      - 'Silent'
      - 'Standard'
      - 'Medium'
      - 'Turbo'
     - 'Gentle'

@WLines84
Copy link

Hello ophilips

my code is pretty basic but does the trick of showing the vacuum in the Homekit and having the on/off button:
#Xiaomi Mi Robot Vacuum
vacuum:

  • platform: xiaomi_miio
    host: ##########
    token: ###########
    name: To
    However would it be possible to have this information showing in the Home App in the iPhone? (the print is from the EVE app but the principle is the same)
    image

@ophilips
Copy link
Author

ophilips commented Jun 30, 2020

Hey @WLines84, by using your code (which is just the home assistant standard config) you only have access to the on/off switch in Apple Home. It is a new feature since version 0.110 https://www.home-assistant.io/blog/2020/05/20/release-110/#homekit
If you want to have more features like vacuum speed, zones etc. it is still necessary to add some more code to your config.

@cobch
Copy link

cobch commented Nov 29, 2021

Hi @ophilips

Long time ago (-:
I wonder if you have updated the code since the fan.speed service seems not to be working anymore. It would be nice to share a new code if you can.
Thanks

@TerrorSource
Copy link

Doesnt work anymore.

Deprecated services: fan.set_speed
source: https://www.home-assistant.io/integrations/fan/

Does anyone have a working config for Homekit with latest HA versions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment