[Release] UniFi Presence Sensor


#1

I've created a Unifi Presence sensor that will log into your Unifi controller and check to see whether specified mac addresses are connected, and then update a Hubitat Elevation virtual switch. A virtual switch is currently required, as MakerAPI does not allow modification of virtual presence sensors. The workaround is (old details removed) use Cobra's DH that has both on/off and presence/absence built in. And skip step 9.

Requirements: access to UniFi controller (local works, not sure about cloud controllers), python script (works on 2.7.x, not tested others)

Files are here

Instructions

  1. Create a virtual switch for each phone you wish to monitor. Note the device ID of each device. eg; http://Hubitat IP/device/edit/601
  2. Enable MakerAPI, and select the virtual switch(es) you created in step 1.
  3. Note the MakerAPI app ID. The three digit number in the address
    eg: http://Hubitat IP/installedapp/configure/700
  4. Note the MakerAPI access token on that page
    eg: 99f0ssdd-bffff-4fffff-bf34rrrrrdssdfrrr
  5. Download and save the files (in their final location) on a device that runs python scripts (2.7.x tested). Note the location and enter it in step 6e.
  6. Edit 'unifipresence.conf', edit the following fields:
    6a. Hubitat local IP address inside the double quotation marks.
    6b. Maker API from step 3 above, inside single quotations.
    6c. Unifi controller IP address (have only tried local, not cloud controller). Include the port number inside single quotation marks.
    6d. Unifi user name and password inside single quotations. Recommend to create a user with read only access, as this file records the user name and password for UniFi controller. Leave SiteUniFi as default
    6e. Filepath of these files inside single quotations. Two more files are created upon first run of the python script. Enter the path before the filename of the two files. (These files are not yet created, and will be done so at step 8).
    6f. (Inside single quotations) Enter mac Address of phone, Device ID (from step 1), and Access token of MakerAPI (from step 3). The Access token will remain the same for each device.
  7. Edit the python script unifipresense.py with the location of the conffile (unifipresence.conf).
  8. Run the python script to ensure creation of two files unifipresence.cookie and unifipresence.status.
  9. Create two rules in Rule Machine for each device to sync status of switch with virtual presence sensors.
  10. Call/run the python script every 1 minute.

Error checking
Presence will only update to Hubitat if the python script notices a change. It does not send presence (on/off) on no change.

If presence is not updating, open unifipresence.status to determine if script has checked Unifi controller successfully. File will update with devices connected. If not, check Ip address + port, password, username, location of files.

If presence is updating in unifipresence.status, check HE side of script etc.

George.


Unifi Presence (help with python)
#2

@Keo
@aaron


#3

@mike
I have a virtual device driver which is both a presence sensor and a switch if you would like to try it.
That way you can get your app to turn on the virtual device which will then be also ‘present’
This will get rid of the need to use any rules and a separate virtual presence sensor

The driver is here:

Andy


#4

That's a slick idea right there. Thanks


#5

You are welcome
I’ve got lots of ‘slick’ ideas.. just most of them are ridiculed by the wife :slight_smile:


#6

I am using @cobra device driver. Flipping awesome!


#7

Not mine originally, I ported most of it from ST & just tweaked it a bit.
I was thinking to add the capability to be a contact sensor and a water sensor too but never found a use for it :slight_smile:

Andy


#8

how long does it take, for you, to detect a device that has left? My UniFI controller seems to keep devices in its list for 6-8 mins after they leave.


#9

Interesting. Based on this I was looking to see if its possible to do something similar with pfSense. For now its not easy, but apparently in v3 of pfSense they will have a rest api and this may be possible.


#10

mine does not seem to be updating. I turned off the wifi on my phone, it has fallen off the list on the unifi controller, but its still reporting me as present.

if I delete the the "unifipresence.status" file and force the script to recreate it, it reports me as present even though my phone is not on the wifi. this seems to be a unifi issue, i suppose.

Does anyone know if there is a way to tweak how long it hangs on to old clients in the controller settings?

edit: finally switched me to "not present" after about 45-50 min.


#11

Is the status file showing you as absent?

Remember script will only update status if the script sees a change. Simply changing status on hubitat to present or away does nothing as the script doesn't know.


#12

The status file shows me as present, so it isn’t updating Hubitat.

It seems like the UniFi controller isn't updating my status.


#13

One thing I notice with any sort of network presence sensing is that when an iPhone goes to sleep it drops off the network (turns off wifi) and if you don't account for this you will get false away notifications. I don't know if android does the same thing.

I don't know if this method of checking online is also affected by this. If you use a script to ping it will stop replying to ping.

The sweet spot I found was about 15 minutes. I've tried this on an iPhone 7+/8 and 5s. I setup a notification to notify my over night and found out they would drop off the network for up to 15 minutes and then check in quickly before dropping off again. So my logic was basically "if reply's then its online, if not reply then keep trying for 15 minute until it replies and if not then its offline".

I also didn't find it fully reliable to so I pair up network presence and geolocation together and have had no false positives with presence. Been working perfectly.

The last thing I wanted was my house to go into away mode (arming the alarm, turning down the thermostat and randomly adjusting lights) while I am home.


#14

It is possible to turn off the "wifi auto sleep" function on android.

Main issues are UniFi holding onto phone connection for 3-4 minutes after leaving wifi network. I've not figured out a way to get away to work reliably, for garage doors etc.


#15

Can you share that ping code? I've noticed it's best to have multiple presence sensors and first past the post to report changes.....


#16

how do you make this run every minute? I added it to cron but not sure how to make it repeat


#17

Windows task scheduler.


#18

I am running it on a raspberry pi. I'll have to search around more.


#19

Let me know how you go because I've got a spare pi.


#20

This looks pretty similar to what was done in ST a couple years ago.

https://github.com/0io/UniFiPresence

Been using it for awhile and glad to see someone hubified it already. Wifi presence is helpful for things like knowing we've got a babysitter, but I don't love it for mode automation....iphone battery depeltes, upgrade the phone, reboot, etc. Still searching for the hubitat compliment to a car arrival sensor but I'll definitely be using this! Thanks.