Ubiquiti UniFi - Plugin Potential?

Having owned UniFi Wifi access points for a while, I’ve started to explore ways to potentially integrate them with Vera. Thankfully various bits have be posted online already to understand the (unpublished) APIs.

Due to my own limited time, it’ll take me a while to get very far, so I’m starting this post up as a reference for related information in the hope that enough interest might exist within this community to explore a plugin or even just some code that could help people to see I) who are connected to your home network (and depending on how many APs you have) which room/area they are connected to…

Note: This is for the Ubiquiti UniFi product range/controller.

Ubiquiti API (unifi_sh_api), based on the latest release of the controller. (Must use the right one)
https://community.ubnt.com/t5/UniFi-Updates-Blog/UniFi-4-8-15-is-released/ba-p/1531713

The following has a Lua script and a bash file .
https://www.domoticz.com/forum/viewtopic.php?t=3703

This one looks to be in Python and is an occupancy checkers.

Bash script to display a list of users
http://community.ubnt.com/t5/UniFi-Wireless/unifi-api-display-list-of-users/m-p/1306967/highlight/true#M112977

UniFi API in Python

UniFi API in Go

UniFi API-Browser

Other articles of interest.
https://community.ubnt.com/t5/UniFi-Wireless/How-can-I-know-all-uris-and-commands-of-the-UniFi-API/m-p/1304089#U1304089
http://community.ubnt.com/t5/UniFi-Wireless/unifi-api-display-list-of-users/m-p/1306967/highlight/true#M112977

I have already started a Ubiquiti plugin … but have not finished it. Still has some problems.
I have a couple of wall plugs I want to use, where Z-Wave does not have enough range.
But I was going to test some Gen-5 wall blugs first and an antennae on my Plus before I decide to deploy the Wifi plugs.

Thanks @RTS

Are the wall plugs Mifi? Are they any good?

I’ve been playing (badly) with some bash scripts today, and also looking at the web browser (API) version that has been created too, and can see there’s certainly potential here for Vera - things like…

  • Zonal awareness - who is connected to which access point, including movement between them.
  • Discovery triggers - when certain devices (people) are connected
  • Dashboard - Wifi status - Number of devices connected, data transfers (up/down) rates,

Plus other potential cool stuff too, like maybe blocking certain devices at certain times, or when certain variables are in play… Oh and there maybe some scope for the Unifi phones too, which could be an interesting addition (although I don’t have one)

I’ll try to keep adding links to my first post, that might help you/the community, and also try to find some time the week after next to do some more reverse engineering via some chrome browser add-ons

If you need some testers for your alpha/beta plugin, when you are at a point to share, if I can, I’d be happy to help.

We have a couple of the mFi Pro 8-outlet guys, one controlling the lights for a display case and one powering the stuff in our server enclosure. I wrote a simple Vera plugin for the mFi power outlets and created virtual devices for the display case outlets. We have one of the minis that Vera is plugged into, so we can power-cycle that from afar. Linda has a printer plugged into a mini, and we’ve written a simple cron-driven script to have the printer print a page if the thing has been on standby for some length of time, so she doesn’t have to clean the inkjet heads every time she wants to print something for real.

We have found them to be reliable. Wish their controller solution used something (maybe “anything”) but MongoDB for data, but nothing is perfect.

And we love the UniFi WAPs…may upgrade ours to AC (from N) this summer.

–Richard

Thanks rlmalisz

Have you got any scripts etc. that you can share, ones that will work with the Unifi access points ?

Ok, I’ve been looking at the Unifi API, and numerous examples and have created my own bash script to authenticate and receive a list of the devices from the controller. It pulls back a JSON file with all the details.

[code]#!/bin/sh

define required variables

username=username
password=password
baseurl=https://192.168.1.111:8443

get site name from controller url

https://192.168.1.111:8443/manage/s/default/dashboard

site=default

cookie=/tmp/unifi_cookie

curl_cmd="curl --tlsv1 --silent --cookie ${cookie} --cookie-jar ${cookie} --insecure "

unifi_login() {
# authenticate against unifi controller
${curl_cmd} --data “{‘username’:‘$username’, ‘password’:‘$password’}” $baseurl/api/login
}

unifi_logout() {
# logout
${curl_cmd} $baseurl/logout
}

unifi_requires() {
if [ -z “$username” -o -z “$password” -o -z “$baseurl” -o -z “$site” ] ; then
echo "Error! please define required env vars before including unifi_sh. E.g. "
echo “”
echo “export username=ubnt”
echo “export password=ubnt”
echo “export baseurl=https://localhost:8443
echo “export site=default”
echo “”
return
fi
}

stat/sta

unifi_list_sta() {
${curl_cmd} --data “json={}” $baseurl/api/s/$site/stat/sta
}

unifi_login
unifi_list_sta
unifi_logout
[/code]

Here’s an example of the list of information that’s returned for a single device registered against the controller. So you can see the potential we have for a plugin for Vera. Not only for the detection of an individual’s device “Hostname”, the strength of their connection “signal”, which access point they are connected to.

[code]
This is the information return by this script for all connected devices.

“_id” : “52ba3710a5563sgt22324846” ,
“_is_guest_by_uap” : false ,
“_last_seen_by_uap” : 1462043449 ,
“_uptime_by_uap” : 1725 ,
“ap_mac” : “dc:tg:db:5s:0c:e9” ,
“assoc_time” : 1462028506 ,
“authorized” : true ,
“bssid” : “dc:9f:sr:b5:y5:20” ,
“bytes-r” : 83 ,
“ccq” : 0 ,
“channel” : 11 ,
“essid” : “HOMEWIFI01” ,
“first_seen” : 1455044367 ,
“hostname” : “TestDevice” ,
“idletime” : 5 ,
“ip” : “192.168.1.178” ,
“is_guest” : false ,
“is_wired” : false ,
“last_seen” : 1462043449 ,
“latest_assoc_time” : 1462041724 ,
“mac” : “28:ty:6a:tg:8c:ed” ,
“noise” : -87 ,
“oui” : “Apple” ,
“powersave_enabled” : true ,
“qos_policy_applied” : true ,
“radio” : “ng” ,
“radio_proto” : “ng” ,
“roam_count” : 6 ,
“rssi” : 41 ,
“rx_bytes” : 22619144 ,
“rx_bytes-r” : 76 ,
“rx_packets” : 132082 ,
“rx_rate” : 24000 ,
“signal” : -46 ,
“site_id” : “51295a5636fgd4701501402a” ,
“tx_bytes” : 54722579 ,
“tx_bytes-r” : 6 ,
“tx_packets” : 79658 ,
“tx_power” : 41 ,
“tx_rate” : 144444 ,
“uptime” : 14943 ,
“user_id” : “52ba3710a52e5268372324846”} ,[/code]

I’m going to try and capture all the API URLs

For many there’s much more to do with these APIs and attributes you can add, but the goal here is just to capture some of the basics URL structure.

$baseurl = https://UnifiURL:8443/
$site = default

Statistics/Reporting

$baseurl/api/s/$site/stat/sta
$baseurl/api/s/$site/stat/report/daily.site
$baseurl/api/s/$site/stat/sessions

List

$baseurl/api/s/$site/list/networkconf

Actions

‘cmd’:‘kick-sta’ - $baseurl/api/s/$site/cmd/stamgr
‘cmd’:‘block-sta’ - $baseurl/api/s/$site/cmd/stamgr
‘cmd’:‘unblock-sta’ - $baseurl/api/s/$site/cmd/stamgr
‘cmd’:‘delete-voucher’ - $baseurl/api/s/$site/cmd/hotspot

Others

$baseurl/logout

Hi parkerc, how are your luau skills?

I have made this bash script ( http://forum.micasaverde.com/index.php/topic,24084.msg275431.html#msg275431) which runs on a machine I have running anyway 24/7. This was pure trail and error but got 8t working. If someone can re-create that in LUA and make a plugin out of it no extra machine is needed and any logic can be much easier integrated. I noticed that the Google onHub now has If this then that integration, this co u let work similar but then much better integrated with all the existing logic and devices.

Hi @tyfoon

Sadly I have no coding skills, I’m a code scavenger, finding bits and pieces from here and there and trying to make them work - more brute force than anything else :slight_smile:

Ideally the code would be in Lua, however until someone more competent that us ( :wink: ) can pick this up, I’m looking to see if I can run the bash scripts on Vera via a scene. As I seem to recall you can do things like ‘os.execute’ which will run command line actions etc. like curl.

What would you like to see in a UniFi plug in ?

I like the person detection/proximity potential, it seems more personal than 3rd party geolocation etc.

I’ve got 3 Unifi AP’s with the controller running on my QNAP - I would be happy to help with the testing effort/functional requirements.

Key use for me would also be the geolocation function. Currently using DHCP reservations and ping sensors which works well enough most of the time.

Would also like to be able to block/unblock the kids iDevices when they annoy me. It’s almost like cutting off their air. :wink:

When you say “geolocation”, is it near from the router (in the LAN) or a location in the house (with triangulation) ?

At one end of the spectrum is just presence - is device X on the wireless network.

If you have multiple AP’s you can use the associated AP and signal strength to make some judgement on where a device is. Not really triangulation as you only have an RSSI for the AP you’re associated to (I think).

That’s a good coincidence as I have the same set up :slight_smile:

There is a lot of great data that can be extracted from the Unifi API’s - I’ve done a few basic things using the bash scripts - but to really integrate it - needs someone with Vera/Lua skills to take this up.

Hello,
I’ve bought an Unifi AP LR and discovered this topic.

Thanks for the links.
I’ve tried to use the API and there’s a lot of informations that could be used in Home Automation.

I’m finishing my plugin for the Synology Surveillance Station and I will look what can be done for Ubiquiti Unifi (it’s almost the same code).

How do you make your rules/scenarios ? Do you use :

  • Variable watching ?
  • Standard scene editor and predefined events on devices ?

Hi @vosmont

How are things going ?

Since posting this, I’ve added the UniFi Secure Gateway (USG) too - with gives me green across the board on the UniFi Controller.

Having this included in any future plugin/app would be great as it provides my external IP, latency to the Internet and my through put - which could allow Vers to do some cool babdwigth monitoring.

Due to my lack of Lua skills and understanding - I’m still working with Bash scripts and the Workflow iOS app (I really love that app) - so I’m not doing anything with watched variables or scenes.

Another feature I like on the UniFi set up is that it reports roaming users so it can tell you when someone has moved from on Access Point to another .

BTW, we just updated our Unifi ACs from B/G/N to B/G/N/AC, and have two of the former, one LR and one not, that we could cheerfully sell cheaply…like $20 each, buyer pays shipping, if anyone is interested. Don’t have the original boxes, but they would come with the POE injectors and the plastic wall mounts. Pop me a message if interested. They’re great access points, but a little nerdy to sell locally to the general populace.

–Richard

[quote=“parkerc, post:16, topic:192116”]Hi @vosmont

How are things going ?

Since posting this, I’ve added the UniFi Secure Gateway (USG) too - with gives me green across the board on the UniFi Controller.

Having this included in any future plugin/app would be great as it provides my external IP, latency to the Internet and my through put - which could allow Vers to do some cool babdwigth monitoring.

Due to my lack of Lua skills and understanding - I’m still working with Bash scripts and the Workflow iOS app (I really love that app) - so I’m not doing anything with watched variables or scenes.

Another feature I like on the UniFi set up is that it reports roaming users so it can tell you when someone has moved from on Access Point to another .[/quote]

Hello,

the plugin is still in the todo list …
I’ve almost done the setting of my EdgeRouter + Unifi (VLAN + QOS) : it’s fantastic ! ;D

I hope that I could start this plugin soon

Hi @RTS

[quote=“RichardTSchaefer, post:2, topic:192116”]I have already started a Ubiquiti plugin … but have not finished it. Still has some problems.
I have a couple of wall plugs I want to use, where Z-Wave does not have enough range.
But I was going to test some Gen-5 wall blugs first and an antennae on my Plus before I decide to deploy the Wifi plugs.[/quote]

Just wondering if you have made any more progress with your Ubiquiti plugin.

I’m doing basic things with my Unifi access points via ssh, but nothing as polished as a plug-in could be - it would be cool to have something simple in the Vera UI, that shows which named access point you are connected to, when you last connected etc.

I would love to see a plug-in for Unifi too! It can even be used to check how many people are home (based on the wifi clients connect), and trigger scenes based on client wifi connections.