Presence detection using Unifi device and wifi connection


I’m trying to create an automation that can trigger when my iPhone connects to the home wifi.

The network is running using Unifi APs and Dream Router.

I do not have the HA Companion App connecting to my HAOS as it’s running on a Pi locally and I don’t want to open up the firewall and NAT. Hence preferring to use the device being present on the local network as meaning ‘home’.

From what I’ve been reading it seems I need to set up a binary state based on whether the iPhone is connected to the wifi. But that’s when I get stuck - I can’t even see where in HA I start to build this state entry.

Could someone get me started please?


If you don’t have the unifi network integration, add it.

Then use the unifi network device trackers.

A couple videos to get you started:

1 Like

That’s very useful thanks.
I’ve followed the second video and now have a person to track but I’ve no idea how I actually go about doing that.
Do I need to create a sensor? Do I edit a config file or is it done via the web interface?

You just use it in an automation and say “if home” or “if away”.

This video has an example of the automation configuration:

1 Like

Once you have a device tracker from your unifi integration (you may have to turn off Mac address randomization for unifi to be able to setup the sensor for your phone)
Add that device tracker to your person as a tracked device. Now anytime the device is set home (unifi does it) then the person associated is now also set home. The home zone state is a count of persons in that zone so…

For home /away automations look at a numeric state of the home zone. trigger above zero = occupied.

Have fun

Is that an apple thing? I’ve never heard of that.

That helped thanks.
It seems that with HA once you know the basic building blocks you’re away with doing stuff - but to learn the basics you have to build stuff - catch 22. The documentation helps with the attributes of objects but seems, to me, to lack examples of how they are used.

But all these comments certainly help and are appreciated. Thanks.

You’re welcome.

There’s LOTS of good info in the videos on that channel. If you need to learn a lot of the basics, start going through those videos - even the ones that might not seem 100% relevant at first glance. A lot of it will stick in the back of your mind, and then when you run into trying to do something, you’ll be like “Oh yeah, I remember seeing something related to that in a video a while back…”


Wildo thanks.
The issue for me is debugging anything.
For example, I’ve followed one video which linked my iPhone to UniFi for Wi-Fi connect / disconnect, and I created a user (person) to track it.
As the first video didn’t show how to use it I watched another which was concerned with opening firewall ports but mentioned briefly a trigger for when a device appears on the network.
By pausing and studying the screenshot I thought I’d figured out what kind of triggered event to watch for and wrote an automation.
Tonight I’ve tested it by leaving the area and returning (with hindsight I could just turn off iPhone wifi for a while instead :roll_eyes:) but nothing happened.
Looking at Logfile I can see the phones companion app tracker has seen it leave the area and return but the person related one hasn’t - and that’s the one the automation uses.

So where do I go from here? I didn’t find a detailed example of how to do this so clearly don’t have enough understanding of how things interact and how to debug. This is my frustration.

HA seems like a great product, but it’s a pain trying to get enough knowledge to be able to fly generally solo - for me anyway.

Update: turning the iPhone wifi off for 10 minutes then on again DID cause my person to show as left the building and returned - and the automation ran. So perhaps I just needed to be out of range for longer. Progress! :blush::+1:t2:

But tips on how to debug woukd still be appreciated thanks

No both modern iPhone and Android devices randomize the Mac address whenever you join a wireless network to make it hard to track a device. That’s fantastic. But it also makes it hard to use a device as a tracker. You can disable it on a SSID by SSID basis. No harm disabling it for your home network.

EDIT: I just went and looked this up since I couldn’t believe I’ve never had to deal with this problem.

Apparently on a Unifi network, this makes zero difference. I just checked, and MAC randomization is enabled for my home wifi connection (it can be enabled and disabled by SSID according to the docs). My phone device tracker still works perfectly, without any modification on my part. Magic? IDK.

Thank you, though. Learn something new every day!

1 Like

That is likely a function of your unifi integration. There’s a setting in the unifi integration configuration for how long a device must be disconnected from the network before being marked as away. Mine is set to 120 seconds, but IIRC the default is like 300 seconds maybe? I forget. I still use times in my automations though, like “when my phone is off the wifi for 20 seconds (so that would actually be a total of 140 seconds), do this” or “when my phone has been connected to the wifi for 10 seconds, do this”.

Works spectacularly well.

As for debugging, the developer tools are your friend. You can look at the state of any device or entity or person or anything else there.

Developer Tools on the left, then States.

And not for nothing, but if you’re struggling now, I promise it’s not really going to get a whole lot better if you continue going at it from odd angles. There’s not really a step-by-step “here’s how you log in. Here’s how you do this. Here’s how you do that.” But by the same token, if you try to solve an issue from the inside-out, instead of from the top-down, you’re likely to be met with more frustration. The VAST majority of solutions to issues you are going to find - whether on YouTube, or on the forums, are going to be very narrowly focused ON THAT PARTICULAR ISSUE. As such, there will be pieces left out, because the person who posted the problem already understands those pieces. This means that as a new user, you’ll need to go and research the left out pieces. I know - I know. How do you go look for something that you don’t even know is there? How do you know what you don’t know? (for the record, that’s also kind of why my first response didn’t mention the home/away thing - the assumption was that you already knew that, since it’s one of the most basic things that most users set up right out of the box. My bad!)

But that’s kind of my point - you’ll need to have a good deal of patience when you’re first getting up and off the ground, and be willing to read a LOT, and/or watch a LOT of YouTube. Otherwise, you’re going to be bald long before you finish configuring all the things you want.

But on the bright side, if you stick with it, Home Assistant is definitely the most flexible and robust solution available for Home Automation.

That said, it certainly is not for everyone, and there’s no harm in admitting that to yourself. It’s not a reflection on intelligence, or anything else of the sort. Some people just don’t have the free time to invest to learn something this complex, and others aren’t interested in learning it at all, they want something that “just works”. These are both fine reasons. It takes all kinds, man.

But - you’ll find that me, and others, will be happy to help you along the way.

Welcome, and enjoy!

1 Like

Your phone randomizes your MAC address per SSID but not each time you connect. In other words, your home router always sees your phone having the same MAC address (assuming you always connect to the same SSID at home).

The purpose of the randomization feature is to prevent tracking your phone across different WiFi networks.

Some valid points. My background is 40 years working as a developer from Assembler, Cobol, C, Perl, Php, Java and Ruby. Learning new technical items isn’t an issue generally and I enjoy it.

As you say it’s difficult to learn anything when coming at it from many angles and in small chucks, but unfortunately there isn’t a comprehensive ‘how to’ guide available - which is my point.
I’m used to, perhaps I’ve been spoiled lol, grabbing some documentation, a coffee, a couple of weeks and digesting it all.

But now I’ve been pointed to debugging (Log & Logbook are clearly key atm) it’ll be easier to start to make some progress.

As always, your responses (and patience) are appreciated assisting and passing on your experience.

Ok, that seems like it would make sense then, and would explain why I’ve never had to do anything for device trackers to just “work” in my setup.

But, that doesn’t explain why @NathanCu suggested that OP may need to disable MAC randomization? Bad intel?

It doesnt disable on guest segments - My wife’s work iPad is a massive PAIN and that was the only way to isolate it.

I don’t follow… How does the device know if it’s on a guest network or not? Wifi is wifi, no? AFAIK, there’s nothing in the standard that identifies a network as a guest network vs not a guest network?

It was a while ago - Unifi may have changed the behavior since - all I know is the device kept rotating its mac address and Unifi wouldn’t ID it as a single device It kept coming up as one of 12 different machines - killing THAT setting was the secret sauce to ID that device. Now it’s one of the most rock solid trackers I have. In fact I used it to remind my wife she left it at home the other day.

The important part is - Considering I control that SSID and segment there is no risk to me to flipping that switch for any SSID I own. So I do, problem mitigated. Would I flip ot for anyone else’s SSID - unless I KNOW them - not on your life.

The way Apple implemented their private address behavior changed a few times as they rolled it out. Early on (maybe beta only?) they randomized the address every 24 hours. The method that they use for the latest iOS is a randomized address per SSID that is only changed if a user forgets the network or hasn’t connected in 6 weeks. Depending on the iOS device & version you may see different behavior.

I certainly agree that randomization is safe to turn off on your own devices for your own networks.

Some more background can be found here and here.

1 Like

Ah. So it sounds as though this is, in fact, a crapple problem.

That would make sense since I have never once experienced any of the issues described.

1 Like