Unifi controller - Presence detection

Moderator: leecollings

vrmp
Posts: 18
Joined: Friday 21 July 2017 23:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8100
Location: Netherlands
Contact:

Unifi controller - Presence detection

Post by vrmp »

I created a LUA script to read the Unifi controller for a specific device name and report the status.
Perhaps someone else has some use for it aswell.

The Function:

Code: Select all

function UnifiController(Username)
	unifi_username = "admin" -- Username for example admin
	unifi_password = "admin123" -- Password for example 1234546
	unifi_controller_ip = "192.168.0.2" -- Controller IP for example 192.168.0.2
	unifi_controller_port = "8443"  -- Controller port, default is 8443
	cookie = "/tmp/unifi_cookie_lua" -- Temp cookie file
	tempfilename = "/tmp/UnifiController.tmp" -- Temp JSON output file

	local f = io.popen("stat -c %Y " .. tempfilename)
	local last_modified = f:read()
	if (os.difftime (os.time(), last_modified) > 30) 
	then
		-- URL for logging in, query and logging out
		url_login = 'curl --cookie ' .. cookie .. ' --cookie-jar ' .. cookie .. ' --insecure -H \'Content-Type: application/json\' -X POST -d \'{"password":"' .. unifi_password .. '","username":"' .. unifi_username .. '"}\' https://' .. unifi_controller_ip .. ':' .. unifi_controller_port .. '/api/login'
		url_open = 'curl --cookie ' .. cookie .. ' --cookie-jar ' .. cookie .. ' --insecure -s -o '..tempfilename..' --data "json={}" https://' .. unifi_controller_ip .. ':' .. unifi_controller_port .. '/api/s/default/stat/sta'
		url_logout = 'curl --cookie ' .. cookie .. ' --cookie-jar ' .. cookie .. ' --insecure https://' .. unifi_controller_ip .. ':' .. unifi_controller_port .. '/logout'
		-- Execute url
		-- Execute url
		read_login = os.execute(url_login)
		read_open = os.execute(url_open)
		read_logout = os.execute(url_logout)
	end

	file = io.open(tempfilename, "r")
	while true do 
		line = file:read("*line")
		if not line then break end
		if string.find(line, Username) 
		then
			UnifiResult = "Home"
		else
			UnifiResult = "NotHome"
		end
	
	end
	file:close()
	
	return UnifiResult
end
An example:

Code: Select all

--Example
TestUser = "Domoticz"
print(TestUser .. ": " .. UnifiController(TestUser))
The status will be "Home" or "NotHome" and I use this to set a virtual device to on or off.
DomoFrank
Posts: 2
Joined: Friday 11 August 2017 21:48
Target OS: Linux
Domoticz version:
Contact:

Re: Unifi controller - Presence detection

Post by DomoFrank »

I would really like to use this function in my Domoticz. I'm not very experienced with LUA, can you explain me how I can implement this?
I did create a User Variable 'UniResult' and I've copied your function to a LUA script and call the function in a dzVents like this:

Code: Select all

return {
    active = true,
    on = {
		timer = {
			'at 15:14'
		}
    },
    execute = function(domoticz)
        local TestUser = "iphone"
        UnifiResult = UnifiController(TestUser)
        print(TestUser .. ": " .. UnifiResult)
    end
}
I use a timer to test the script, finally I will connect it to a dummy device/switch.

But I get 2 Errors:
2017-08-11 22:16:00.335 dzVents: Info: ------ Start internal script: HomeTest:, trigger: at 22:16
2017-08-11 22:16:00.335 Error: dzVents: Error: An error occured when calling event handler HomeTest
2017-08-11 22:16:00.335 Error: dzVents: Error: .../domoticz/scripts/dzVents/generated_scripts/HomeTest.lua:10: attempt to call global 'UnifiController' (a nil value)
2017-08-11 22:16:00.335 dzVents: Info: ------ Finished HomeTest
2017-08-11 22:16:00.336 Error: EventSystem: Lua script UnifyController did not return a commandArray

What am I doing wrong?
vrmp
Posts: 18
Joined: Friday 21 July 2017 23:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8100
Location: Netherlands
Contact:

Re: Unifi controller - Presence detection

Post by vrmp »

I'm not using dzvents but the normal lua code.
When you separate the function to a different file using dzvents you should use the helper function:
https://github.com/dannybloe/dzVents/issues/28

Perhaps you can also place the function in your dzvents code block, but I'm not sure of that.
JimmyH1969
Posts: 153
Joined: Tuesday 28 June 2016 16:38
Target OS: Windows
Domoticz version: Beta
Location: Amersfoort NL
Contact:

Re: Unifi controller - Presence detection

Post by JimmyH1969 »

Hi,

Does not seem to work on my system, this is what i see in the log

2017-08-12 13:46:49.188 Error: EventSystem: in Unify: [string "function UnifiController(Username) ..."]:26: attempt to index global 'file' (a nil value)

That's like it cant open/create a file i think?

I'm running Domoticz on a Windows 7 system and have Curl installed, could Windows be the cause?

:(
vrmp
Posts: 18
Joined: Friday 21 July 2017 23:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8100
Location: Netherlands
Contact:

Re: Unifi controller - Presence detection

Post by vrmp »

That could be the problem. Did you replace these settings with a valid path on your Windows system?

cookie = "/tmp/unifi_cookie_lua" -- Temp cookie file
tempfilename = "/tmp/UnifiController.tmp" -- Temp JSON output file
JimmyH1969
Posts: 153
Joined: Tuesday 28 June 2016 16:38
Target OS: Windows
Domoticz version: Beta
Location: Amersfoort NL
Contact:

Re: Unifi controller - Presence detection

Post by JimmyH1969 »

Changing it to a valid path works, but now i'm getting this message in the UnifiController.tmp

{ "data" : [ ] , "meta" : { "msg" : "api.err.LoginRequired" , "rc" : "error"}}
vrmp
Posts: 18
Joined: Friday 21 July 2017 23:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8100
Location: Netherlands
Contact:

Re: Unifi controller - Presence detection

Post by vrmp »

Is the file in the cookie variable also created?
Without this file the login would fail as you describe.
Can you check that?
JimmyH1969
Posts: 153
Joined: Tuesday 28 June 2016 16:38
Target OS: Windows
Domoticz version: Beta
Location: Amersfoort NL
Contact:

Re: Unifi controller - Presence detection

Post by JimmyH1969 »

yes it is, the contents:

# Netscape HTTP Cookie File
# https://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
vrmp
Posts: 18
Joined: Friday 21 July 2017 23:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8100
Location: Netherlands
Contact:

Re: Unifi controller - Presence detection

Post by vrmp »

Can you remove the logout part from the function and try again?

Code: Select all

read_logout = os.execute(url_logout)
JimmyH1969
Posts: 153
Joined: Tuesday 28 June 2016 16:38
Target OS: Windows
Domoticz version: Beta
Location: Amersfoort NL
Contact:

Re: Unifi controller - Presence detection

Post by JimmyH1969 »

That results in the same error in the UnifiController.tmp
If i remove the second line

Code: Select all

read_open = os.execute(url_open)
and only leave the first line, the "UnifiController.tmp" is not created or modified. I guess there is something wrong with the first line.

If i start that command from a prompt, like this:

Code: Select all

curl --cookie unifi_cookie_lua --cookie-jar unifi_cookie_lua --insecure -H 'Content-Type: application/json' -X POST -d '{"password":"admin","username":"admin"}' https://192.168.1.12:8443/api/login
Then the result is:

Code: Select all

curl: (6) Couldn't resolve host 'application'
{ "data" : [ ] , "meta" : { "msg" : "api.err.Invalid" , "rc" : "error"}}
vrmp
Posts: 18
Joined: Friday 21 July 2017 23:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8100
Location: Netherlands
Contact:

Re: Unifi controller - Presence detection

Post by vrmp »

Can you replace the ' with " in you manual command?
I think Windows does not recognize this and assumed after the space the next option is given, which is application in this case.
JimmyH1969
Posts: 153
Joined: Tuesday 28 June 2016 16:38
Target OS: Windows
Domoticz version: Beta
Location: Amersfoort NL
Contact:

Re: Unifi controller - Presence detection

Post by JimmyH1969 »

I tried and get this.

{ "data" : [ ] , "meta" : { "msg" : "api.err.Invalid" , "rc" : "error"}}

I do think it's a commandline issue. I just can't seem to get the command right :x
Last edited by JimmyH1969 on Sunday 13 August 2017 21:42, edited 1 time in total.
vrmp
Posts: 18
Joined: Friday 21 July 2017 23:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8100
Location: Netherlands
Contact:

Re: Unifi controller - Presence detection

Post by vrmp »

I will have a look at it tomorrow on a Windows system
JimmyH1969
Posts: 153
Joined: Tuesday 28 June 2016 16:38
Target OS: Windows
Domoticz version: Beta
Location: Amersfoort NL
Contact:

Re: Unifi controller - Presence detection

Post by JimmyH1969 »

Thanks, would be great to have this working. :D
vrmp
Posts: 18
Joined: Friday 21 July 2017 23:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8100
Location: Netherlands
Contact:

Re: Unifi controller - Presence detection

Post by vrmp »

Can you try this

Code: Select all

curl --cookie unifi_cookie_lua --cookie-jar unifi_cookie_lua --insecure -H "Content-Type: application/json" -X POST -d "{\"password\":\"admin\",\"username\":\"admin\"}" https://192.168.1.12:8443/api/login
JimmyH1969
Posts: 153
Joined: Tuesday 28 June 2016 16:38
Target OS: Windows
Domoticz version: Beta
Location: Amersfoort NL
Contact:

Re: Unifi controller - Presence detection

Post by JimmyH1969 »

tried, no succes :?

Code: Select all

{ "data" : [ ] , "meta" : { "msg" : "api.err.Invalid" , "rc" : "error"}}
vrmp
Posts: 18
Joined: Friday 21 July 2017 23:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8100
Location: Netherlands
Contact:

Re: Unifi controller - Presence detection

Post by vrmp »

I check some stuff and this works for me on my Windows system:

Code: Select all

curl --cookie unifi_cookie_lua --cookie-jar unifi_cookie_lua --insecure -H "Content-Type: application/json" --data "{\"password\":\"admin\",\"username\":\"admin\"}" https://192.168.1.12:8443/api/login
curl --cookie unifi_cookie_lua --cookie-jar unifi_cookie_lua --insecure -s -o test.txt --data "json={}" https://192.168.1.12:8443/api/s/default/stat/sta
This should create a file test.txt with the JSON output as expected.
Can you make the needed changes for you Windows system in the function to create the above yourself or can I help you with it?

Nevermind, here it is:

Code: Select all

url_login = 'curl --cookie ' .. cookie .. ' --cookie-jar ' .. cookie .. ' --insecure -H "Content-Type: application/json" --data "{\"password\":\"' .. unifi_password .. '\",\"username\":\"' .. unifi_username .. '\"}" https://' .. unifi_controller_ip .. ':' .. unifi_controller_port .. '/api/login'
Hopefully it will work now on your system :D
parkerc
Posts: 6
Joined: Sunday 24 April 2016 17:52
Target OS: -
Domoticz version:
Contact:

Re: Unifi controller - Presence detection

Post by parkerc »

Hi

Thanks so much for sharing this.

I would really like to be able to use this too and run it on a Linux box.

Are the file locations important? Sadly I'm not a programmer but would it be possible for the code to create the required files if they are present ?

Could this script also be used to identify if any remote vpn users are connected?
vrmp
Posts: 18
Joined: Friday 21 July 2017 23:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8100
Location: Netherlands
Contact:

Re: Unifi controller - Presence detection

Post by vrmp »

No problem!

It should work on Linux without any issue.
The files are created by the script, but the path where it does should be available.
On Windows the path /tmp is not available but on Linux it is (I'm using a Pi for my Domoticz installation)

I think it will also work for VPN users, but I did not test this.
When the VPN user is connected I think that account will be listed under clients?
If so you can use the name that is shown there or set an alias to the account.
You can use that name in the lua code to get the status.
JimmyH1969
Posts: 153
Joined: Tuesday 28 June 2016 16:38
Target OS: Windows
Domoticz version: Beta
Location: Amersfoort NL
Contact:

Re: Unifi controller - Presence detection

Post by JimmyH1969 »

Yes, it works. I had to modify it like this

Code: Select all

curl --cookie-jar unifi_cookie_lua --insecure -H "Content-Type: application/json" -X POST -d "{\\"password\\":\\"Admin\\",\\"username\\":\\"Admin\\"}" https://192.168.1.12:8443/api/login
Using dubble backslashes did the trick i guess!

Thanx for all the help! :D
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest