Sorry, my fault, made a mistake, it is sent now.
Presence detection using UniFi Controller and DzVents [Solved]
Moderator: leecollings
-
- 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
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
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
}
- 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
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
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
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
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Re: Presence detection using UniFi Controller and DzVents
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
Thanks, you are my hero !!! Works like a charm
Re: Presence detection using UniFi Controller and DzVents
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
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 }
-
- 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
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.
- 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
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
Rob
-
- 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
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?
Cheers
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
}
Re: Presence detection using UniFi Controller and DzVents
yes, you need to change the path of the cookie in the script:
In my case it is:
Changing this should make the message 404 ==> not found disappear
Code: Select all
local cookie = 'full qualified name of coookie'
Code: Select all
local cookie = /opt/domoticz/userdata/domocookie.txt
-
- 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
Thx Gerald, working now
-
- 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
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.
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.
Who is online
Users browsing this forum: No registered users and 1 guest