This is a PowerShell module, which allows to interact with Xiaomi Mi Smart Home devices programmatically.The module contains a group of functions, that you can easily integrate with other scripts and tools, to work with the information provivided by the Xiaomi Smart Home sensors. One of such cases, can be using this script to query the temperature data from the temperature sensor regually and then using some other script or a tool to draw temperature graphs.
Currently, the number of supported devices is limited, as well as only read-only actions are supported. Anyhow, there are plans to provide support for additional sensors in the near future.
- For starters, you need to have a Xiaomi Mi Smart Home Gateway.
- You probably want to have at least one sensor, e.g. temperature/humidity one, as well.
- To be able to access the gateway over the network, you must have developer mode unlocked by following these steps:
- Download the latest version of MiHome application to your Android device (yes, at the moment, developer mode can be enabled only via Android).
- Follow the in-app instructions to connect the application to your Xiaomi Mi Smart Home Gateway.
- Once the Gateway is added, open the Gateway settings and devigate to About section.
- At the bottom of About sections, there should be the version number listed. Click it multiple times until you see to additional options (both in Chinese) appearing above.
- Click the first new option (the one with the longer Chinese name).
- Take a note of the 16 symbol key you see on the second line. You might need this key for the write operations.
- Activate the first option and click the right Chinese button (probably meaning OK) at the bottom. The developer mode should be active now.
- You also need Windows Management Framework 5.0 (or above) to use the module.
The module is published in PowerShell Gallery repository, so the easiest way to install it is from there. Simply run the following command in you PowerShell prompt:
Install-Module -Name Xiaomi;
In case the above installation using the package manager fails, you can install the module manually. Simply
download the latest version of the module and extract
the Xiaomi-master folder into the folder, which is included in your $ENV:PSModulePath
, e.g.
%USERPROFILE%\Documents\WindowsPowerShell\Modules. Make sure to rename Xiaomi-master to Xiaomi afterwards.
To confirm the module is successfully installed and see all the available functions, use the following command:
Get-Command -Module Xiaomi;
Currently, the module contains the following functions:
- Connect-XiaomiSession
- Disconnect-XiaomiSession
- Get-XiaomiGateway
- Get-XiaomiSensor
- Get-XiaomiTemperature
The following example can be used as the simplest test to see if all (any) gateway devices on the network are reachable.
# Initate the connection:
$Connection = Connect-XiaomiSession;
# Get the gateway(s) information:
$Connection | Get-XiaomiGateway;
# Close the connection:
$Connection | Disconnect-XiaomiSession;
This example shows how to get the list of all the sensors attached to the gateway(s). It might be a good idea to run it to make sure that all the sensors are discovered, before performing any actions on them.
# Initate the connection:
$Connection = Connect-XiaomiSession;
# Get the gateway(s) and all the sensors attached the each of them:
$Connection | Get-XiaomiGateway | Get-XiaomiSensor;
# Close the connection:
$Connection | Disconnect-XiaomiSession;
This is a more detailed example, which keeps querying data from the temperature sensors every few seconds. Also, this time we are passing the parameters to the functions directly, instead providing them via a pipeline as in the previous examples.
# Initate the connection:
$Connection = Connect-XiaomiSession;
# Get the gateway(s):
$Gateway = Get-XiaomiGateway -Connection $Connection;
# Get all the sensors:
$Sensor = Get-XiaomiSensor -Gateway $Gateway;
# Keep querying and printing temperature/humidity every 10 seconds:
While ($TRUE) {
Get-XiaomiTemperature -Sensor $Sensor | FT;
Start-Sleep -Seconds 10;
}
# Close the connection:
$Connection | Disconnect-XiaomiSession;
The following resources were invaluable in writing this PowerShell module:
- https://github.com/jon1012/mihome
- https://notes.jmsinfor.com/blog/post/admin/Xiaomi-Hub
- https://louiszl.gitbooks.io/lumi-gateway-local-api/content/
- http://bbs.xiaomi.cn/t-13198850
- https://github.com/rockmanqh/blynk-aquara
Thank you!