Presence detection using UniFi Controller and DzVents

Easy to use, 100% Lua-based event scripting framework.
Post Reply
Delfuego
Posts: 3
Joined: Sunday 29 July 2018 16:18
Target OS: Linux
Domoticz version: 4.9700
Location: The Netherlands
Contact:

Presence detection using UniFi Controller and DzVents

Post by Delfuego » Friday 10 August 2018 12:22

Recently I installed the script from the next link to monitor devices on my WiFi network using the Unifi Controller.
viewtopic.php?f=63&t=19197
Everything works fine but you need to SSH and CronTab all kind of things.

I wrote a script in DzVents that does the same and is a lot easier to use. You need at least DzVents version 2.4.0.

Same as in the previous link you create a virtual switch for every device you want to monitor.
Make sure DzVents is activated in your Domoticz settings. Check the next link on instructions.
https://www.domoticz.com/wiki/DzVents:_ ... _scripting

In the script you change 5 lines of code to your personal settings.
1. Change IP and Port to your Unifi Controller settings.
2. Change YourPassword and YourUsername to your password and username for Unifi Controller.
3. Change IP and Port to your Unifi Controller settings.
4. Change the mac addresses to addresses of devices you want to monitor. Mac addresses are case sensitive!!
5. Change the Id (Idx) of the devices you want to monitor, the position in DxIndx has to be the same as the position of the mac adress for that device in Mac.

You can add or remove device in step 4 and 5. Just make sure both, mac addresses and Idx numbers, match. So every mac address in Mac needs an Idx in DzInx on the same position.

Activate the script and the monitoring starts.

The Unifi Controller can take up to five minutes to detect a device leaving WiFi. The script runs every five minutes. Worst case it takes ten minutes to detect a device is not present on Wifi by Domoticz.

Code: Select all

return {
   on = {
      timer = { 'every 5 minutes' },
      httpResponses = { 'loggedin' , 'data' }
   },
   execute = function(domoticz, item)
        if (item.isTimer) then
            domoticz.openURL({
                url = 'https://192.168.1.1:8443/api/login',  --Change IP and Port to your Unifi Controller settings
                method = 'POST',
                postData = { ['password'] = 'YourPassword' , ['username'] = 'YourUsername' }, --Change YourPassword en YourUsername to your password en username for Unifi Controller 
                callback = 'loggedin'
         })
        end
        if (item.isHTTPResponse and item.ok) then
            if (item.trigger == 'loggedin') then
                domoticz.openURL({
                url = 'https://192.168.1.1:8443/api/s/default/stat/sta',  --Change IP and Port to your Unifi Controller settings
                method = 'GET',
                callback = 'data'
                })
            else    
                            
                local Mac = {'11:aa:22:bb:33:cc' , '11:aa:22:bb:33:cc' , '11:aa:22:bb:33:cc' , '11:aa:22:bb:33:cc' , '11:aa:22:bb:33:cc' }  --Change to mac addresses of devices you want to monitor
                local DzIndx = {11,12,13,22,33}  --Change to Domiticz Idx, the position in DxIndx has to be the same as the mac adress for that device in Mac (previous line)
                for i,y in ipairs(Mac)
                do
                    home = string.find( item.data , Mac[i])
                    if (home == nil) then
                        domoticz.devices(DzIndx[i]).switchOff().checkFirst()
                    else
                        domoticz.devices(DzIndx[i]).switchOn().checkFirst()
                    end
                end    
            end
        end    
   end 
}
Last edited by Delfuego on Saturday 25 August 2018 9:05, edited 4 times in total.

User avatar
papoo
Posts: 98
Joined: Friday 22 January 2016 23:14
Target OS: Raspberry Pi
Domoticz version: 4.9
Location: France
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by papoo » Friday 10 August 2018 13:52

Thank you
pity that my router is not compatible

mcmikev
Posts: 151
Joined: Tuesday 26 May 2015 8:11
Target OS: Raspberry Pi
Domoticz version: beta
Location: right here
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by mcmikev » Thursday 23 August 2018 15:58

Great script! Did use the crontab version before too, but this is much easier!!
thank you for making it!

CronoS
Posts: 55
Joined: Wednesday 15 July 2015 23:40
Target OS: -
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by CronoS » Monday 27 August 2018 21:22

Awesome! Works like a charm. Also with my unifi controller on Azure! Thanks

Gerald
Posts: 3
Joined: Monday 30 October 2017 13:59
Target OS: Linux
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by Gerald » Friday 14 September 2018 13:06

You sir; just made my day :-)
Thanks!

User avatar
Petturik
Posts: 2
Joined: Friday 13 July 2018 13:05
Target OS: Raspberry Pi
Domoticz version:
Location: NL
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by Petturik » Sunday 30 September 2018 15:09

I had to change a part of the code, seeing my site isn't called "default" anymore.

so I changed :

Code: Select all

url = 'https://192.168.2.12:8443/api/s/default/stat/sta',  --Change IP and Port to your Unifi Controller settings
to

Code: Select all

url = 'https://192.168.2.12:8443/api/s/<8randomcharactersmadeupbyunifi>/stat/sta',  --Change IP and Port to your Unifi Controller settings
After that it works like a charm !
RPi3b+
Z-Wave(+)
Zigbee
ESP8266

credenty
Posts: 10
Joined: Saturday 12 November 2016 12:07
Target OS: Windows
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by credenty » Thursday 18 October 2018 22:19

Hello,
It works perfect in Windows, thank you very much for this great script !!!
I have seen how I can change the execution time of the script, but how can I increase the time it is disconnected and reduce the detection time?

Delfuego
Posts: 3
Joined: Sunday 29 July 2018 16:18
Target OS: Linux
Domoticz version: 4.9700
Location: The Netherlands
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by Delfuego » Friday 19 October 2018 10:32

credenty wrote:
Thursday 18 October 2018 22:19
Hello,
It works perfect in Windows, thank you very much for this great script !!!
I have seen how I can change the execution time of the script, but how can I increase the time it is disconnected and reduce the detection time?
I’m afraid the Unifi controller takes up to five minutes to detect a device has left your WiFi. Worst case in my script detection takes 10 minutes. Running the script every minute makes it a little bit faster but still somewhere between 1 and 6 minutes.
If somebody knows how to speed things up I’m very interested!

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests