Using node-red-contrib-unifi with a UniFi WiFi Network

I am trying to address the age old problem of which phones are attached to my network (i.e. are we in?).

I have 5 meshed UniFi APs which I have set up and control via the UniFi Network application. I do not have a UniFi hardware controller.

Am I right to assume I can't use node-red-contrib-unifi? Are there any UniFi alternatives to fetch 'attached clients' data from my APs ?

Thank you

I don't use unifi but looking at the node I would say not, as its designed to talk to the controller API.

Without that each AP would need to be queried separately. I don't know if there is an API available on the AP itself ? You may be able to get information from the AP web interface.

To solve this issue myself I run my DHCP server on the Rpi that has node-red, then I get an MQTT message each time a device renews its lease.

Can't remember if you can do that from the unifi node. If not, you might be able to use the SNMP node. But I have to say that it is an unreliable measure anyway since battery powered Wi-Fi devices will generally spend as much time as possible disconnected.

Which tells you something every NN hours - or even weeks, depending on how you've set up DHCP :slight_smile:

Actually every 5 mins :upside_down_face:

Wow, that's quite a load on both the devices and the DHCP server. It can cause issues with devices disconnecting as well as they poll for a new address.

There aren't that many devices using DHCP as most of the things on the network have static IPs.

I also combine this with a door contact on the front door. That way if a phone is at home but "sleeping" I don't consider the person not at home unless the door has also been opened within last 10mins.

I have found this to work very well. There has to be a slight delay to consider someone not at home, but anyone arriving is usually logged as in by the time they get to the front door.

Obviously it has it limitations - eg if someone leaves their phone at home.

I gave up on trying to automate presence years ago. I just set in/out manually :grin:

I work up in the attic so its handy to see who is in the house.

Also my daughter has a habit of leaving everything in her room switched on when she goes out.
In her case I also check last motion detected in her room (so as not to plunge her into darkness :wink: )
I reckon the chances of her leaving without her phone are about the same as being hit by a meteor and lighting at the same time :joy:

Then I store the state of the lights etc and turn them off when she leaves, and then restore them when she gets home, and she is none the wiser :rofl:

3 Likes

Smcgann Hmmm I suspect you are right. It's a shame because the "UniFi network application" obviously collects the information as it displays / interacts via a web page.

I can tell if I'm home by pinging my phone's (reserved) IP address with node-red-node-ping

[{"id":"65e0a49e109e70a6","type":"ping","z":"5de4802425738256","protocol":"IPv4","mode":"triggered","name":"Ping ","host":"192.168.1.51, 192.168.1.52","timer":"20","inputs":1,"x":410,"y":120,"wires":[["3c35d8f7b54dc61d"]]},{"id":"3c35d8f7b54dc61d","type":"switch","z":"5de4802425738256","name":"online?","property":"payload","propertyType":"msg","rules":[{"t":"istype","v":"number","vt":"number"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":560,"y":120,"wires":[["86b6ba68e32bf4d1"],["1af3826e1a9f7e88"]]},{"id":"86b6ba68e32bf4d1","type":"change","z":"5de4802425738256","name":"ONLINE","rules":[{"t":"set","p":"payload","pt":"msg","to":"topic & \" is online\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":100,"wires":[["3d6790d4d4e2d3b3"]]},{"id":"3d6790d4d4e2d3b3","type":"debug","z":"5de4802425738256","name":"Who's home?","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":900,"y":120,"wires":[]},{"id":"79c181405c6a63c2","type":"inject","z":"5de4802425738256","name":"GO","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":250,"y":120,"wires":[["65e0a49e109e70a6"]]},{"id":"1af3826e1a9f7e88","type":"change","z":"5de4802425738256","name":"OFFLINE","rules":[{"t":"set","p":"payload","pt":"msg","to":"topic & \" is offline\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":140,"wires":[["3d6790d4d4e2d3b3"]]}]

Did you try it ?

I don't have a hardware controller, but have a container running somewhere that i can access with this node.

Looks like this (node-red-contrib-unifi-os) might do the job although I'm not 100% clear on the cofiguartion options.

I am a contributor, but only work on the Protect Side of the module

let me call in the main author: @crxporter

Since I've been called...

If you are running either software or hardware UniFi controller - yes this is possible. The controller runs on UniFi hardware as "UniFi OS" or as a software controller on just about any hardware (raspberry pi or docker are popular of course)

I'll be happy to help you set it up, it'll be similar to how @bakman2 runs the node. I'm busy for the next ~4 hours but could help assist afterwords.

Great..... Thanks everyone. So I run node-red on a mac mini (Monterey). I have the "Unifi Network Application running on the same mac.

I can see how (I think) I cab set up the Controller.....IP: localhost, port:8443 Type: UNiFi network application with username and password the same as I would use to access the application webpage.

Which node would I use to find out if a client was atatched to an AP e.g. 192.168.1.5 ? and in this context what is an 'endpoint'

Thank you
Ian

That's correct, you will set it to Network Application (not Unifi OS)

'endpoint' - The various API addresses

Using the request node - the following endpoint will GET you, all active clients (i think) - again im on the Protect Side of the module

/proxy/network/api/s/default/stat/sta/

See here:
products:software:unifi-controller:api [Ubiquiti Community Wiki] (ubntwiki.com)

Thank you

I'm using https://localhost and 8443 with usual user and password but the just node says "initialiizing'. I can use the same information to log on locally with a browser.

Regards
Ian

Just the ip (127.0.0.1) or localhost - yo don’t need https:// (I think)

both give "TypeError: Invalid URL"

Let's see. I'll review how the connection should look then ask a couple of questions about your setup...

I would expect the controller setup to look like this (but with your user/password):


Note that you should use local credentials on the controller, not your typical UniFi email/password login. The 2 factor authentication does not work here.

Then put a request node in that looks like this:


That endpoint is /proxy/network/api/s/default/stat/health so you don't have to manually type if you don't want.

Do that and then set up an inject and debug node like this:

Hit the inject and hopefully you get some output. Note I think there is a timer after deploy before it will login, maybe 15 or 30 seconds.

Things to try
Test the controller on port 8443 and 443
Restart nodered (not just redeploy) after you make config changes just to verify that any old settings are cleared
Like I said above, make sure you are using local credentials on the controller

I'll be around for a while now - hopefully this gets you somewhere! Once you're logged in, I'll be happy to share my flow for watching "which MAC addresses are connected to wifi" (if you want it)