Using the Xiaomi Door/Window Sensor

After I got my Xiaomi kit, I integrated the Gateway and Button pretty quickly into my Smart-Home set up. The Door/Window sensors, on the other hand, took a little more time to figure out. However, it ended up being worth it in the end. These sensors changed the game by making automations that respond to actions; as opposed to buttons, time, or your voice. This, I feel, is my first entry into true Home Automation, instead of just having smart devices.

There are two Door/Window sensors that are compatible with the Xiaomi Gateway; a first generation and second generation version. With my Door/Window sensors, I wanted to try a couple of things. First, I wanted to be able to monitor the front door. With this, I figured I could set up a very, very, very basic alarm system. Secondly, I wanted to monitor the bedroom door. I was already using the Gateway as a night light in the bathroom at night. With the Door/Window sensor, I could automate that as well as my morning lights routine.

This guide will show you:

  • How to connect the Door/Window Sensors to the Xiaomi Gateway.
  • How to add the Sensors to your Home Assistant front end.
  • How to use the Door/Window Sensors in automations.
  • Make a simple Door alarm.
  • How to add your own “ringtones” to the Xiaomi Gateway

The Xiaomi Door/Window Sensors

I used Google Translate to provide a Shoddily Translated English Manual for the Xiaomi Door/Window Sensor

Setting up the Door/Window Sensors

  • Setting up the Door/Window Sensor is identical to setting up the Button:
    • Push the button on the Gateway 3 times.
      • Be careful, holding the button down for 5 seconds will reset the Gateway.
    • If the Notification volume is still on, the Gateway will prompt you to connect the sensor.
    • Use a pin or sim card tool to push into the hole in the bottom of the Sensor.
    • It should now be connected.

    Screen Shot 2017-11-04 at 4.56.30 PM

    • You can check within the App or by restarting Home Assistant searching for it in the Developer Tool’s States page.


    • Now that the Door/Window sensor is connected, you can confirm its connection still works wherever you put it by pushing the hole with a pin again. This will cause the Gateway to confirm if it’s still connected.
  • Note: Once connected to the Gateway, the violet light on the sensor will stop flashing when it switches on or off.

Adding the Sensors to your Front End.

  • To add the sensors to your front end, you can use a Template Sensor in your configuration.yaml. Mine is in my sensors.yaml file.
  • Below are how I added both sensors to my front end.

Screen Shot 2017-11-19 at 4.59.59 PM

  • For the binary Door/Window sensors, “off” is when the sensor is closed, and “on” is open. I wanted my front end to say “Open” and “Closed” as well as change the icon to match the state of the door.

Screen Shot 2017-11-19 at 5.00.12 PM

  • This is how it will look when added to your front end.
  • Now let’s put the sensors to work.

Automated Night Light & Home Wake-Up Script

  • My first move in automation, was to use the Gateway as a Night Light in the bathroom.
  • First, you’ll want to open your automation.yaml file.

Screen Shot 2017-11-05 at 11.44.59 AM

  • This first part, makes the light turn on when the bedroom door opens.

Screen Shot 2017-11-05 at 11.45.09 AM

  • Then, it shuts off when the door is closed.

Screen Shot 2017-11-19 at 4.33.01 PM

  • You can add both automations to a group, so that only the group needs to be called when activating these automations.

Screen Shot 2017-11-19 at 4.40.03 PM

Screen Shot 2017-11-19 at 4.40.42 PM

  • When added to a group, the automations can all be accessed by clicking the automation group.

Screen Shot 2017-11-22 at 4.45.05 PM

  • Now, I can activate the Night Light automation by turning on group.evening_night_light within the bedtime script from a previous post.
  • The automation will stay on throughout the night.
  • Notice I also tied the bedroom door sensor to the end of my bedtime script.
    • These sensors can have multiple uses.
    • Now, my lamp will stay on in the living room, until I close the bedroom door to go to bed.

Screen Shot 2017-11-19 at 4.35.49 PM

  • With the addition of this new sensor, I also changed my whole Morning Wake Up automation.
  • This new routine doesn’t use the time to turn lights on. Instead, it will turn on my lights when I open the bedroom door, after my usual 5:15 wake up time.
    • It also turns off the Night Light automation group as well as turning itself off.
  • This creates a nice smooth string of actions that all go into effect after I tell Alexa that I’m going to bed. It goes like this:
    • Tell Alexa it’s time for bed (this can be done with a custom Alexa skill or by using the Alexa Routines)
    • The bedroom turns on
    • The living room turns off (except the main lamp, which turns off when the bedroom door closes)
    • Night Light & Morning Wake up automations turn on
    • When I go to bed and shut the door, the rest of the living room shuts off
    • Throughout the night, if the bedroom door is open, the night light turns on
    • After 5:15, when the bedroom door is opened, the living room turns on, and the Night Light and Morning routine automations are all
    • turned off.

Make A Simple Alarm

  • Creating a simple alarm using the Xiaomi Gateway and Door/Window Sensors is really easy
  • First, open your automations.yaml file to begin a new automation.

Screen Shot 2017-11-19 at 5.06.07 PM

  • This automation uses the sensor on my front door. When opened, the Gateway will play whichever sound I choose. That’s it!

Screen Shot 2017-11-19 at 5.06.59 PM

  • I also added a script to my front end for turning off and silencing the alarm.

Screen Shot 2017-11-19 at 5.14.01 PM

  • I used the MDI Icons “alarm-light” and “volume-off” when customizing the entities.
  • You can now tie in your alarm to any of your other automations. I am considering tying it to the rest of my Bedtime/Wake up routine to go off if someone opens the door while we’re sleeping.

Screen Shot 2017-11-19 at 5.00.41 PM

BONUS: You can add your own “ringtones” to the Xiaomi Gateway!

I wanted to have a better alarm sound than the one I used creating the automation above. You can add your own sounds to the Gateway through the MiHome app. I used an alarm that I found amongst these different alarm sounds.

  • Navigate to the Gateway in the MiHome app
  • Choose “Device” from the top of the screen
  • Select the Gateway
  • Select Ringtone Settings
  • Choose one of the Ringtone Types
  • Touch the “+ Add Ringtone” at the bottom of the page
  • Choose your desired file and touch “Yes”
  • Your file will now be available at the top of the page as a “Custom Ringtone.”

The Xiaomi Gateway and Door/Window sensors are a great addition to any Smart-Home set up. The sensors dramatically increase the possibilities of home automation and are very affordable. They took me a little more time to add to the system, but that is just because they are capable of doing so much. I highly recommend them. If these guides have been helpful to you, please share them! If you have any questions, please reach out to me in the comments. Thanks for visiting!

If you’re interested in getting started with the Xiaomi Gateway, I recommend starting with one of the kits.

Different Gateway Kits Available


All of the parts can also be purchased individually:


8 thoughts on “Using the Xiaomi Door/Window Sensor

  1. Thanks for this! I’ve been following along and all of your posts have been very helpful. Do you have any plans to do a how-to write up on using Amazon dash buttons with hassio?


    1. Thank you! I have not been able to successfully get Amazon dash buttons to work with Home Assistant. I’ve tried 3 separate times with 2 versions of buttons. It was not reliable enough to use for anything. Actually, not being able to use the dash buttons is what lead me to the Xiaomi stuff because of the Xiaomi buttons.


  2. Hi,
    very nice blog with a lot of helpful tips and tricks, love it.
    Do you publish your config file somewhere??
    Cause it would be hard to type everything that you show in your pictures…



  3. hey dude love the articles, just an observation would be great if your code was actual text, easier to cut and paste into my files compared to images.



  4. Well done, the explanations are very effective – just please, can you pass the codes that we can copy instead of writing from the pictures?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s