Post

Apple HomeKit & Xiaomi Mijia Smart Home Setup|Step-by-Step Guide for Beginners

Discover how to seamlessly integrate Xiaomi Mijia smart cameras and lamps with Apple HomeKit. This guide solves setup challenges, enabling smooth device control and enhanced home automation for beginners.

Apple HomeKit & Xiaomi Mijia Smart Home Setup|Step-by-Step Guide for Beginners

点击这里查看本文章简体中文版本。

點擊這裡查看本文章正體中文版本。

This post was translated with AI assistance — let me know if anything sounds off!


First Experience with Smart Home - Apple HomeKit & Xiaomi Mijia

Mi Home Smart Camera and Mi Home Smart Desk Lamp, HomeKit Setup Tutorial

[2020/04/20] Advanced Guide Released : Experienced users please go directly here » Demo: Using Raspberry Pi as HomeBridge Host to Connect All Mijia Appliances to HomeKit

Chat:

Recently moved to a new place; unlike the previous one, where the ceiling had office-style light fixtures so bright I had to unplug some tubes to make it comfortable for my eyes; the new place has decorative reflective lights, which feel a bit dim for using the computer or reading. After two weeks, my eyes feel drier and more uncomfortable. I initially planned to shop at IKEA, but considering light color and eye protection, I compared cost-performance and finally chose the Xiaomi desk lamp (also owning a Xiaomi smart camera before, both are Mi Home series products).

This Article:

Actually, I didn’t pay much attention to whether it supports Apple HomeKit when I was shopping. As an iOS developer, that’s quite unprofessional of me because I never expected Xiaomi to support it.

This article will introduce Using Apple HomeKit, How to connect smart home devices that do not support Apple HomeKit via third-party integration, and How to build a smart home using Mi Home (with IFTTT).

You can skip to the parts that match your device needs.

Shopping:

I bought two desk lamps in total: one (Pro) for my computer desk, and the other for bedside reading.

Mi Home Desk Lamp Pro

NT$ 1,795 Supports Mi Home, Apple HomeKit

NT$ 1,795 Supports Mi Home, Apple HomeKit

Mi Home LED Smart Desk Lamp

NT$ 995 Only supports Mi Home

NT$ 995 supports only Mi Home.

For detailed information, please refer to the official website. Both lamps support smart control, color changing, brightness adjustment, and eye protection. The Pro version supports Apple HomeKit and three-level angle adjustment. Based on current use, the functionality of a single lamp is quite satisfactory. The only drawback is that the Pro version’s angle adjustment allows only the base to rotate horizontally, not the lamp itself, which means you cannot adjust the light angle!

Ideal Smart Home Goals:

Current Devices:

  1. Mijia Smart Camera PTZ Version 1080P (Supported: Mijia)

  2. Mi Home Desk Lamp Pro (Supports: Apple HomeKit, Mi Home)

  3. Mi Home LED Smart Desk Lamp (Supports: Mi Home)

Ideal Goals:

When returning home: Automatically turn off the camera (for privacy and to prevent accidental triggering of the home alarm; the Mi Home app has a bug that prevents the home alarm from turning on/off as scheduled), turn on the Pro desk lamp (to avoid fumbling in the dark).
When leaving home: Automatically turn on the camera (home monitoring enabled by default), turn off all lights.

Final Achievements of This Article:

Send push notifications when leaving or arriving home, and trigger actions with a tap on the phone (current devices cannot achieve the ideal automation).

The Path to Smart Home Setup:

Apple HomeKit Usage

*Only for Mi Home Desk Lamp Pro! Mi Home Desk Lamp Pro! Mi Home Desk Lamp Pro!

This is the simplest part because it all uses native features.

Just four steps

Only Four Steps

  1. Find the Home app (if not installed, please search “Home” in the App Store and install it)

  2. Open the Home app

  3. Click the “+” icon at the top right to add a nearby location

  4. Scan the HomeKit QR Code at the bottom of the Pro desk lamp to add the accessory!

After successfully adding the accessory, press firmly (3D TOUCH) or long press on the accessory to adjust brightness and color.

How to Use Third-Party Integration with Apple HomeKit for Smart Homes That Do NOT Support Apple HomeKit?

Besides the smart devices that are natively supported, does that mean devices not supporting Apple HomeKit cannot be controlled through the Home app at all?
This chapter will guide you step-by-step on how to add unsupported devices (cameras, regular desk lamps) to the Home app!

Mac ONLY, Windows users please skip to the Mi Home section

My device is MacOS 10.14/iOS 12

Using HomeBridge

HomeBridge uses a Mac computer as a bridge to simulate unsupported devices as HomeKit accessories, allowing them to be added to the “Home” app.

Operation Comparison

Operation Comparison

One key point is you need to keep a Mac computer turned on to maintain a smooth bridging channel; once the computer is shut down or asleep, you won’t be able to control those HomeKit devices.

Of course, some experts online use a Raspberry Pi as a bridge by buying one and setting it up themselves; however, this involves too much technical detail and will not be covered in this article.

If you still want to try it after knowing the drawbacks, you can continue reading or skip to the next section about using Mijia directly.

Step 1:

Install node.js: Click here Download and install.

Step 2:

Open “Terminal” and enter

1
sudo npm -v

Check if the node.js npm package manager is installed successfully: displaying the version number means success!

Step 3:

Install the HomeBridge package via npm:

1
sudo npm -g install homebridge --unsafe-perm

After the installation is complete… the HomeBridge tool is ready to use!

Earlier, it was mentioned that “HomeBridge uses a Mac computer as a bridge to simulate unsupported devices as HomeKit devices.” In fact, HomeBridge is just a platform, and each device requires separate HomeBridge plugins to be added.

It’s easy to find resources by searching “mija product English name homebridge” on Google or GitHub; here are two device resources I use:

1. Mijia Camera PTZ Version Resources: MijiaCamera

Cameras are more complex devices, so I spent some time researching and organizing information; I hope this helps those in need!

First, use the “Terminal” to run the command to install the MijiaCamera npm package.

1
sudo npm install -g homebridge-mijia-camera

After installation, we need to obtain two pieces of information from the camera: the network IP address and the Token.

Open the Mi Home app → Camera → Top right corner “…” → Settings → Network info to get the IP address!

Token information is more complicated and requires you to connect your phone to your Mac:

Open iTunes Interface

Open the iTunes interface

Select backup Do NOT check Encrypt local backup, then click “Back Up Now”

After the backup is complete, download and install the backup viewer software: iBackupViewer

Open “iBackupViewer.” On first launch, it will ask you to go to Mac “System Preferences” - “Security & Privacy” - “Privacy” - “+” - add “iBackupViewer.”
**If you have privacy concerns, you can disable network access for this software and remove it after use*

After reopening “iBackupViewer” and successfully loading the backup file, click the top right corner to switch to “Tree View” mode.

The left side will show all your installed apps. Find the Mi Home app “AppDomain-com.xiaomi.mihome” -> “Documents”

Find and select the file “數字_mihome.sqlite” in the document list on the right side.

Click the top right “Export” -> “Selected”

Upload the exported SQLite file to https://inloop.github.io/sqlite-viewer/ to view its contents.

You can see all device information fields on the Mi Home app. Scroll right to the end, find the ZTOKEN field, double-click to edit, select all, and copy.

Finally, open the website http://aes.online-domain-tools.com/ to convert ZTOKEN into the final Token.

  1. Paste the copied ZTOKEN into “Input Text” and select “Hex”.
  2. Enter the key “00000000000000000000000000000000” (32 zeros) and also select “Hex”.
  3. Then click “Decrypt!” to convert.
  4. Select all, copy the text in the blue box at the bottom right, and remove spaces to get the Token we need.

Here, I tried to directly sniff the token using “miio,” but it seems the Mijia camera firmware has been updated, and this method no longer quickly and easily obtains the token!

Back to HomeBridge! Editing the config.json file

Use “Finder” -> “Go” -> “Go to Folder” -> enter “~/.homebridge” to navigate.

Open “config.json” with a text editor. If the file does not exist, create one or click here to download and place it directly inside.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
   "bridge":{
      "name":"Homebridge",
      "username":"CC:22:3D:E3:CE:30",
      "port":51826,
      "pin":"123-45-568"
   },
   "accessories":[
      {
         "accessory":"MijiaCamera",
         "name":"Mi Camera",
         "ip":"",
         "token":""
      }
   ]
}

Add the above content to config.json, inserting the IP and Token obtained earlier.

At this point, return to the “Terminal” and run the following command to start HomeBridge.

1
sudo homebridge start

If you have already started and then changed the content of config.json, you can modify it as follows:

1
sudo homebridge restart

Restart

At this point, a HomeKit QRCode will appear for you to scan and add the accessory (the steps are as mentioned above for adding Apple HomeKit devices).

The following status messages will also appear:
[2019–7–4 23:45:03] [Mi Camera] connecting to camera at 192.168.0.100…
[2019–7–4 23:45:03] [Mi Camera] current power state: off

The appearance of these and no error messages means the setup was successful!

Common errors usually involve incorrect tokens. Just check if any steps in the above process were missed.

You can now turn your Mi Home smart camera on and off from the “Home” app!

2. Mi Home LED Smart Desk Lamp HomeBridge Resource: homebridge-yeelight-wifi

Next is the Mi Home LED Smart Desk Lamp. Since it doesn’t support Apple HomeKit like the Pro version, we still need to add it using HomeBridge. Although the steps do not require the complicated process of obtaining IP and Token, making it simpler than cameras, the desk lamp has its own quirks. You need to pair it with another YeeLight app and enable local network control:

I have to complain about this poor integration. The native Mi Home app cannot perform this setting; so please search for the “Yeelight” app on the App Store to download and install it.

Open the app -> Log in directly with your Mi Home account -> Add device -> Mi Home Desk Lamp -> Follow the instructions to rebind the desk lamp to the Yeelight app

After completing device binding, go back to the “Devices” page -> tap “Mi Home Desk Lamp” to enter -> tap the “△” tab at the bottom right -> enter “LAN Control” settings -> turn on the switch to allow LAN (Local Area Network) control.

The desk lamp setup is complete. You can keep using this app to control the lamp or rebind it to the Mi Home app.

Next is the HomeBridge setup; first, open the “Terminal” and run the command to install the homebridge-yeelight-wifi npm package

1
sudo npm install -g homebridge-yeelight-wifi

After installation, follow the same camera steps and go to the ~/.homebridge folder. Create or edit the config.json file, and this time, just add the following inside the last }

1
2
3
4
5
6
"platforms": [
   {
         "platform" : "yeelight",
         "name" : "yeelight"
   }
 ]

Got it!

Finally, combine the above camera config.json file as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
 "bridge": {
  "name": "Homebridge",
  "username": "CC:22:3D:E3:CE:30",
  "port": 51826,
  "pin": "123-45-568"
 },

 "accessories": [
  {
   "accessory": "MijiaCamera",
   "name": "Mi Camera",
   "ip": "",
   "token": ""
  }
 ],

 "platforms": [
   {
         "platform" : "yeelight",
         "name" : "yeelight"
   }
 ]
}

Then, go back to the “Terminal”:

1
sudo homebridge start

or

1
sudo homebridge restart

You can now see the previously unsupported Mi Home LED Smart Desk Lamp added to the HomeKit “Home” app!

Supports color and brightness adjustments as well!

All HomeKit accessories are set up, how to make them smart?

After connecting everything properly, open the “Home” app as usual.

Add a scenario following the steps; here, “Going Home” is used as an example:

Tap the “+” at the top right -> Add Scene -> Custom -> Enter the accessory name (e.g., Home) -> Tap “Add Accessories” below -> Select the connected HomeKit accessories -> Set the accessory status for this scene (Camera: Off / Desk Lamp: On) -> Tap “Test Scene” to test -> Tap “Done” at the top right!

The setup is complete! Now, tap “Scene” on the homepage to execute all the accessory settings inside!

Another quick tip is to swipe up the control center and tap the house-shaped button to quickly operate HomeKit/execute scenes (you can switch modes at the top right)!

With intelligence in place, how to automate?

The intelligence is already in place. Now I want to achieve the ultimate goal: automatically turn off the camera and turn on the lights when I get home; automatically turn on the camera and turn off the lights when I leave home.

Switch to the third tab “Automation” to set it up. Sorry, none of the devices mentioned above (iPad/Apple TV/HomePod) can act as a Home Hub, so I did not explore this area.

The principle seems to be that when you return home, detecting the “home hub” phone/watch can accurately trigger the action!

Here I found a tricky method: (GPS sensing)

Using third-party apps to integrate with “Home” and add automation settings allows you to bypass restrictions on using the “Automation” tab by leveraging your phone’s GPS location for automation.

p.s GPS may have an error margin of about 100 meters

Here, the third-party integrated app I use is: myHome Plus

After downloading and installing, open the app -> allow access to “Home Data” -> you will see the “Home” data configuration -> tap the “Settings” button at the top right -> tap “My Home” to enter
-> scroll down to the “Triggers” section -> tap “Add Trigger”

Select the trigger type as “Location” -> Enter a name (e.g., Home) -> Click “Set Location” to set the area -> Then, under REGION STATUS, choose whether entering or leaving the area triggers it -> Finally, under SCENES, select the corresponding “scene” to execute (created above)

After tapping “Done” at the top right to save, go back to the “Home” app, and you will see the “Automation” tab is enabled and ready to use!

At this point, you can choose the “+” at the top right to directly add an automation script using the “Home” app!!

The steps are similar to third-party apps but with better integration! After creating automation with the native “Home” app, you can also swipe to delete the ones made with third-party apps.

!! Just make sure to keep at least one item; otherwise, the Tab will revert to its original locked state!!

Siri Voice Control Section:

Compared to the Mi Home introduced below, HomeKit offers much higher integration, allowing direct voice control of configured accessories and scene execution without any additional setup.

This concludes the introduction to HomeKit settings. Next, let’s explain how to use the native Mi Home smart system.

How to Build a Smart Home Using Mijia Itself:

Here is a confusing point: I couldn’t find the exact same Mi Home desk lamp when adding a device in the Mi Home app. The answer is:

Just look at the text, this is it

Just read the text, this is it.

Other devices: For cameras and Pro desk lamps, just follow the official instructions to set them up and add them. No further details will be provided here.

Scenario Setting:

Same as “Family Setup” -> Switch to the “Smart” Tab -> Select “Manual Execution” -> Choose device actions below (more features available since it’s native) -> Continue adding other devices (desk lamp) -> “Save” to complete!

Someone will definitely ask why not just choose “Leave or Arrive at a Location”? Because this feature is basically useless. The app is not optimized for GPS in Taiwan, and its positioning can only be set to landmarks. If your location matches, you can use this feature directly. You can skip the rest of the article!

Trivia: The China Map in Google Maps Is Completely Wrong!

For quick toggles, you can set up widgets from “My” -> “Widgets”!

This allows you to quickly execute scenes and devices from the Notification Center!

You can also control components from the Apple Watch! If the watch app keeps showing a blank screen, please delete and reinstall the watch or phone app, as this app has quite a few bugs.

With intelligence in place, how do we automate?

Here, you still need to use the GPS sensing method. If the new scene mentioned above uses “leaving or arriving at a location,” you can skip the following settings!


[2019/09/26] Update iOS ≥ 13 to Achieve Automation Using Only the Built-in Shortcuts App:

iOS ≥ 13.1 Use the “Shortcuts” automation feature with Mi Home smart devices, click to view »


iOS ≥ 12, iOS < 13 Only :

Using the Built-in Shortcuts App with IFTTT

First go to “Me” -> “Lab Features” -> “iOS Shortcuts” -> “Add Mijia Scene to Shortcut”

Open the built-in “ Shortcuts “ app (if you can’t find it, search and download it from the App Store).

Click the “+” at the top right to create a shortcut -> Tap the “Done” button in the top right after setting below -> Name -> Enter a name (English is recommended, as it will be used later)

Return to the Add Shortcut page -> enter “Mi Home” in the bottom menu search -> add the corresponding scene set in Mi Home, then turn off “Show When Run” to prevent Mi Home app from opening after execution.

*If you cannot find Mi Home, please return to the Mi Home app and try toggling “Me” -> “Lab Features” -> “iOS Shortcuts” -> “Add Mi Home Scenes to Shortcuts,” then close and reopen the Shortcuts app.

At this point, we need to use a third-party app. We use IFTTT for GPS-based background triggers for entering and leaving locations. Search for “IFTTT” in the App Store to download and install it.

Open IFTTT and log in. Switch to the “My Applets” tab, then click the “+” icon in the top right to add a new applet.
Click “+this” -> search for “Location” -> choose either entering or leaving.

Set location -> Click “Create trigger” to confirm -> Then click the “+that” below -> Search for “notification”

Choose “Send a rich notification from the IFTTT app”:

Title = Notification Title, Message = Notification Content

Link URL please enter: shortcuts://run-shortcut?name= Shortcut Name

That’s why it’s better to name shortcuts in English whenever possible.

-> Click “Create action” -> You can click “Edit title” to set the name

-> “Finish” saved successfully!

The next time you leave or enter the set area, you will receive a triggered notification (with about a 100-meter margin of error). Tapping the notification will automatically execute the Mi Home scene!

Tap the notification to automatically run the scene in the background

Clicking the notification will automatically run the scene in the background

Siri Voice Control Section:

Since Mi Home is not a built-in Apple app, supporting Siri voice control requires additional setup:

In the “Intelligence” Tab -> “Add to Siri” -> Select “Target Scenario” and press “Add to Siri”

-> Tap the red record button (e.g., turn off the light) -> Done!

You can directly call and control the execution of scenes in Siri!

Summary

The lengthy setup steps above can be summarized as follows:

For a good experience, you have to spend a lot of money on appliances with the HomeKit logo (so you don’t need to keep a Mac running as a HomeBridge server; they integrate perfectly with Apple’s native Home app). You also need to buy a HomePod, Apple TV, or iPad to serve as the home hub. Both HomeKit-certified appliances and home hubs are expensive!

If you have technical skills, consider using third-party smart devices (such as Mi Home) with a Raspberry Pi to set up HomeBridge.

If you are an ordinary user like me, it’s still easiest to use Mi Home. My current habit is to execute scene operations from the notification center’s quick widget when I arrive or leave home. The Shortcuts app combined with IFTTT is only used for notification reminders, in case I sometimes forget.

Although the current experience has not reached the ideal goal, it is already a step closer to “Smart Home”!

Advanced Section

Demo: Using Raspberry Pi as a HomeBridge Host to Connect All Mijia Appliances to HomeKit

Further Reading

  1. Xiaomi Smart Home New Purchases (AI Speaker, Temperature and Humidity Sensor, Scale 2, DC Inverter Fan)

  2. iOS ≥ 13.1 Automation with “Shortcuts” for Mijia Smart Home (Using the built-in Shortcuts app in iOS ≥ 13.1 to complete automation)

  3. Mi Home APP / Xiao Ai Speaker Region Issues

If you have any questions or feedback, feel free to contact me.


Buy me a beer

This post was originally published on Medium (View original post), and automatically converted and synced by ZMediumToMarkdown.

Improve this page on Github.

This post is licensed under CC BY 4.0 by the author.