Unifi Network: Number of Active Clients on Guest SSID

Good afternoon! I spent quite a while searching for a solution to this problem, so I thought I would share the final solution here!

Problem:
I wanted to add a condition to my Away mode automations so they don’t run if we have guests at home. Sometimes my parents stay with us and watch the kids whilst we have a date night - and it doesn’t win any favours if you turn the entire house off within minutes of leaving and they’re still here!

In the past, I just used an Input_Boolean and used it as a condition - we enable it when guests arrive, and disabled it when they left. Apart from when we didn’t - which was often. Easily forgotten about - same problem as before!

My Idea:
Using the Unifi integration, track if there is any clients connected to my Guest SSID and automatically set the Guest mode based on that. Some have already done this using the “is_guest” attribute, but I have found that to be unreliable, and flagged some of the clients on my Kids VLAN as guests…

My Solution:
Very similar to using the “is_guest” attribute, but instead using the “essid” attribute to count how many clients are home and connected to that SSID name. Problem solved!

Template sensor YAML:

sensors:
  guest_wifi_count:
    friendly_name: Guest WiFi Count
    value_template: >-
      {{ states.device_tracker | selectattr('state', 'eq', 'home') | selectattr('attributes.essid', 'eq', "**[SSID OF GUEST NETWORK HERE]**") | list | count }}

Maybe this helps if anyone else looks for the same solution!

Cheers - Jay.

10 Likes

Very elegant, Jay!

So basically by allowing a friend wifi access, you automatically set the home to guest mode…

Might give this a try today.

1 Like

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")}}

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.