Xiaomi Gateway (Aqara)

From Domoticz
Jump to: navigation, search

Xiaomi Gateway

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 supported in the current beta Domoticz release.

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

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

Xiaomi.jpg

French explanation can be found here : http://domo-attitude.fr/xiaomi-smart-home-presentation-inclusion-domoticz/

Compatible Devices

Compatible Devices:

Xiaomi Gateway LED, Speaker/Alarm, Illumination Sensor Buy online (this one is needed in order for all other devices to work)

  1. Human Body Sensor (Motion Sensor) Buy online
  2. Wireless Smart Switch Buy online
  3. Door/Window Sensor Buy online
  4. Temperature and Humidity Sensor Buy online
  5. Smart Socket/Plug - Zigbee version (The WiFi version is not supported) Buy online
  6. Xiaomi Mi Smart Cube (Xiaomi Mi Magic Controller) Buy online
  7. Wireless Aqara Switch (Dual Button) Buy online
  8. Wireless Aqara Switch (Single Button) Buy online
  9. Wired Aqara Switch (Dual Button) Buy online (uses UK Wall Box)
  10. Wired Aqara Switch (Single Button) Buy online (uses UK Wall Box)
  11. Xiaomi Smoke Detector Buy online
  12. Xiaomi Gas Leak Detector Buy online
  13. Xiaomi Aqara Window Door Sensor Buy online
  14. Xiaomi Aqara Temperature Humidity Sensor Buy online
  15. Xiaomi Water Sensor *this sensor currently requires the Beta version of Domoticz


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 (Seems to be the longest line with Chines characters) under settings -> Locale - at time of writing this seems to be required.
  3. Mainland China and language can set on English
  4. Sign In/make an account
  5. Select your Gateway in Mi Home and with a short while
  6. Then the 3 dots at the top right of the screen
  7. Then click on about
  8. Tap the version (2.36 is the current Android version as of 7 October 2017) number at the bottom of the screen repeatedly
  9. 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!]
  10. Choose the first new option
  11. 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.
  12. 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", between all the Chinese text.
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. Enable new hardware before the next steps in Domoticz
  2. Name: "Xiaomi Gateway" or anything you want to name it
  3. Remote Address: the IP address of your gateway
  4. Port: Use 9898 Note that you cannot have another service on your system already using port 9898
  5. 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 (Should be done earlier, I had to delete the gateway and add it again)
  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

Multiple gateways

You can use multiple gateways with the current beta release of Domoticz, however not all functions are supported. You will be able to control the functions of the additional gateways, such as playing sounds (except for the custom mp3) and using the light, however if you control the light from the Xiaomi Home App, the status change will not be reflected in Domoticz.

To use multiple gateways:

  1. Setup your first (Master) gateway as per normal in Domoticz.
  2. Disable the master gateway in Domoticz.
  3. Setup your second gateway as per normal in Domoticz. (repeat this for each additional gateways, only enabling the newest gateway added each time)
  4. Once all gateways are added, enable all gateways, and restart Domoticz.
  5. If you end up with the same device listed for more than one gateway, delete the additional devices in Domoticz.

Note that only the master gateway will listen for incoming messages (the master gateway is the hardware device in Domoticz with the lowest hardware Id). This is not a perfect solution, and if you have any questions or problems, post in the forum and I will try to address them.


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.

You can enable separate voltage devices by adding a user variable 'XiaomiVoltage'. This is not really needed as the battery level should be included with each device, but some users prefer to have the 2 separate devices for each sensor.