Python plugin: Ubiquiti Unifi Presence

Python and python framework

Moderator: leecollings

wizzard72
Posts: 116
Joined: Friday 20 December 2013 7:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Python plugin: Ubiquiti Unifi Presence

Post by wizzard72 »

I've created a plugin for presence detection. So when your phone is connected to the Unifi network it will be detected through its MAC address. Some phone can set random MAC addresses, avoid this and use the MAC address of the phone, otherwise it will not be detected.

It's tested with the Unifi Controller software installed on a Raspberry Pi. Probably it works with a Cloud Key too (not tested).

Image Image
Image Image

It's also posible to block a configured phone (configurable in the hardware settings):
Image Image

It's also posible to create a geofencing device (configurable in the hardware settings):
Image Image

And the plugin shows information about the unifi devices also. I have a gateway, switch and multiple access points. I didn't test with a different setup.
Image Image
Image Image
Image Image
Image Image

For more information: https://github.com/Wizzard72/Domoticz-Unifi-Presence
If you have a problem ask here first and if it's not solved create an issue on github.
Last edited by wizzard72 on Wednesday 25 May 2022 17:46, edited 1 time in total.
iganin
Posts: 47
Joined: Tuesday 28 October 2014 17:55
Target OS: NAS (Synology & others)
Domoticz version: 2020.2
Location: Aalter, Belgium
Contact:

Re: Python plugin: Ubiquiti Unifi Presence

Post by iganin »

I have got three Unifi APs. One of them in my garden. Is there a way to detect when my phone is connected to the AP which is located in my garden? The idea is to send a door bell to garden when i'm there.
rbosch
Posts: 11
Joined: Monday 18 January 2016 20:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: Python plugin: Ubiquiti Unifi Presence

Post by rbosch »

I have installed the plugin. Trying to use it with a Dream Machine Pro.
Something goes wrong as I get these errors:

Code: Select all

2020-12-07 23:52:04.897 Error: (Unifi Presence) ----> Line 897 in '/usr/lib/python3/dist-packages/requests/models.py', function json
2020-12-07 23:52:04.898 Error: (Unifi Presence) ----> Line 518 in '/usr/lib/python3/dist-packages/simplejson/__init__.py', function loads
2020-12-07 23:52:04.898 Error: (Unifi Presence) ----> Line 370 in '/usr/lib/python3/dist-packages/simplejson/decoder.py', function decode
2020-12-07 23:52:04.898 Error: (Unifi Presence) ----> Line 400 in '/usr/lib/python3/dist-packages/simplejson/decoder.py', function raw_decode
2020-12-07 23:52:14.868 Error: (Unifi Presence) 'onHeartbeat' failed 'IndexError':'string index out of range'.
2020-12-07 23:52:14.868 Error: (Unifi Presence) ----> Line 658 in '/home/pi/domoticz/plugins/Domoticz-Unifi-Presence/plugin.py', function onHeartbeat
2020-12-07 23:52:14.868 Error: (Unifi Presence) ----> Line 345 in '/home/pi/domoticz/plugins/Domoticz-Unifi-Presence/plugin.py', function onHeartbeat
2020-12-07 23:52:24.838 Error: (Unifi Presence) 'onHeartbeat' failed 'IndexError':'string index out of range'.
2020-12-07 23:52:24.838 Error: (Unifi Presence) ----> Line 658 in '/home/pi/domoticz/plugins/Domoticz-Unifi-Presence/plugin.py', function onHeartbeat
2020-12-07 23:52:24.838 Error: (Unifi Presence) ----> Line 345 in '/home/pi/domoticz/plugins/Domoticz-Unifi-Presence/plugin.py', function onHeartbeat
2020-12-07 23:52:34.858 Error: (Unifi Presence) 'onHeartbeat' failed 'IndexError':'string index out of range'.
2020-12-07 23:52:34.858 Error: (Unifi Presence) ----> Line 658 in '/home/pi/domoticz/plugins/Domoticz-Unifi-Presence/plugin.py', function onHeartbeat
2020-12-07 23:52:34.858 Error: (Unifi Presence) ----> Line 345 in '/home/pi/domoticz/plugins/Domoticz-Unifi-Presence/plugin.py', function onHeartbeat
azonneveld
Posts: 153
Joined: Wednesday 02 October 2019 7:37
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands
Contact:

Re: Python plugin: Ubiquiti Unifi Presence

Post by azonneveld »

This is very nice work!

I have not tested it, because I use homekit-homebridge for presence detection.

I was wondering, is it possible to toggle a portforwarding on/off using the the unifi api?
rpi4 - zigbee2mqtt - roborock - espeasy - rfxcom - homewizard p1 - otgw - homebridge - surveillance station - egardia - goodwe - open weather map - wol - BBQ detection - rsync backup
wkossen
Posts: 62
Joined: Friday 06 November 2020 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python plugin: Ubiquiti Unifi Presence

Post by wkossen »

iganin wrote: Friday 04 December 2020 22:50 I have got three Unifi APs. One of them in my garden. Is there a way to detect when my phone is connected to the AP which is located in my garden? The idea is to send a door bell to garden when i'm there.
Just a more or less unrelated answer, wouldn't using Owntracks as a way to determine location be helpful here? (unless you have a lot of gps drift) you could geofence your garden and house and other areas and get quite good approximations of where your phone is.
wizzard72
Posts: 116
Joined: Friday 20 December 2013 7:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python plugin: Ubiquiti Unifi Presence

Post by wizzard72 »

rbosch wrote: Tuesday 08 December 2020 0:03 I have installed the plugin. Trying to use it with a Dream Machine Pro.
Something goes wrong as I get these errors:

Code: Select all

2020-12-07 23:52:04.897 Error: (Unifi Presence) ----> Line 897 in '/usr/lib/python3/dist-packages/requests/models.py', function json
2020-12-07 23:52:04.898 Error: (Unifi Presence) ----> Line 518 in '/usr/lib/python3/dist-packages/simplejson/__init__.py', function loads
2020-12-07 23:52:04.898 Error: (Unifi Presence) ----> Line 370 in '/usr/lib/python3/dist-packages/simplejson/decoder.py', function decode
2020-12-07 23:52:04.898 Error: (Unifi Presence) ----> Line 400 in '/usr/lib/python3/dist-packages/simplejson/decoder.py', function raw_decode
2020-12-07 23:52:14.868 Error: (Unifi Presence) 'onHeartbeat' failed 'IndexError':'string index out of range'.
2020-12-07 23:52:14.868 Error: (Unifi Presence) ----> Line 658 in '/home/pi/domoticz/plugins/Domoticz-Unifi-Presence/plugin.py', function onHeartbeat
2020-12-07 23:52:14.868 Error: (Unifi Presence) ----> Line 345 in '/home/pi/domoticz/plugins/Domoticz-Unifi-Presence/plugin.py', function onHeartbeat
2020-12-07 23:52:24.838 Error: (Unifi Presence) 'onHeartbeat' failed 'IndexError':'string index out of range'.
2020-12-07 23:52:24.838 Error: (Unifi Presence) ----> Line 658 in '/home/pi/domoticz/plugins/Domoticz-Unifi-Presence/plugin.py', function onHeartbeat
2020-12-07 23:52:24.838 Error: (Unifi Presence) ----> Line 345 in '/home/pi/domoticz/plugins/Domoticz-Unifi-Presence/plugin.py', function onHeartbeat
2020-12-07 23:52:34.858 Error: (Unifi Presence) 'onHeartbeat' failed 'IndexError':'string index out of range'.
2020-12-07 23:52:34.858 Error: (Unifi Presence) ----> Line 658 in '/home/pi/domoticz/plugins/Domoticz-Unifi-Presence/plugin.py', function onHeartbeat
2020-12-07 23:52:34.858 Error: (Unifi Presence) ----> Line 345 in '/home/pi/domoticz/plugins/Domoticz-Unifi-Presence/plugin.py', function onHeartbeat
I don't have Dream Machine Pro, so maybe the API output is a little different. Can you post the output of the following url:
https://dreammachineIPAddres:8443/api/s/{}/stat/health
Last edited by wizzard72 on Tuesday 08 December 2020 16:39, edited 1 time in total.
wizzard72
Posts: 116
Joined: Friday 20 December 2013 7:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python plugin: Ubiquiti Unifi Presence

Post by wizzard72 »

iganin wrote: Friday 04 December 2020 22:50 I have got three Unifi APs. One of them in my garden. Is there a way to detect when my phone is connected to the AP which is located in my garden? The idea is to send a door bell to garden when i'm there.
I think I can.
Do you have an idea how I can present that data in the Domoticz GUI?
iganin
Posts: 47
Joined: Tuesday 28 October 2014 17:55
Target OS: NAS (Synology & others)
Domoticz version: 2020.2
Location: Aalter, Belgium
Contact:

Re: Python plugin: Ubiquiti Unifi Presence

Post by iganin »

It becomes interesting. I saw that api can get AP ID for specific MAC from the UniFi controller. However, I have no skills to implement it.

Try "User A in Garden" when the user with MAC A is connected to AP which is labelled as "Garden"
wizzard72
Posts: 116
Joined: Friday 20 December 2013 7:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python plugin: Ubiquiti Unifi Presence

Post by wizzard72 »

I want to make a general function so every one can use it. I can make a who is connected to an AP. That can probably be a text device. So you get for example 3 text devices (AP1 / AP2 / AP3 - I can read the names of the AP's from the API) and add to these text devices the devices connected to that AP.

Then in lua you can do your logic
iganin
Posts: 47
Joined: Tuesday 28 October 2014 17:55
Target OS: NAS (Synology & others)
Domoticz version: 2020.2
Location: Aalter, Belgium
Contact:

Re: Python plugin: Ubiquiti Unifi Presence

Post by iganin »

It's fine for me.
wizzard72
Posts: 116
Joined: Friday 20 December 2013 7:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python plugin: Ubiquiti Unifi Presence

Post by wizzard72 »

I put it on my todo list
rbosch
Posts: 11
Joined: Monday 18 January 2016 20:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: Python plugin: Ubiquiti Unifi Presence

Post by rbosch »

I don't have Dream Machine Pro, so maybe the API output is a little different. Can you post the output of the following url:
https://dreammachineIPAddres:8443/api/s/{}/stat/health
The Dreammachine Pro needs to be addressed at port 443. Doing this from the browser result in receiving the welcome screen. But this is probably not what you are looking for. I have Talend API tester installed, so I can fire some json at the machine, but my json knowledge is very limited so I would need some detailed instruction.

Thx for helping!
iganin
Posts: 47
Joined: Tuesday 28 October 2014 17:55
Target OS: NAS (Synology & others)
Domoticz version: 2020.2
Location: Aalter, Belgium
Contact:

Re: Python plugin: Ubiquiti Unifi Presence

Post by iganin »

I try to start the plugging on Docker and get this

Code: Select all

2020-12-08 20:57:10.090 Status: (UniFi Presence) Started.
2020-12-08 20:57:10.147 Error: (UnifiPresence) failed to load 'plugin.py', Python Path used was '/opt/domoticz/plugins/Domoticz-Unifi-Presence/:/usr/lib/python38.zip:/usr/lib/python3.8:/usr/lib/python3.8/lib-dynload:/usr/local/lib/python3.8/dist-packages:/usr/lib/python3/dist-packages:/usr/lib/python3.8/dist-packages'.
2020-12-08 20:57:10.148 Error: (UniFi Presence) Module Import failed, exception: 'ModuleNotFoundError'
2020-12-08 20:57:10.148 Error: (UniFi Presence) Module Import failed: ' Name: requests'
2020-12-08 20:57:10.148 Error: (UniFi Presence) Error Line details not available
It seems i'm missing some python modules. Going to investigate it as I use my own container.
wizzard72
Posts: 116
Joined: Friday 20 December 2013 7:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python plugin: Ubiquiti Unifi Presence

Post by wizzard72 »

rbosch wrote: Tuesday 08 December 2020 20:53
I don't have Dream Machine Pro, so maybe the API output is a little different. Can you post the output of the following url:
https://dreammachineIPAddres:8443/api/s/{}/stat/health
The Dreammachine Pro needs to be addressed at port 443. Doing this from the browser result in receiving the welcome screen. But this is probably not what you are looking for. I have Talend API tester installed, so I can fire some json at the machine, but my json knowledge is very limited so I would need some detailed instruction.

Thx for helping!
Aha, didn't know that the Dreammachine Pro used port 443. Can you test this,you need to be logged into the Dreammachine webpage and then:
https://dreammachineIPAddres:443/api/s/ ... tat/health

Can you paste the result or send me a PM
wizzard72
Posts: 116
Joined: Friday 20 December 2013 7:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python plugin: Ubiquiti Unifi Presence

Post by wizzard72 »

iganin wrote: Tuesday 08 December 2020 21:04 I try to start the plugging on Docker and get this

Code: Select all

2020-12-08 20:57:10.090 Status: (UniFi Presence) Started.
2020-12-08 20:57:10.147 Error: (UnifiPresence) failed to load 'plugin.py', Python Path used was '/opt/domoticz/plugins/Domoticz-Unifi-Presence/:/usr/lib/python38.zip:/usr/lib/python3.8:/usr/lib/python3.8/lib-dynload:/usr/local/lib/python3.8/dist-packages:/usr/lib/python3/dist-packages:/usr/lib/python3.8/dist-packages'.
2020-12-08 20:57:10.148 Error: (UniFi Presence) Module Import failed, exception: 'ModuleNotFoundError'
2020-12-08 20:57:10.148 Error: (UniFi Presence) Module Import failed: ' Name: requests'
2020-12-08 20:57:10.148 Error: (UniFi Presence) Error Line details not available
It seems i'm missing some python modules. Going to investigate it as I use my own container.
Looks like you don't have the Python3 module requests
wizzard72
Posts: 116
Joined: Friday 20 December 2013 7:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python plugin: Ubiquiti Unifi Presence

Post by wizzard72 »

Updated the plugin.

Done some improvements:
  • detect the hardware in the site
  • the pulled hardware information is directly connected to the detected hardware
  • the dreammachine (pro) use a slightly different login url. In the hardware page you now can select Dreammachine or Controller
Because of changes in the hardware tab, you have to update the plugin (git pull) and restart domoticz.
Luuc_a
Posts: 24
Joined: Tuesday 16 July 2013 10:12
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: Python plugin: Ubiquiti Unifi Presence

Post by Luuc_a »

Hi Wizard,

Because of the Christmas holidays I have some time to test the update :D
Unfortunaly it's not working for my setup. The errors I get are:

Code: Select all

2020-12-22 10:18:23.171 Error: (Unifi) 'onHeartbeat' failed 'KeyError'.
2020-12-22 10:18:23.171 Error: (Unifi) ----> Line 835 in '/home/pi/domoticz/plugins/Domoticz-Unifi-Presence/plugin.py', function onHeartbeat
2020-12-22 10:18:23.171 Error: (Unifi) ----> Line 412 in '/home/pi/domoticz/plugins/Domoticz-Unifi-Presence/plugin.py', function onHeartbeat
2020-12-22 10:18:23.171 Error: (Unifi) ----> Line 463 in '/home/pi/domoticz/plugins/Domoticz-Unifi-Presence/plugin.py', function request_details
The output of the API is:

Code: Select all

{"meta":{"rc":"error","msg":"api.err.LoginRequired"},"data":[]}
I use a Unifi Gateway Pro 3.
David82
Posts: 4
Joined: Monday 21 May 2018 22:19
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python plugin: Ubiquiti Unifi Presence

Post by David82 »

I think there is a probleem with de login command.

with correct login data I get this:
2021-01-07 08:07:48.277 Status: (UniFI) Started.
2021-01-07 08:07:49.313 (UniFI) Debug logging mask set to: NONE
2021-01-07 08:07:49.313 (UniFI) setVersionCheck - Plugin allowed to start (triggered by: onStart)
2021-01-07 08:07:49.313 (UniFI) Number of icons loaded = 3
2021-01-07 08:07:49.313 (UniFI) onStart: Items = UnifiPresenceAnyone
2021-01-07 08:07:49.313 (UniFI) onStart: Icon 101 Name = UnifiPresence
2021-01-07 08:07:49.313 (UniFI) onStart: Items = UnifiPresenceOverride
2021-01-07 08:07:49.313 (UniFI) onStart: Icon 102 Name = UnifiPresence
2021-01-07 08:07:49.313 (UniFI) onStart: Items = UnifiPresenceDevice
2021-01-07 08:07:49.313 (UniFI) onStart: Icon 103 Name = UnifiPresence
2021-01-07 08:07:49.309 Status: (UniFI) Entering work loop.
2021-01-07 08:07:49.310 Status: (UniFI) Initialized version 2.5.0, author 'Wizzard72'
2021-01-07 08:07:50.475 (UniFI) detect Unifi Devices: Invalid login, or login has expired
2021-01-07 08:07:50.476 (UniFI) create_devices: Found phone to monitor from configuration = Phone1=00:11:32:18:B0:XX
2021-01-07 08:07:59.998 (UniFI) onHeartbeat: Attempting to reconnect Unifi Controller
2021-01-07 08:08:00.885 (UniFI) onHeartbeat: Requesting Unifi Controller details
2021-01-07 08:08:00.906 (UniFI) request_details: Invalid login, or login has expired
2021-01-07 08:08:00.927 (UniFI) request_online_phones: Invalid login, or login has expired
2021-01-07 08:08:00.927 (UniFI) ProcessDevices: Total Phones connected = 0
2021-01-07 08:08:05.439 (UniFI) onHeartbeat: Attempting to reconnect Unifi Controller
2021-01-07 08:08:06.204 (UniFI) onHeartbeat: Requesting Unifi Controller details
2021-01-07 08:08:06.224 (UniFI) request_details: Invalid login, or login has expired
2021-01-07 08:08:06.243 (UniFI) request_online_phones: Invalid login, or login has expired
2021-01-07 08:08:06.244 (UniFI) ProcessDevices: Total Phones connected = 0

with the incorrect login
2021-01-07 08:08:44.504 Status: (UniFI) Started.
2021-01-07 08:08:45.538 (UniFI) Debug logging mask set to: NONE
2021-01-07 08:08:45.538 (UniFI) setVersionCheck - Plugin allowed to start (triggered by: onStart)
2021-01-07 08:08:45.538 (UniFI) Number of icons loaded = 3
2021-01-07 08:08:45.538 (UniFI) onStart: Items = UnifiPresenceAnyone
2021-01-07 08:08:45.538 (UniFI) onStart: Icon 101 Name = UnifiPresence
2021-01-07 08:08:45.538 (UniFI) onStart: Items = UnifiPresenceOverride
2021-01-07 08:08:45.538 (UniFI) onStart: Icon 102 Name = UnifiPresence
2021-01-07 08:08:45.538 (UniFI) onStart: Items = UnifiPresenceDevice
2021-01-07 08:08:45.538 (UniFI) onStart: Icon 103 Name = UnifiPresence
2021-01-07 08:08:45.533 Status: (UniFI) Entering work loop.
2021-01-07 08:08:45.534 Status: (UniFI) Initialized version 2.5.0, author 'Wizzard72'
2021-01-07 08:08:47.203 (UniFI) detect Unifi Devices: Invalid login, or login has expired
2021-01-07 08:08:47.204 (UniFI) create_devices: Found phone to monitor from configuration = Phone1=00:11:32:18:B0:XX
2021-01-07 08:08:56.726 (UniFI) onHeartbeat: Attempting to reconnect Unifi Controller
2021-01-07 08:09:02.448 (UniFI) onHeartbeat: Attempting to reconnect Unifi Controller
2021-01-07 08:09:08.314 (UniFI) onHeartbeat: Attempting to reconnect Unifi Controller
2021-01-07 08:09:14.253 (UniFI) onHeartbeat: Attempting to reconnect Unifi Controller
2021-01-07 08:09:20.174 (UniFI) onHeartbeat: Attempting to reconnect Unifi Controller

unifi controler versie 6.0.36 (Build: atag_6.0.36_14304) (raspberry)
USG-3P
rbosch
Posts: 11
Joined: Monday 18 January 2016 20:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: Python plugin: Ubiquiti Unifi Presence

Post by rbosch »

Thx for the update for the dream machine. I have done a fresh git pull, and selected the Dream machine option.
But still getting errors:

Code: Select all

2021-01-11 00:10:06.423 Error: (Unifi Presence) 'onStart' failed 'JSONDecodeError'.
2021-01-11 00:10:06.424 Error: (Unifi Presence) ----> Line 872 in '/home/pi/domoticz/plugins/Domoticz-Unifi-Presence/plugin.py', function onStart
2021-01-11 00:10:06.424 Error: (Unifi Presence) ----> Line 215 in '/home/pi/domoticz/plugins/Domoticz-Unifi-Presence/plugin.py', function onStart
2021-01-11 00:10:06.424 Error: (Unifi Presence) ----> Line 706 in '/home/pi/domoticz/plugins/Domoticz-Unifi-Presence/plugin.py', function detectUnifiDevices
2021-01-11 00:10:06.424 Error: (Unifi Presence) ----> Line 900 in '/usr/local/lib/python3.7/dist-packages/requests/models.py', function json
2021-01-11 00:10:06.424 Error: (Unifi Presence) ----> Line 518 in '/usr/lib/python3/dist-packages/simplejson/__init__.py', function loads
2021-01-11 00:10:06.424 Error: (Unifi Presence) ----> Line 370 in '/usr/lib/python3/dist-packages/simplejson/decoder.py', function decode
2021-01-11 00:10:06.424 Error: (Unifi Presence) ----> Line 400 in '/usr/lib/python3/dist-packages/simplejson/decoder.py', function raw_decode

Looking at this PHP client class from @mallepietje https://github.com/Art-of-WiFi/UniFi-AP ... ag/v1.1.63, it seems that for querying the API an insert in the base URL should be made by adding /proxy/network/.

Code: Select all

if ($this->is_unifi_os) {
            $url = $this->baseurl . '/proxy/network' . $path;
        } else {
            $url = $this->baseurl . $path;
        }
Also some code is dedicated to doing smart stuff with the token:

Code: Select all

       if ($this->is_unifi_os && $this->request_type !== 'GET') {
            $csrf_token = $this->extract_csrf_token_from_cookie();
            if ($csrf_token) {
                $headers[] = 'x-csrf-token: ' . $csrf_token;
            }
        }
Not sure how to translate this to modifications to your python script.
(I have a lot of general programming knowledge but no python experience).
Hope this helps.
wizzard72
Posts: 116
Joined: Friday 20 December 2013 7:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python plugin: Ubiquiti Unifi Presence

Post by wizzard72 »

@rbosch, can you login into your dreammachine and then change the url into this:

https://dreammachineIP:Port:/api/s/default/stat/device

Can you send me the output by pm.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest