Presence detection using UniFi Controller and DzVents

Easy to use, 100% Lua-based event scripting framework.
Post Reply
Delfuego
Posts: 2
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 en YourUsername to your password en 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 11 August 2018 19:04, edited 2 times in total.

User avatar
papoo
Posts: 89
Joined: Friday 22 January 2016 23:14
Target OS: Raspberry Pi
Domoticz version: beta
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

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests