[PROJECT] Driver for Unifi Network Controllers

You have the controller type in the driver set for "Other Unifi Controllers" correct?

Sorry if I am asking basic questions... there are really not that many things to go wrong with it at that stage unless the API changes. My UDMP is at 6.0.43 (the latest in the Official release channel). Hopefully 6.0.45 did not change things for some reason, although the Unifi sample page for the API shows no changes at all from 6.0.41 (the last I had bookmarked) to the 6.0.45.

Yes Other
Strange thing is, I deleted all the settings and added them back (exactly the same) and now it logged in. Nothing different so not sure what fixed it or what was different.
Hopefully will stay this way

P.S. Please can I ask why it would need me to login every few days even when it was working.

Thanks for looking at this.

1 Like

When you save your preferences the driver automatically schedules a Login (you should see it in the Scheduled Jobs list at the bottom of the device page) to run every day automatically. That usually seems to handle the token timeout (the driver gets a token from the controller upon successful login and then does not use the username/password again until another Login attempt).

However... the token sometimes seems to expire otherwise (I have not figured out why). That is why I left the Login command available, in case it is needed. I tried an automatic Login method (if authentication failed, try Login, then redo the command) with this early on (and also on my BlinkAPI driver) but it seemed to cause more errors than it saved so I changed them to just use this scheduled method.

Long story short:
It will try to do a Login every day, based on when you Saved Preferences.
Sometimes tokens expire otherwise (early) and Login is needed.

Thanks for letting me know it works again. If you figure out anything that seems to indicate why it didn't... please let me know so I can try to improve on the driver.

1 Like

Have you thought about adding this to the Hubitat Package Manager? I made the mistake of assuming the Unifi drivers package in there was yours until I could not get it to work!

I have thought about it. Right now none of my drivers are in the HPM.

1 Like

@snell - just a heads-up on this, the latest update for the CloudKey 2+ changes the various URL's from https://ip.address.goes.here:8443/ to https://ip.address.goes.here/network - the good news is that this makes the platform compatible with the UDMP and updating the controller type from the dropdown works perfectly.

Thought you'd like to know... :slight_smile:

1 Like

That is excellent news (not that they changed it, but that it still works). Hopefully people will notice your post and maybe in the near future I can change the types to be "Most Controllers" and "Legacy Controllers" or something... but for now, better to leave it as is I bet.

1 Like

Thank you for your work on this. I just installed 0.2.3 and I'm able to log in successfully, Everything appears to be working but no matter what I do, I cannot get a successful Presence Check or MACExists command. The command runs but the client is not found. I can verify the MAC on the controller itself, copy/paste into the driver and still not found. Interestingly, Unifi controller reports 63 total clients but your driver only reports 17. Does this work over vLans? I have several.

The driver is only looking at what the controller itself sends back to it. I do not have any idea how that would work over vLans though as I do not have any myself (and no samples including them). If you do not mind, could you set your logging to Trace and send me a PM with the log when you run a refresh? You can redact any info you want before sending it to me. Although it would be useful if you see the MAC address you are looking for in the log, if you could highlight it (you can leave just the first couple octets of it if you want to redact the rest, just so I can see it actually is in there and if there is anything different about the formatting).

I set to Trace and ran a refresh. This is the error I receive:

2021-02-23 09:10:39.130 am [error]org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'null' with class 'null' to class 'int'. Try 'java.lang.Integer' instead on line 575 (ReceiveData)

I also ran a MACExists command on my iPhone with the mac pulled directly from my Unifi Controller (v6.0.28). Still recieve:

Current States

  • Alarms : []
  • Last Login : Tue Feb 23 08:30:55 PST 2021
  • Last Refresh : Tue Feb 23 06:43:08 PST 2021
  • Site : default
  • Status : CurrentStats successful.
  • Total Clients : 17
  • Version : Up to date
  • wlan-Health : unknown
  • Last Refresh : Tue Feb 23 09:08:54 PST 2021
  • Last Refresh : Tue Feb 23 09:10:16 PST 2021
  • Last Refresh : Tue Feb 23 09:10:24 PST 2021
  • Last Refresh : Tue Feb 23 09:10:38 PST 2021
  • MAC Exists Result : (redacted mac) does not exist as of Tue Feb 23 09:11:10 PST 2021

Apparently I didn't hit "save" when I set to Trace. Let me try again:

2021-02-23 09:16:49.324 am [error]org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'null' with class 'null' to class 'int'. Try 'java.lang.Integer' instead on line 575 (ReceiveData)

[dev:665] 2021-02-23 09:16:49.289 am [trace] Unifi Controller - Event: wlan-Health = unknown

[dev:665] 2021-02-23 09:16:49.285 am [trace] Unifi Controller - CurrentStats Data: {"meta":{"rc":"ok"},"data":[{"_id":"59c31381e4b0a8f2a024084d","name":"default","desc":"Orting","attr_hidden_id":"default","attr_no_delete":true,"anonymous_id":"2c5de7b5-f392-4126-af24-e4ecb1d6e0d0","health":[{"subsystem":"wlan","status":"unknown","num_ap":0,"num_adopted":0,"num_disabled":0,"num_disconnected":0,"num_pending":0},{"subsystem":"wan","num_gw":0,"num_adopted":0,"num_disconnected":0,"num_pending":0,"status":"unknown"},{"subsystem":"www","status":"unknown"},{"subsystem":"lan","status":"unknown","num_sw":0,"num_adopted":0,"num_disconnected":0,"num_pending":0},{"subsystem":"vpn","status":"unknown"}],"num_new_alarms":0},{"_id":"5b037b15e4b0d2923a9a4156","desc":"Graham","name":"zadto44g","health":[{"subsystem":"wlan","num_user":15,"num_guest":0,"num_iot":0,"tx_bytes-r":3152,"rx_bytes-r":2896,"status":"ok","num_ap":4,"num_adopted":4,"num_disabled":0,"num_disconnected":0,"num_pending":0},{"subsystem":"wan","num_gw":0,"num_adopted":0,"num_disconnected":0,"num_pending":0,"status":"unknown"},{"subsystem":"www","status":"unknown"},{"subsystem":"lan","status":"ok","num_user":49,"num_guest":0,"num_iot":0,"tx_bytes-r":3550872,"rx_bytes-r":9932247,"num_sw":4,"num_adopted":4,"num_disconnected":0,"num_pending":0},{"subsystem":"vpn","status":"unknown"}],"num_new_alarms":3}]}

[dev:665]2021-02-23 09:16:49.270 am [trace] Unifi Controller - Event: Status = CurrentStats successful.

[dev:665] 2021-02-23 09:16:49.262 am [trace] Unifi Controller - CheckAlarms Data: {"meta":{"rc":"ok"},"data":[]}

[dev:665] 2021-02-23 09:16:49.258 am [trace] Unifi Controller - Event: Status = CheckAlarms successful.

[dev:665] 2021-02-23 09:16:49.202 am [trace] Unifi Controller - Event: Last Refresh = Tue Feb 23 09:16:49 PST 2021

[dev:665] 2021-02-23 09:16:49.173 am [trace] Unifi Controller - Parameters = [192.168.10.203:8443/api/s/default/rest/alarm?archived=false, ignoreSSLIssues:true, requestContentType:application/json, contentType:application/json, headers:[Host:192.168.10.203:8443, Accept:/, Cookie:unifises=aJpAAiax5INjcrT4jOPUjprkQxd9XTXd]]

[dev:665] 2021-02-23 09:16:49.168 am [trace] Unifi Controller - Parameters = [192.168.10.203:8443/api/stat/sites, ignoreSSLIssues:true, requestContentType:application/json, contentType:application/json, headers:[Host:192.168.10.203:8443, Accept:/, Cookie:unifises=aJpAAiax5INjcrT4jOPUjprkQxd9XTXd]]

Ok. So the error at 575 means that it did not get any value for the number of users connected to the WLAN (the controller returned a null for that field instead of a 0, something I did not expect, so the line errored out). That error would prevent it from getting further through that area for you. I will have to fix that.

In the Trace you sent... your Current Stats data shows no users on the first section... But the SECOND area has ~60. So it looks like the controllers break up the segments in the data, which is why it is nowhere near the right number for you. It could even be why the presence is not working. I will definitely have to work on this (no way I am getting it done tonight though).

If you could check your Past Logs after the Daily Check is scheduled to run... It would be useful to see if that data is also getting broken up.

Thank you very much for this logging though! I can try to make this better for everyone as a result.

Updated Version(s):

  • UnifiAPI.groovy = 0.2.4

Change(s):

  • First attempt at handling multiple-site setups. I do not have one myself (and REALLY do not want to mess up my home network playing with this type of thing) so this is based on what little logs I have seen so far. It SHOULD iterate through if the controller reports multiple sites and sum up things like user counts and such. It will also attempt to put status information for each site, naming them, if there is more than one.
  • Changed job scheduling to be cron-based, so please run Save Preferences after updating to get the new schedules (although it really should not change anything).
  • Tried to make MAC Exists and Presence Detection work across multiple sites... again by iterating through whatever the controller responds with but I do not know how this will work.

Different/Related Topic(s):

  • If anyone is willing to send me sample Trace logs running all the major commands across their controller (including when there is a DailyCheck)... I would appreciate it as additional sample data to compare against mine. Feel free to redact anything you want from it of course.

This is a great driver. Thank you @snell for developing it and making it available. I have previously tried two other Unifi drivers for Hubitat, but this is the first one that has successfully achieved login. I plan to use it for presence detection without family members having to install software on their phones. And since the car also uses wifi, perhaps I can use this app to detect when the car is "home".

Interestingly, for the first few hours. I noticed no child devices were being created. On the virtual device, Hubitat reported:

Current States

  • Last Login : Sat Mar 06 13:56:27 CST 2021
  • Last Presence Check : Sat Mar 06 13:56:03 CST 2021
  • Online Clients : 0
  • Status : PresenceCheck successful.
  • Unifi Devices : -1

and errors were appearing in the log:

[dev:2913](http://10.9.0.10/logs#dev2913)2021-03-06 13:55:58.309 [error](http://10.9.0.10/device/edit/2913)java.lang.IllegalArgumentException: The JSON input text should neither be null nor empty. on line 509 (ReceiveData)

[dev:2913](http://10.9.0.10/logs#dev2913)2021-03-06 13:55:03.237 [error](http://10.9.0.10/device/edit/2913)java.lang.IllegalArgumentException: The JSON input text should neither be null nor empty. on line 509 (ReceiveData)

[dev:2913](http://10.9.0.10/logs#dev2913)2021-03-06 13:54:58.208 [error](http://10.9.0.10/device/edit/2913)java.lang.IllegalArgumentException: The JSON input text should neither be null nor empty. on line 509 (ReceiveData)

I switched the logging to Trace, and everything seemed to resolve itself as a result of that. Errors stopped appearing in the log, and Hubitat refreshed the virtual device:

Current States

  • CPU : 38.7
  • Last Login : Sat Mar 06 14:06:29 CST 2021
  • Last Presence Check : Sat Mar 06 14:17:39 CST 2021
  • Memory : 57.6
  • Online Clients : 78
  • Status : PresenceCheck successful.
  • Unifi Devices : 12
  • Uptime : 12 days 21 hours 1 minutes
  • lan-Health : ok
  • vpn-Health : unknown
  • wan-Health : ok
  • wlan-Health : ok
  • www-Health : ok

I'm sharing how it worked in my install in case others experience something similar. (Setup: UDM Pro on firmware 1.8.6, Unifi 6.0.43. Hubitat accesses the UDM Pro using credentials for a Limited Admin user with Read Only access to Unifi. Hubitat is on a different VLAN than Unifi, but a firewall rule allows Hubitat to initiate traffic to the UDM Pro.)

It's very elegant how child devices are assigned the same names as in Unifi. Looking forward to incorporating network presence into our rules. Thank you.

1 Like

Does this require the UDM, UDM Pro, or a USG? I have none of those. I use the EdgeRouter X but I do have a Unifi Controller running on a PC for 4 Unifi switches. I can't even get past the login.

What's the exact format for the Unifi Controller IP/Hostname in Preferences?

Feel free to speak to me like a networking infant. :smiley: I only switched over from TP-Link Deco M5s about 6 weeks ago.

I designed it for a UDM Pro and I know people have used it with a UDM and I think USG... but I do not know anyone else that has used it for an EdgeRouter. But it should work with the Unifi Controller running on the PC. When you were trying to connect were you going to the controller on the PC or the EdgeRouter?

Format for the Unifi Controller IP/Hostname:
#.#.#.#
Ex: 192.168.1.1

Hostname obviously can vary by what you might have set up in your local network. Most people do not use hostname on their local/home network but it does not really affect how I make the driver so I just leave it possible.

Edit:
I did a quick search and there has been SOME documentation made (not official like the Unifi API) on the EdgeOS's API-capabilities. I may be able to add support for it but it uses a fairly different structure so if I do add it, it will be pretty rough at first.

Just checking in about a couple things.
I have a USG with CloudKey2+ on 6.1.65 controller.

Only way this worked for me was setting to UDM.

Cpu says 0?

What exactly is "Alarms" for?

I also have 5 devices yet it says 4?

@markbellkosel84 let me go through the questions:

  1. If you can send me a Trace log across when you run a CurrentStats command I can check why the CPU might be zero. In the typical data (if it is reported) it usually comes across as the % with one decimal place (ex: mine is currently 12.0), Memory is the same (mine is 40.1 at the moment). Since your Current States values for those are lacking the decimal I wonder if it is being reported differently.
  2. Alarms is a list of any "alarms" the controller might currently have. An example would be if it detects a Rogue AP it will raise an alarm. That would show in here until you archive them at the controller.
  3. The number of Unifi devices is based on what I get in the CurrentStats command, so the Trace from #1 could help here also. Right now it adds them up based on what the controller has returned for the wan, lan, and wlan segments (I have no examples showing it with devices in vpn or www). If you do not mind telling... what Unifi devices do you have? Combined with the Trace I can try to see where I missed adding one in.
1 Like

I was trying every variation of the Unifi Controller's IP address.

Don't worry about it. I trying this as more of a curiosity than any need.

For the Edge router, the IP probably will not matter. The rest of the URL that the commands rely on is different. Worth adding in if there is demand for it.

1 Like