Page 4 of 4

Re: Presence detection using UniFi Controller and DzVents

Posted: Thursday 25 March 2021 12:08
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.

Re: Presence detection using UniFi Controller and DzVents

Posted: Saturday 15 May 2021 10:00
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
}


Re: Presence detection using UniFi Controller and DzVents

Posted: Saturday 15 May 2021 10:22
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

Re: Presence detection using UniFi Controller and DzVents

Posted: Saturday 15 May 2021 10:32
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

Re: Presence detection using UniFi Controller and DzVents

Posted: Monday 24 May 2021 11:40
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
}


Re: Presence detection using UniFi Controller and DzVents

Posted: Monday 24 May 2021 13:06
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 !

Re: Presence detection using UniFi Controller and DzVents

Posted: Monday 24 May 2021 13:18
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.

Re: Presence detection using UniFi Controller and DzVents

Posted: Monday 24 May 2021 13:23
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

Re: Presence detection using UniFi Controller and DzVents

Posted: Friday 30 July 2021 20:14
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

Re: Presence detection using UniFi Controller and DzVents

Posted: Monday 18 October 2021 12:21
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

Re: Presence detection using UniFi Controller and DzVents

Posted: Monday 01 November 2021 9:14
by Hellrazr
Thx Gerald, working now :)

Re: Presence detection using UniFi Controller and DzVents

Posted: Thursday 11 August 2022 12:46
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.