Re: Presence detection using UniFi Controller and DzVents
Posted: Thursday 25 March 2021 12:08
Open source Home Automation System
https://forum.domoticz.com/
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
}
You practically have a solution already in your script.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
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
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
waaren wrote: ↑Saturday 15 May 2021 10:22You practically have a solution already in your script.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
Can you try changingtoCode: Select all
if (item.isTimer) then domoticz.openURL({ url = controllerUrl .. 'api/auth/login', method = 'POST', postData = { ['password'] = controllerPassword, ['username'] = controllerLogin }, callback = 'unifi_loggedin' }) end
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
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 !basmaaks wrote: ↑Monday 24 May 2021 11:40basmaaks 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 }
on an iPhone you can switch this off per wifi network.
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
}
Code: Select all
local cookie = 'full qualified name of coookie'
Code: Select all
local cookie = /opt/domoticz/userdata/domocookie.txt