Unifi: not seeing away devices!

Hi

I have setup Unifi plugin to track when I’m home/away ! It detects well when I’m home, but it never “goes” away :frowning:

This is setup I have done in configuration file:

device_tracker:

  • platform: unifi
    host: 192.168.1.248
    username: sylvain
    password: monmotdepasseici
    verify_ssl: False
    track_new_devices: yes
    interval_seconds: 20
    consider_home: 180

username/pass changed for security reasons here !

Any ideas what’s wrong ? I guess it accepts well login/pass if it detects already online devices no ?

Thanks

2 Likes

What version of HA are you running?
I used to see many many login failures for unifi, but in HA 0.41 they upgraded to pyunifi v2 and it resolved the issues for me.

HA 0.41.0 running here ! I made more investigations and discovered that it detects nearly instantly when I’m back, but it takes between 15/20 minutes to detect that I’m away which is definitively too long !

How did you see the login failures in previous version ? Logs of HA ?

Yeah from the logs.
The number of errors from unifi was making my logs huge!
TBH though I didnt check how quickly away status is set.

I have the same issue. The component doesn’t detect i’m away for roughly 15-20 minutes after I left home. Changing interval_seconds does not do anything.

Sounds like the ARP timeout is somewhere in the 15-20 minute range. Do you know if you can configure this in the device to be a shorter time?

Basically device connects and updates ARP cache immediately, device disconnects and ARP isn’t updated until the timeout occurs.

It’s using ARP to know if device is there or not ? it makes non sense as module requests credentials of controlers so it should be able to check the list of connected devices directly which is a lot more accurate and fast !

ARP time-out is not something you can setup right now on Unifi devices, only by hand modifying Avaya which is not really nice and doesn’t survive firmware update :frowning:

Are you sure it’s really using ARP ? as I had a quick look at the python file that handles interface with Unifi API, and only method I have seen is Get_Clients which I guess do same thing as client list in Ufini Controler interface.
Not sure where to look to know which method HA uses for Unifi devices :confused:

Well, it’s really more of a theory than a concrete answer. I don’t have one of these devices, so i also don’t use the component in HA so I don’t know exactly how its checking for presence. But even though the HA component is connecting and using credentials if the ARP timeout hasn’t occurred HA will still think the device is connected. That’s IF it’s using ARP. Maybe ping the dev of the component?

From the HA perspective, all it does is login to the Unifi controller and query the connected clients. If the Unifi controller reports a client as connected, then HA will see the device as ‘home’. You may need to tweak the config in the Unifi controller to lower the timeout value, although I’m not sure you can. Mine varies, but can be up to 10+ minutes before noticing.

I made further investigation this morning and I noticed also there is a long delay in controler interface before a device disconnected appears as offline (around at least 10 minutes !) so looks like HA can’t do better !
Only way I would see that would be more efficient who be a ping every 30s or whatever to the device to be sure it’s there or not ! Will have to check if a plugin exists to test such thing in HA…

I haven’t had a chance to check it out yet, but I found this link at some point in my travels:

Not sure if it will help you, if you try it, let us know!

I went finally with Owntracks and updates through my Mosquitto broker and it works great (IP ping is not very efficient due to energy saver algorythm in modern wireless devices !).
I found GPS is lot more efficient :slight_smile:

1 Like

I have a local fix for this. I guess I should get all setup so I can contribute it.

Curious to know how ? as most modern Wifi mobile devices are tricky to detect for sure with energy savings algorythm !

The Unifi controller shows recently connected devices (with 20 minutes or so) on their client list, but also reports a “last_seen” time for each device. I use this last seen time to filter out the devices who haven’t been seen within the “consider_home” timeframe.

Changed line 86 to:

self._clients = {client['mac']: client for client in clients if (dt_util.utcnow() - dt_util.utc_from_timestamp(float(client['last_seen']))) < self.consider_home}

( defined conside_home = config[DOMAIN].get(CONF_CONSIDER_HOME) in get_scanner )

https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/device_tracker/unifi.py

2 Likes

@tloth01 have you checked if you see your wireless devices like phones be considered as away quickly after having left the place ?

Yep, it works. I have my “consider_home” set to 3 minutes, so it takes 3 minutes after disconnecting.

I cant get this to work. Changed line 86

2017-06-13 22:10:03 INFO (MainThread) [homeassistant.loader] Loaded device_tracker.unifi from homeassistant.components.device_tracker.unifi
2017-06-13 22:10:05 INFO (MainThread) [homeassistant.components.device_tracker] Setting up device_tracker.unifi
2017-06-13 22:10:05 ERROR (MainThread) [homeassistant.components.device_tracker] Error setting up platform unifi
  File "/usr/local/lib/python3.5/dist-packages/homeassistant/components/device_tracker/unifi.py", line 67, in get_scanner
  File "/usr/local/lib/python3.5/dist-packages/homeassistant/components/device_tracker/unifi.py", line 76, in __init__
  File "/usr/local/lib/python3.5/dist-packages/homeassistant/components/device_tracker/unifi.py", line 86, in _update
  File "/usr/local/lib/python3.5/dist-packages/homeassistant/components/device_tracker/unifi.py", line 86, in <dictcomp>

Yea, I should setup a pull request, but I haven’t even setup an actual developer environment yet. Too busy with work I get paid for :smiley:

gotta import consider_home:

    from homeassistant.components.device_tracker import (
DOMAIN, PLATFORM_SCHEMA, DeviceScanner, CONF_CONSIDER_HOME)

pass it to UnifiScanner:

return UnifiScanner(ctrl, config[DOMAIN].get(CONF_CONSIDER_HOME)) 

and allow UnifiScanner to receive it:

    def __init__(self, controller, consider_home):
    """Initialize the scanner."""
    self._controller = controller
    self.consider_home = consider_home
2 Likes