Unifi Network: Number of Active Clients on Guest SSID

Yep exactly that! Just need some guests to come round to properly test it! :smiley:

2 Likes

Hi jay, i looking to do something similar and came across your thread.
For a public place,
I would like to turn daikin ac on / off based on active wifi clients, e.g if no active clients turn off ac.
Would it be possible? fortunately i already got unifi ap…Just need to setup HA and get the daikin ac.

I’m also looking for the same… I found this custom_component but I can’t add it to Home Assistant…

This has already stopped working :upside_down_face:
So ignore this for now whilst I try and work it out…


Just to add to this, I was using this template snippet but it started playing up and not rendering because not all device_tracker entities have the essid attribute (companion apps etc).

I have written a truly cursed alternative which checks everything first. It’s much less efficient though so if you have any suggestions, please do let me know :smile:

{% set devices = [] %}
{% for entity in states.device_tracker %}
  {% if 'essid' in entity['attributes'].keys() %}
    {% if '**[SSID OF GUEST NETWORK HERE]**' in entity['attributes'].values() %}
      {% set devices = devices + [entity] %}
    {% endif %}
  {% endif %}
{% endfor %}

{% set dev_count = devices | list | count  %} 

{% if dev_count == 1 %}
  {{dev_count}} guest device connected
{% else %}
  {{dev_count}} guest devices connected
{% endif %}
1 Like

Your template snippets always shows me “0 guest devices connected”. I think there is a problem with {% set devices = devices + [entity] %} which is not correctly adding the entity to the array.
I adapted it a bit and it seems to work now for just getting the raw count:

- platform: template
  sensors:
    guest_wifi_count:
      friendly_name: Guest WiFi Count
	  value_template: >
        {% set ns = namespace(counter=0) %}
        {% for entity in states.device_tracker %}
        {% if 'essid' in entity['attributes'].keys() %}
        {% if 'BattlestarGalactica' in entity['attributes'].values() %}
        {%- set ns.counter = ns.counter + 1 %}
        {% endif %}
        {% endif %}
        {% endfor %}
        {{ ns.counter }}

DISCLAIMER: I’m very new to HA hence I’m not sure if this is the best solution.

1 Like

I actually ended up making a template sensor for this rather than relying on the template in a dashboard card:

template:
  sensor:
      - name: "guest.ssid Device Count"
        state: "{{ states.device_tracker | selectattr('attributes.essid','defined') | selectattr('attributes.essid','eq','guest.ssid') | list | count }}"
        state_class: measurement
        unique_id: "guest_ssid_device_count"
        icon: "mdi:devices"

Then read that in the template:

 {% set dev_count = states('sensor.guest_ssid_device_count') | string %}{{iif(dev_count=="1","1 guest device connected",dev_count+" guest devices connected")}}
2 Likes

Great thread! But there are a few options on how to do this, can someone please share if you have a solution that is working and is reliable?

I think the solution proposed by @aaroncarson here is the best one.
If you configure a template sensor like this, it is directly usable as entity e.g. in an automation.

2 Likes

Yep, it’s worked reliably for a few months now across my 3 SSIDs :smiley:

1 Like

Does this still work for all of you? The following code does not update when guests join my network (I’ve substitued my nework for “guest.ssid” of course). However, the HACS Unifi Counter Sensor does update as expected. Just curious if it’s only me?

{{ states.device_tracker | selectattr('attributes.essid','defined') | selectattr('attributes.essid','eq','guest.ssid') | list | count }}

The UniFi Device trackers don’t get enabled by default anymore (changed in August?) on the integration. So while they’re on the network, I suspect your device tracker entities are created but disabled. If they’re disabled they won’t appear in the filter and therefore won’t update the count.

2 Likes

Can them be enabled again to make it work?

Yes you can manually. But it kind of defeats the purpose of counting guests if you have to go enable the entity manually every time someone connects…

Edit/removed original reply.

I found in the docs where this change was made. This is dumb. I want device trackers to be enabled by default. Is there some way to override this nonsense change that was made??

I don’t begrudge those who want it off by default - but I also can see why I’d want it optional on a network by network basis. (maybe I dont care about device trackers for items on XXX subnets - dont clutter HA with those) but my guest subnet I absolutely want to know how many devices are active at any given time, etc… I was using this to auto trigger ‘guest mode’ in my home until I lost it last… (Sep?)

I saw in some post on git (buried in one of the PoE switch bug/requests) where the developer was aware of the need - but there was some other blocking work with the PoE stuff. I was doing research before posting a bug/feat request if there wasn’t already one now that the PoE switch feature shipped.

Create a new one please. It’s hard to remember stuff from the forums or embedded in other issues.

1 Like

Unifi: change to device tracker default off breaks guest subnet tracking case · Issue #86181 · home-assistant/core (github.com)

1 Like

I have gone through my Unifi controller, and can confirm that any devices seen beyond a certain date do not have device tracker entities created in HA - not that they are there and disabled, but that they don’t even exist. It’s almost as though HA “automatically cleaned up” a bunch of (what I presume to be) disabled device trackers? Not sure. All I know is that for devices prior to said date, the device trackers show up in HA.

This is a big problem for me since I am attempting to create an alarm system for my house, and one of the triggers for putting the house in “guest mode” will be if any guests are connected to my guest WiFi. Since cell phone coverage here is crap, the first thing guests do is scan the QR code on the kitchen tablet to get connected. To ensure they don’t STAY connected (neighbors, etc), the code automatically rolls over every night to boot everyone off. (I posted the cost in a thread on here already)

So, if I have no device trackers, and can’t tell if someone is or is not on my guest network, that’s a problem. Being forced to remember to go into HA and enable an entity every time they show up, while being a very large inconvenience for me, will be wholly untenable for my wife, and will drive WAF for this project to zero.

As for what @NathanCu said about not begrudging those who want it off by default - that’s fine. But for a dev to make the assumption that disabled by default is best for everyone was an incredibly bad decision, IMO. What should have happened was “We added a new option to allow you to disable certain newly-created entities by default”, rather than simply breaking the integration without allowing users the ability to even re-enable it should they want to.

Before I make an edit - to the bug Robert - It’s about counting any foriegn entity on a given network - the way it works now we cont get device trackers for new nodes. Guests or no - unless we go in and manually enable each one.

THat has many uses - i cant count them all - but the one MOST people use them for are How many unknowns are on my network, It was easily templatable as a counter device tracker where integration = unifi and XYZ=some other condition to filter down by network or segment or subnet or otherwise.

I just want the option for the trackers to autocreate enabled like they used to. Default off is fine but I cant find how turn it back on manually (with user knowledge of the potential consequences - you should see what the BLE tracker does to your device trackers). I may have missed something in the last few releases?

If its a script workaround that sits and looks for new disabled device tracker entities with integration unifi and flip them on?

FYI this is an architectural decision in Home assistant and the behaviour of all device trackers, though it might have been because people complained about getting too many entities from the UniFi integration originally.

The only way to get them autoenabled nowadays is if the device is already in the device registry, then the device tracker will be automatically enabled. There is no logic to change that behaviour right now. That would be a feature request for home assistant in general.

Rather we look into alternative solutions like a sensor per SSID or something

1 Like