Presence detection using UniFi Controller and DzVents  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

rgroothuis
Posts: 347
Joined: Friday 03 April 2015 17:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by rgroothuis »

waaren wrote: Thursday 25 March 2021 11:45
rgroothuis wrote: Thursday 25 March 2021 11:18 @waaren: just sent you a PM with the log file info.
I have not received it.
Sorry, my fault, made a mistake, it is sent now.
basmaaks
Posts: 16
Joined: Tuesday 17 October 2017 18:55
Target OS: -
Domoticz version:

Re: Presence detection using UniFi Controller and DzVents

Post by basmaaks »

anybody here to help me out ?

Got myself a cloud key gen 2, this needs another way to authenticate. I modified the script to the code below, it works but the file domocookie.txt needs to be removed to get it work. Now I've got a bash script that removes the file domocookie.txt every 2 minutes. I think there should be a better solution but can't figure it out myself

Code: Select all

return {
    on = {
        timer = { 'every 3 minutes' },
        -- domoticz.utils.osExecute("bash /home/pi/domoticz/scripts/remove.sh")
        httpResponses = { 'unifi_loggedin' , 'unifi_data' }
    },
    data = {
        presenceHistory = { history = true, maxMinutes = 6 } 		-- Change maxMinutes value to appropriate for your devices
    },
    execute = function(domoticz, item)
        local controllerUrl = 'https://192.168.178.42/' 	--Change IP and Port to your Unifi Controller settings
        local controllerLogin = 'some user'						-- Your username for Unifi Controller
        local controllerPassword = 'my password'-- Your password for Unifi Controller
       
        local devices = {
            ['fc:66:cf:8f:5d:6f'] = 784,	-- phone 1
            ['62:6e:70:7f:6b:45'] = 783,	-- phone 2
        }

        if (item.isTimer) then
            domoticz.openURL({
                url = controllerUrl .. 'api/auth/login',
                method = 'POST',
                postData = { 
                    ['password'] = controllerPassword,
                    ['username'] = controllerLogin 
                },
                callback = 'unifi_loggedin'
            })
        end
        
        if (item.isHTTPResponse and item.ok) then
            if (item.trigger == 'unifi_loggedin') then
                domoticz.openURL({
                    url = controllerUrl .. 'proxy/network/api/s/default/stat/sta',
                    method = 'GET',
                    callback = 'unifi_data'
                })
            end
        
            if (item.trigger == 'unifi_data') then
                local presenceHistory = domoticz.data.presenceHistory

                for mac, device in pairs(devices) do
                    local isPresent = string.find(item.data, mac)
                    
                    presenceHistory.add({
                        ['mac'] = mac,
                        ['isPresent'] = not (isPresent == nil)
                    })
                
                    local wasPresent = presenceHistory.reduce(function(acc, item)
                        if (item.data.mac == mac and item.data.isPresent) then
                            acc = acc + 1
                        end
                        
                        return acc
                    end, 0)
                                       
                    if (wasPresent == 0) then
                        domoticz.devices(device).switchOff().checkFirst()
                    else
                        domoticz.devices(device).switchOn().checkFirst()
                    end
                end
            end
        end
    end
}

User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by waaren »

basmaaks wrote: Saturday 15 May 2021 10:00 anybody here to help me out ?

Got myself a cloud key gen 2, this needs another way to authenticate. I modified the script to the code below, it works but the file domocookie.txt needs to be removed to get it work. Now I've got a bash script that removes the file domocookie.txt every 2 minutes. I think there should be a better solution but can't figure it out myself

You practically have a solution already in your script.

Can you try changing

Code: Select all

        if (item.isTimer) then
            domoticz.openURL({
                url = controllerUrl .. 'api/auth/login',
                method = 'POST',
                postData = { 
                    ['password'] = controllerPassword,
                    ['username'] = controllerLogin 
                },
                callback = 'unifi_loggedin'
            })
        end
to

Code: Select all

        if (item.isTimer) then
             local cookie = 'full qualified name of coookie'
	    domoticz.utils.osExecute('rm ' .. cookie .. ' &' )
            domoticz.openURL({
                url = controllerUrl .. 'api/auth/login',
                method = 'POST',
                postData = { 
                    ['password'] = controllerPassword,
                    ['username'] = controllerLogin 
                },
                callback = 'unifi_loggedin'
            })
        end
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
basmaaks
Posts: 16
Joined: Tuesday 17 October 2017 18:55
Target OS: -
Domoticz version:

Re: Presence detection using UniFi Controller and DzVents

Post by basmaaks »

waaren wrote: Saturday 15 May 2021 10:22
basmaaks wrote: Saturday 15 May 2021 10:00 anybody here to help me out ?

Got myself a cloud key gen 2, this needs another way to authenticate. I modified the script to the code below, it works but the file domocookie.txt needs to be removed to get it work. Now I've got a bash script that removes the file domocookie.txt every 2 minutes. I think there should be a better solution but can't figure it out myself

You practically have a solution already in your script.

Can you try changing

Code: Select all

        if (item.isTimer) then
            domoticz.openURL({
                url = controllerUrl .. 'api/auth/login',
                method = 'POST',
                postData = { 
                    ['password'] = controllerPassword,
                    ['username'] = controllerLogin 
                },
                callback = 'unifi_loggedin'
            })
        end
to

Code: Select all

        if (item.isTimer) then
             local cookie = 'full qualified name of coookie'
	    domoticz.utils.osExecute('rm ' .. cookie .. ' &' )
            domoticz.openURL({
                url = controllerUrl .. 'api/auth/login',
                method = 'POST',
                postData = { 
                    ['password'] = controllerPassword,
                    ['username'] = controllerLogin 
                },
                callback = 'unifi_loggedin'
            })
        end

Thanks, you are my hero !!! Works like a charm
basmaaks
Posts: 16
Joined: Tuesday 17 October 2017 18:55
Target OS: -
Domoticz version:

Re: Presence detection using UniFi Controller and DzVents

Post by basmaaks »

basmaaks wrote: Saturday 15 May 2021 10:00 anybody here to help me out ?

Next question, apple device's uses a different Mac adres every time. How do I modify the script to find hostnames in stead of Mac addresses ?

Code: Select all

return {
    on = {
        timer = { 'every 3 minutes' },
        -- domoticz.utils.osExecute("bash /home/pi/domoticz/scripts/remove.sh")
        httpResponses = { 'unifi_loggedin' , 'unifi_data' }
    },
    data = {
        presenceHistory = { history = true, maxMinutes = 6 } 		-- Change maxMinutes value to appropriate for your devices
    },
    execute = function(domoticz, item)
        local controllerUrl = 'https://192.168.178.42/' 	--Change IP and Port to your Unifi Controller settings
        local controllerLogin = 'some user'						-- Your username for Unifi Controller
        local controllerPassword = 'my password'-- Your password for Unifi Controller
       
        local devices = {
            ['fc:66:cf:8f:5d:6f'] = 784,	-- phone 1
            ['62:6e:70:7f:6b:45'] = 783,	-- phone 2
        }

        if (item.isTimer) then
            domoticz.openURL({
                url = controllerUrl .. 'api/auth/login',
                method = 'POST',
                postData = { 
                    ['password'] = controllerPassword,
                    ['username'] = controllerLogin 
                },
                callback = 'unifi_loggedin'
            })
        end
        
        if (item.isHTTPResponse and item.ok) then
            if (item.trigger == 'unifi_loggedin') then
                domoticz.openURL({
                    url = controllerUrl .. 'proxy/network/api/s/default/stat/sta',
                    method = 'GET',
                    callback = 'unifi_data'
                })
            end
        
            if (item.trigger == 'unifi_data') then
                local presenceHistory = domoticz.data.presenceHistory

                for mac, device in pairs(devices) do
                    local isPresent = string.find(item.data, mac)
                    
                    presenceHistory.add({
                        ['mac'] = mac,
                        ['isPresent'] = not (isPresent == nil)
                    })
                
                    local wasPresent = presenceHistory.reduce(function(acc, item)
                        if (item.data.mac == mac and item.data.isPresent) then
                            acc = acc + 1
                        end
                        
                        return acc
                    end, 0)
                                       
                    if (wasPresent == 0) then
                        domoticz.devices(device).switchOff().checkFirst()
                    else
                        domoticz.devices(device).switchOn().checkFirst()
                    end
                end
            end
        end
    end
}

basmaaks
Posts: 16
Joined: Tuesday 17 October 2017 18:55
Target OS: -
Domoticz version:

Re: Presence detection using UniFi Controller and DzVents

Post by basmaaks »

basmaaks wrote: Monday 24 May 2021 11:40
basmaaks wrote: Saturday 15 May 2021 10:00 anybody here to help me out ?

Next question, apple device's uses a different Mac adres every time. How do I modify the script to find hostnames in stead of Mac addresses ?

Code: Select all

return {
    on = {
        timer = { 'every 3 minutes' },
        -- domoticz.utils.osExecute("bash /home/pi/domoticz/scripts/remove.sh")
        httpResponses = { 'unifi_loggedin' , 'unifi_data' }
    },
    data = {
        presenceHistory = { history = true, maxMinutes = 6 } 		-- Change maxMinutes value to appropriate for your devices
    },
    execute = function(domoticz, item)
        local controllerUrl = 'https://192.168.178.42/' 	--Change IP and Port to your Unifi Controller settings
        local controllerLogin = 'some user'						-- Your username for Unifi Controller
        local controllerPassword = 'my password'-- Your password for Unifi Controller
       
        local devices = {
            ['fc:66:cf:8f:5d:6f'] = 784,	-- phone 1
            ['62:6e:70:7f:6b:45'] = 783,	-- phone 2
        }

        if (item.isTimer) then
            domoticz.openURL({
                url = controllerUrl .. 'api/auth/login',
                method = 'POST',
                postData = { 
                    ['password'] = controllerPassword,
                    ['username'] = controllerLogin 
                },
                callback = 'unifi_loggedin'
            })
        end
        
        if (item.isHTTPResponse and item.ok) then
            if (item.trigger == 'unifi_loggedin') then
                domoticz.openURL({
                    url = controllerUrl .. 'proxy/network/api/s/default/stat/sta',
                    method = 'GET',
                    callback = 'unifi_data'
                })
            end
        
            if (item.trigger == 'unifi_data') then
                local presenceHistory = domoticz.data.presenceHistory

                for mac, device in pairs(devices) do
                    local isPresent = string.find(item.data, mac)
                    
                    presenceHistory.add({
                        ['mac'] = mac,
                        ['isPresent'] = not (isPresent == nil)
                    })
                
                    local wasPresent = presenceHistory.reduce(function(acc, item)
                        if (item.data.mac == mac and item.data.isPresent) then
                            acc = acc + 1
                        end
                        
                        return acc
                    end, 0)
                                       
                    if (wasPresent == 0) then
                        domoticz.devices(device).switchOff().checkFirst()
                    else
                        domoticz.devices(device).switchOn().checkFirst()
                    end
                end
            end
        end
    end
}

Never mind, find the solution myself. Had a - in the hostname and that's not working. Now with one "kfhasdjaksdk" its working perfect !
rgroothuis
Posts: 347
Joined: Friday 03 April 2015 17:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by rgroothuis »

The json response is also giving the device name back, besides the MAC address. You can check on device name as well. I can share my script (created with support from Warren) if you are interested. Currently not at home, cannot copy and paste. Let me know if you need this example script.
User avatar
erem
Posts: 230
Joined: Tuesday 27 March 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Amsterdam/netherlands
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by erem »

basmaaks wrote: Monday 24 May 2021 11:40
basmaaks wrote: Saturday 15 May 2021 10:00 anybody here to help me out ?

Next question, apple device's uses a different Mac adres every time. How do I modify the script to find hostnames in stead of Mac addresses ?
on an iPhone you can switch this off per wifi network.
i have it switched off for my home network.
settings-wifi- choose network-switch off private address
Regards,

Rob
Hellrazr
Posts: 7
Joined: Monday 10 May 2021 14:34
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by Hellrazr »

Hi, I got the same problem. Also have the cloudkey gen2. I Need to delete the domocookie.txt to get the script to work. If I don't delete it I get this:
Error: dzVents: Error: (3.1.7) HTTP/1.1 response: 404 ==>> Not Found

Anyone have any ideas how to fix it?

Code: Select all

return {
    on = {
        timer = { 'every 3 minutes' },
            --domoticz.utils.osExecute("bash /home/pi/domoticz/scripts/remove.sh")
        httpResponses = { 'unifi_loggedin' , 'unifi_data' }
    },
    data = {
        presenceHistory = { history = true, maxMinutes = 6 } 		-- Change maxMinutes value to appropriate for your devices
    },
    execute = function(domoticz, item)
        local controllerUrl = 'https://192.168.1.136/' 	--Change IP and Port to your Unifi Controller settings
        local controllerLogin = 'user'						-- Your username for Unifi Controller
        local controllerPassword = 'password'-- Your password for Unifi Controller
       
        local devices = {
            ['5c:87:30:c1:6b:1d'] = 159,	-- Phone1
            ['da:de:a0:fd:08:46'] = 158,	-- Phone2
        }

        if (item.isTimer) then
             local cookie = 'full qualified name of coookie'
	    domoticz.utils.osExecute('rm ' .. cookie .. ' &' )
            domoticz.openURL({
                url = controllerUrl .. 'api/auth/login',
                method = 'POST',
                postData = { 
                    ['password'] = controllerPassword,
                    ['username'] = controllerLogin 
                },
                callback = 'unifi_loggedin'
            })
        end
        
        if (item.isHTTPResponse and item.ok) then
            if (item.trigger == 'unifi_loggedin') then
                domoticz.openURL({
                    url = controllerUrl .. 'proxy/network/api/s/default/stat/sta',
                    method = 'GET',
                    callback = 'unifi_data'
                })
            end
        
            if (item.trigger == 'unifi_data') then
                local presenceHistory = domoticz.data.presenceHistory

                for mac, device in pairs(devices) do
                    local isPresent = string.find(item.data, mac)
                    
                    presenceHistory.add({
                        ['mac'] = mac,
                        ['isPresent'] = not (isPresent == nil)
                    })
                
                    local wasPresent = presenceHistory.reduce(function(acc, item)
                        if (item.data.mac == mac and item.data.isPresent) then
                            acc = acc + 1
                        end
                        
                        return acc
                    end, 0)
                                       
                    if (wasPresent == 0) then
                        domoticz.devices(device).switchOff().checkFirst()
                    else
                        domoticz.devices(device).switchOn().checkFirst()
                    end
                end
            end
        end
    end
}
Cheers
Gerald
Posts: 4
Joined: Monday 30 October 2017 12:59
Target OS: Linux
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by Gerald »

yes, you need to change the path of the cookie in the script:

Code: Select all

local cookie = 'full qualified name of coookie'
In my case it is:

Code: Select all

local cookie = /opt/domoticz/userdata/domocookie.txt
Changing this should make the message 404 ==> not found disappear
Hellrazr
Posts: 7
Joined: Monday 10 May 2021 14:34
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by Hellrazr »

Thx Gerald, working now :)
madvisionz
Posts: 12
Joined: Tuesday 25 September 2018 12:38
Target OS: NAS (Synology & others)
Domoticz version: 4.10327
Contact:

Re: Presence detection using UniFi Controller and DzVents

Post by madvisionz »

Had this thing working like a charm in the passed with an USG and Domoticz running on Synology. Moved to an UDM-SE and it's not working anymore. Changed the script to the same like post by basmaaks » Saturday 15 May 2021 10:00 (not the cooky part).

Getting error: dzVents: Error: (3.0.1) HTTP/1.1 response: 404 ==>> Not Found

How can I get this working again on my UDM-SE? Also, FW is enabled on UDM, rule needed to read from Domoticz to Unifi?

Thnx in advanced.
Post Reply

Who is online

Users browsing this forum: BarryT and 1 guest