Xiaomi Gateway (Aqara)

来自Domoticz

小米网关 Xiaomi (Aqara) makes a smart home gateway/hub that has support for a variety of Xiaomi sensors. They can be purchased on AliExpress or other stores at very competitive prices. Protocol is Zigbee and WiFi, and the gateway and Domoticz need to be in the same network/subnet.

Multiple gateways are not supported (this decision has been made to simplify development during beta. For now, you can setup a second Domoticz server on a different subnet to run a 2nd Xiaomi Gateway), however the Smart Socket/Plug *may* act as a wireless repeater so you can use these to extend network range.

If you experience problems and are connecting your Domoticz server through a LAN cable, try connecting Domoticz through WiFi instead.

Currently you need the beta version of Domoticz to support the Xiaomi Gateway and devices

Note that the gateway needs to be at least version 2 (DGNWG02LM). Radio support on the gateway indicates at least version 2. If you are unsure, please ask your supplier before purchasing

Compatible Devices

Compatible Devices:

  1. Human Body Sensor (Motion Sensor)
  2. Wireless Smart Switch
  3. Door/Window Sensor
  4. Temperature and Humidity Sensor
  5. Smart Socket/Plug - Zigbee version (The WiFi version is not supported)
  6. Xiaomi Mi Smart Cube
  7. Wireless Aqara Switch (Dual Button)
  8. Wireless Aqara Switch (Single Button)
  9. Wired Aqara Switch (Dual Button)
  10. Wired Aqara Switch (Single Button)
  11. Xiaomi Gateway LED, Speaker/Alarm, Illumination Sensor
  12. Xiaomi Smoke Detector
  13. Xiaomi Gas Leak Detector

Not Compatible:

Other Xiaomi devices that do not connect directly to the Xiaomi Gateway, such as the Robotic Vacuum, WiFi Power Sockets, Bluetooth Lights, Air Purifier, Kettle etc. Even though these devices may be available in the Mi Home App, they do not communicate directly with the Gateway and so are out of scope of this Domoticz plugin which is only for devices that connect through the Xiaomi Gateway. That is not to say these other devices will not work with Domoticz, only that the will need a different plugin, or script - check the forums for more information.

Adding the Xiaomi Gateway to Domoticz

For Android: You first need to enable local network functions by using the Android Mi Home App https://play.google.com/store/apps/details?id=com.xiaomi.smarthome

  1. Install the App on a Android device
  2. Make sure you set your region to: Mainland China under settings -> Locale - at time of writing this seems to be required.
  3. Mainland China and language can set on English
  4. Select your Gateway in Mi Home
  5. Then the 3 dots at the top right of the screen
  6. Then click on about
  7. Tap the version (2.23 is the current Android version as of 8 March 2017) number at the bottom of the screen repeatedly
  8. You should see now 2 extra options listed in English (was Chinese in earlier versions)until you did now enable the developer mode. [ if not try all steps again!]
  9. Choose the first new option
  10. Then tap the first toggle switch to enable LAN functions. Note down the password (29p9i40jeypwck38 in the screenshot). Make sure you hit the OK button (to the right of the cancel button) to save your changes.
  11. If you change here something, you lose your password! [ this password you need in Domoticz ]


文件:Mihome lan.png


Now choose the second new menu option, and note down the IP address after "localIp".
Make the IP static in your router!

For iOS: You first need to enable local network functions by using the iOS Mi Home App iosApp Mi

  1. Install the App on a iOS device
  2. Make sure you set your region to: Mainland China under settings -> Locale - required for the moment.
  3. Mainland China and language can set on English
  4. Select your Gateway in Mi Home
  5. Then the 3 dots at the top right of the screen
  6. Then click on about
  7. Tap under Tutorial menu(on the blank part) repeatedly
  8. You should see now 3 extra options listed in Chinese until you did now enable the developer mode. [ if not try all steps again!]
  9. Choose the second new option
  10. Then tap the first toggle switch to enable LAN functions. Note down the password (29p9i40jeypwck38 in the screenshot). Make sure you hit the OK button (to the right of the cancel button) to save your changes.
  11. If you change here something, you lose your password! [ this password you need in Domoticz ]


文件:Mihome lan.png


Now choose the third new menu option, and note down the IP address after "localIp".
Make the IP static in your router!


Then in Domoticz go to Setup -> Hardware, and choose the Xiaomi Gateway Type. Enter the following:

  1. Name: "Xiaomi Gateway" or anything you want to name it
  2. Remote Address: the IP address of your gateway
  3. Port: Use 9898 Note that you cannot have another service on your system already using port 9898
  4. Password: The password of your gateway

Note that you may need to clear your browser cache if you can't see the Xiaomi Gateway option and/or all the fields

文件:Xiaomi 03.PNG

  1. Then click the Add button.
  2. set in Domotic new hardware and devices active
  3. Navigate to Setup -> Devices, and you should now see detected devices listed.

You should learn the new Xiaomi devices [ sensors you have ] first on the gateway with the Xiaomi app. When they are learned in the gateway, you will see the devices under new devices in Domoticz.

Playing Gateway Sound

Since FW 1.4.1_145.0141 we are able to play sounds stored on the Gateway (Already stored and personalised ones) for using them Domoticz creates 5 Switches :

  1. Xiaomi Gateway Alarm Ringtone : Off/Police siren 1/Police siren 2/Accident tone/Missle countdown/Ghost/Sniper/War/Air Strike/Barking dogs
  2. Xiaomi Gateway Alarm Clock : Off/MiMix/Enthusiastic/GuitarClassic/IceWorldPiano/LeisureTime/Childhood/MorningStreamlet/MusicBox/Orange/Thinker
  3. Xiaomi Gateway Doorbell : Off/Doorbell ring tone/Knock on door/Hilarious/Alarm clock
  4. Xiaomi Gateway MP3 : Who allow to play personnalise sound that you store on the gateway using MiHome App, see below for more information
  5. Xiaomi Gateway Volume : Who allow to set Gateway Volume, each time this slider is trigger gateway will play last sound set from Domoticz


文件:Gatewaysound.png

Each Sound can be triggered on Scene/Group/Lua/Blocky and Json Call

The following blockly example will show you how to use sounds. This works identical to selector switches. First check what percentage corresponds to your sound by clicking on the 'edit' button of the sound device, for example the doorbell :

文件:Doorbelllevels.PNG

You can use this value in the Blockly script:

文件:Doorbell.png


About Xiaomi Gateway MP3

This switch allows you to play sound that you have stored on the gateway using the MiHome App. Each sound has a record number which starts with 100xx: 10000 : Is always stop 10001 : First personalise sound 10002 : Second personalise sound etc etc..

When Domoticz creates this Switch, it creates a User Variable : XiaomiMP3 with 10001 as a value : remember what is 10001 ? first sound recorded on the gateway ! Change it for 10002, click on Xiaomi Gateway MP3 switch and it will play sound 10002 which is your second personalised sound.

IMPORTANT : If you record a sound as 10001 and erase it after, 10001 value is no longer usable, each new sound value number growup. Actually, we don't know what is the limit, as on file size and on 100xx value

Easy Way to use personalise sound

To be more easy to use, we "offer" you a simple tweak who allow you to play 99 personalise sound.

First create a dummy Selector, and call it Mid Value, edit this brand new dummy and set it as a Dimmer.

文件:Midvalue.png

Then Create a Device Lua Script who will react to this Dummy Slider and update XiaomiMP3 user variable

--script_device_Xiaomimp3.lua
--as each sound is recorded as 100+xx value the slider will update user variable to 10000+dimmer value and play sound
commandArray = {}

if devicechanged['Mid Value'] then
      DomValue = otherdevices_svalues['Mid Value'];
       NumericValue = tonumber(DomValue)
            Calcvalue = (NumericValue + 10000)
            
    commandArray['Variable:XiaomiMP3']=''..Calcvalue..''
    print('Xiaomi Gateway will play sound stored on bank '..Calcvalue)
    commandArray['Xiaomi Gateway MP3']='On'

end
return commandArray

On the dimmer pic : value is 3 that mean that Variable:XiaomiMP3 is set to 10003, gateway will play the 3rd sound I store on the gateway.

With this script each time you change value on the dimmer Gateway will automaticaly play sound, not easy to to on point and clik but really usefull on Scene/Group/Lua/Blocky and Json Call, have a try to record fonction from MiHome app, that an easy way to made some vocal notification with your favorite voice


Great job Corbin & Deennoo !!

lua scripts example

Using the Xiaomi round Switch:

commandArray = {}
-- Commande Selector Switch
    if (devicechanged['Xiaomi Switch Micro-onde'] == 'Click') then
        commandArray[1]={['Xiaomi Smart Plug']='On'}
        commandArray[2]={['Xiaomi Smart Plug']='Off AFTER 10'}
    end

    if (devicechanged['Xiaomi Switch Micro-onde'] == 'Double Click') then
        commandArray[1]={['Xiaomi Smart Plug']='On'}
        commandArray[2]={['Xiaomi Smart Plug']='Off AFTER 30'}
    end
    
    if (devicechanged['Xiaomi Switch Micro-onde'] == 'Long Click' ) then
        commandArray['Xiaomi Smart Plug']='On FOR 2'
    end
return commandArray

Xiaomi Motion Sensor to turn on Yeelights at night:

commandArray = {}

local minutesnow = os.date("%H") * 60 + os.date("%M")
local sunrise = timeofday['SunriseInMinutes']
local sunset = timeofday['SunsetInMinutes']

local nighttime = false
if (minutesnow < (sunrise + 25)) or (minutesnow > (sunset - 25)) then
    nighttime = true
end

motionLights = {}
table.insert(motionLights, {sensorName="Xiaomi Kitchen", lightName="YeeLight Kitchen"})
table.insert(motionLights, {sensorName="Xiaomi Bedroom", lightName="YeeLight Bedroom"})
table.insert(motionLights, {sensorName="Xiaomi Toilet", lightName="YeeLight Toilet"})

for deviceName,deviceValue in pairs(devicechanged) do
    if nighttime == true then
        for i, light in ipairs(motionLights) do
            if (deviceName==light["sensorName"]) then
                commandArray[light["lightName"]] = deviceValue
            end
        end
    else
        --need to make sure light turns off when outside of 'nighttime'
        if deviceValue == "Off" then
            for i, light in ipairs(motionLights) do
                if (deviceName==light["sensorName"]) then
                    commandArray[light["lightName"]] = "Off"
                end
            end
        end
    end
end

return commandArray

Blocky examples

Using the Xiaomi round Switch:

文件:Blocky xiaomi switch.PNG


Troubleshooting

Problem Solution
After adding the gateway, no devices are found in Domoticz OR devices do not update state Solution: You need to ensure the system Domoticz is running on can receive UDP Multicast packets. On Linux, this can be done with

iptables -P OUTPUT ACCEPT

iptables -I INPUT -p udp -j ACCEPT

After adding the gateway, no devices are found in Domoticz OR devices do not update state Make sure the WiFi where your Xiaomi Gateway is connected is on the same subnet as your Domoticz server. Also have your Domoticz server connected directly to the same WiFi network, rather than a LAN cable (try disconnecting the LAN cable as well)
Unable to control devices (Smart Plug/Gateway Light/Alarm) Enter the correct IP address for your gateway. Be aware that if you restart your Xiaomi Gateway, it may get a new IP address and this will need to be updated in Domoticz. Note, devices will still be found if you have an incorrect IP address or password entered.
Error: XiaomiGateway: unable to write command - Invalid Key Enter the correct password for your gateway. Double check it is exact, with no extra spaces.
On Raspberry communication is working after setup into Hardware page but not anymore after reboot/restart Check that the option <Wait for Network at Boot> is enabled ( check here for details and screenshots )

You can enable the raw gateway messages to appear in the Domoticz log by creating a user variable 'XiaomiMessage' of type Integer and value 1. (Setup -> More Options -> User Variables). Then restart the Gateway hardware in Domoticz.