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

**URL:** <https://discourse.nodered.org/t/using-node-red-contrib-unifi-with-a-unifi-wifi-network/74246>\
**Category:** General\
**Tags:** unifi\
**Created:** [27 January 2023 06:42 UTC](https://discourse.nodered.org/t/using-node-red-contrib-unifi-with-a-unifi-wifi-network/74246 "2023-01-27T06:42:19Z")\
**Posts on this page:** 18\
**Page:** 3

<div class="post-metadata">

**Author:** ![ianh33](https://avatars.discourse-cdn.com/v4/letter/i/ea5d25/32.png) [@ianh33](https://discourse.nodered.org/u/ianh33)\
**Post date:** [27 January 2023 21:08 UTC](https://discourse.nodered.org/t/using-node-red-contrib-unifi-with-a-unifi-wifi-network/74246/41 "2023-01-27T21:08:49Z")

</div>

> [@crxporter](#):
>
> /api/s/mysite/stat/health

"Error: No Site Context"

---

<div class="post-metadata">

**Author:** ![crxporter](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/crxporter/32/25734_2.png) [@crxporter](https://discourse.nodered.org/u/crxporter)\
**Post date:** [27 January 2023 21:09 UTC](https://discourse.nodered.org/t/using-node-red-contrib-unifi-with-a-unifi-wifi-network/74246/42 "2023-01-27T21:09:23Z")

</div>

Do it with the default site:

`/api/s/default/stat/health`

---

<div class="post-metadata">

**Author:** ![ianh33](https://avatars.discourse-cdn.com/v4/letter/i/ea5d25/32.png) [@ianh33](https://discourse.nodered.org/u/ianh33)\
**Post date:** [27 January 2023 21:10 UTC](https://discourse.nodered.org/t/using-node-red-contrib-unifi-with-a-unifi-wifi-network/74246/43 "2023-01-27T21:10:56Z")

</div>

same result

---

<div class="post-metadata">

**Author:** ![crxporter](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/crxporter/32/25734_2.png) [@crxporter](https://discourse.nodered.org/u/crxporter)\
**Post date:** [27 January 2023 21:12 UTC](https://discourse.nodered.org/t/using-node-red-contrib-unifi-with-a-unifi-wifi-network/74246/44 "2023-01-27T21:12:41Z")

</div>

Try this one - it's just going to check who is the logged in user on the controller - doesn't need a site.

`/api/self`

---

<div class="post-metadata">

**Author:** ![crxporter](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/crxporter/32/25734_2.png) [@crxporter](https://discourse.nodered.org/u/crxporter)\
**Post date:** [27 January 2023 21:14 UTC](https://discourse.nodered.org/t/using-node-red-contrib-unifi-with-a-unifi-wifi-network/74246/45 "2023-01-27T21:14:06Z")

</div>

And if that works - you can grab a list of your sites so you know what to fill in for the other calls:

`/api/stat/sites`

...edit: this one may not work? I'm testing now...  
...edit 2: oops I was testing on my NVR instead of my network controller. works great on the UDM.

---

<div class="post-metadata">

**Author:** ![ianh33](https://avatars.discourse-cdn.com/v4/letter/i/ea5d25/32.png) [@ianh33](https://discourse.nodered.org/u/ianh33)\
**Post date:** [27 January 2023 21:17 UTC](https://discourse.nodered.org/t/using-node-red-contrib-unifi-with-a-unifi-wifi-network/74246/46 "2023-01-27T21:17:07Z")

</div>

> [@crxporter](#):
>
> /api/stat/sites

both worked..... I've taken enough of your time and can probably take it from here.

thank you....how do I send you a 'thank you beer" ?

---

<div class="post-metadata">

**Author:** ![crxporter](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/crxporter/32/25734_2.png) [@crxporter](https://discourse.nodered.org/u/crxporter)\
**Post date:** [27 January 2023 21:21 UTC](https://discourse.nodered.org/t/using-node-red-contrib-unifi-with-a-unifi-wifi-network/74246/47 "2023-01-27T21:21:17Z")

</div>

Well - I'm not done yet 😉

To answer your original question, now that you're connected... I highly recommend you connect to this websocket:

`/wss/s/default/events`

And watch the output. You'll get push updates from your unifi devices with lists of who's connected to which wifi or LAN port! This is how I see who is home, based on MAC addresses of either a phone or smart watch between my wife and I.

Of course, you'll need to sort out the site name but I think you're very close on that.

Should look like this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/a/3a2b05782ac9c88e1f0e50e7c5318f686560bf2b.png)

As for a "thanks" - just a star on our project will be great! We're still in pretty active development on this so any input you have, please share! Either here (flag me or marcus) or on github!

Have fun!

---

<div class="post-metadata">

**Author:** ![ianh33](https://avatars.discourse-cdn.com/v4/letter/i/ea5d25/32.png) [@ianh33](https://discourse.nodered.org/u/ianh33)\
**Post date:** [27 January 2023 21:25 UTC](https://discourse.nodered.org/t/using-node-red-contrib-unifi-with-a-unifi-wifi-network/74246/48 "2023-01-27T21:25:08Z")

</div>

I'm new to this...... How do I star your project ?

---

<div class="post-metadata">

**Author:** ![crxporter](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/crxporter/32/25734_2.png) [@crxporter](https://discourse.nodered.org/u/crxporter)\
**Post date:** [27 January 2023 21:32 UTC](https://discourse.nodered.org/t/using-node-red-contrib-unifi-with-a-unifi-wifi-network/74246/49 "2023-01-27T21:32:35Z")

</div>

On nodered - if you log in you'll see "star ratings" on the right side on our site:

> **[node-red-contrib-unifi-os](https://flows.nodered.org/node/node-red-contrib-unifi-os)**
>
> Nodes to access UniFi data using endpoints and websockets

On GH there's just a star in the top right (by forks):

> **[GitHub - NRCHKB/node-red-contrib-unifi-os: Nodes to access UniFi data using...](https://github.com/NRCHKB/node-red-contrib-unifi-os)**
>
> Nodes to access UniFi data using endpoints and websockets - GitHub - NRCHKB/node-red-contrib-unifi-os: Nodes to access UniFi data using endpoints and websockets

If you don't have either account, don't worry about it. Knowing you're enjoying the effort is thanks enough!

---

<div class="post-metadata">

**Author:** ![ianh33](https://avatars.discourse-cdn.com/v4/letter/i/ea5d25/32.png) [@ianh33](https://discourse.nodered.org/u/ianh33)\
**Post date:** [27 January 2023 21:36 UTC](https://discourse.nodered.org/t/using-node-red-contrib-unifi-with-a-unifi-wifi-network/74246/50 "2023-01-27T21:36:55Z")

</div>

done both..... thank you.

I'm getting ""Error: connect ECONNREFUSED ::1:443"" with the websocket node, but i need to go now. I will get back on it on Sunday and report back. I'm puzzled why the websocket is trying to connect using port 443 instead of 8443.

Thank you once again  
Ian

---

<div class="post-metadata">

**Author:** ![crxporter](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/crxporter/32/25734_2.png) [@crxporter](https://discourse.nodered.org/u/crxporter)\
**Post date:** [27 January 2023 21:48 UTC](https://discourse.nodered.org/t/using-node-red-contrib-unifi-with-a-unifi-wifi-network/74246/51 "2023-01-27T21:48:06Z")

</div>

That's one that we have heard about before - when you have time, if you could provide more details on this GH issue, we will appreciate it:

> <https://github.com/NRCHKB/node-red-contrib-unifi-os/issues/54>
>
> It appears WebSocket interface only uses port 443, and doesn't use port configur…ed in the configuration node.
> My Unifi controller runs on port 8443, and WebSocket interface bound to the same port (tested with Python script).
> 
> Please expose WebSocket port through config node.

Thanks!!

---

<div class="post-metadata">

**Author:** ![ianh33](https://avatars.discourse-cdn.com/v4/letter/i/ea5d25/32.png) [@ianh33](https://discourse.nodered.org/u/ianh33)\
**Post date:** [28 January 2023 08:16 UTC](https://discourse.nodered.org/t/using-node-red-contrib-unifi-with-a-unifi-wifi-network/74246/52 "2023-01-28T08:16:12Z")

</div>

Some useful info re sites. I only have one site but I have given it a name lets say it is "mysite".

`/api/s/mysite/stat/health` doesn't work becuase UniFi doesn't recognise the name "mysite"

If you do a `/api/stat/sites` you find that UniFi uses "mysite" as a description and gives it a name like "kxk6adkb". This works `/api/s/kxk6adkb/stat/health`

---

<div class="post-metadata">

**Author:** ![ianh33](https://avatars.discourse-cdn.com/v4/letter/i/ea5d25/32.png) [@ianh33](https://discourse.nodered.org/u/ianh33)\
**Post date:** [29 January 2023 07:34 UTC](https://discourse.nodered.org/t/using-node-red-contrib-unifi-with-a-unifi-wifi-network/74246/53 "2023-01-29T07:34:16Z")

</div>

Websockets now work (thank you crxporter).... see GitHub.

crxporter..... can I be cheeky and lazy and ask if you would post your node red flow that filters the websocket events to find attached devices (e.g. mobile phone). There is an huge amount of information !!!

Ian

---

<div class="post-metadata">

**Author:** ![crxporter](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/crxporter/32/25734_2.png) [@crxporter](https://discourse.nodered.org/u/crxporter)\
**Post date:** [29 January 2023 15:22 UTC](https://discourse.nodered.org/t/using-node-red-contrib-unifi-with-a-unifi-wifi-network/74246/54 "2023-01-29T15:22:43Z")

</div>

I'm sorry, that's proprietary informations...

Nah of course I'll share it. I send all the `events` from websocket through this function node:

```plaintext
if (!('data' in msg.payload)) return;

const UnifiData = msg.payload.data;

const base = 'Unifi';

// Wifi connected devices

let MAC = global.get('Devices');
if (!Array.isArray(UnifiData)) return;

for (const thisDevice of MAC) {
    let Message = {
        payload: true,
        topic: thisDevice.device + base
    }

    for (const unifiDevice of UnifiData) {
        if (!unifiDevice.is_wired && thisDevice.wifi.toLowerCase() === unifiDevice.mac) {
            // Message.payload = true;
            node.send(Message);
            // node.warn(unifiDevice);
            break;
        }
    }

}

return;

```

And for this to work, you will need an object in your `global` context which has the MAC addresses and device names you're looking for:

```json
[{"device":"GarrettPhone","wifi":"AA:BB:CC:00:11:22"},{"device":"GarrettWatch","wifi":"AA:BB:CC:00:11:33"},{"device":"HeidiPhone","wifi":"AA:BB:CC:00:11:44"},{"device":"HeidiWatch","wifi":"AA:BB:CC:00:11:55"}]

```

Then you'll get messages from the function node that have a `msg.topic` identifying the device and `payload=true` if the device is online. This goes on to a trigger node with a timeout of 5 minutes ... if the device doesn't show as online for 5 minutes, I assume it's gone offline!  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/9/797824aea5a35c8e0081445979f3498ad53c0e1a.png)

Have fun!

---

<div class="post-metadata">

**Author:** ![ianh33](https://avatars.discourse-cdn.com/v4/letter/i/ea5d25/32.png) [@ianh33](https://discourse.nodered.org/u/ianh33)\
**Post date:** [29 January 2023 16:30 UTC](https://discourse.nodered.org/t/using-node-red-contrib-unifi-with-a-unifi-wifi-network/74246/55 "2023-01-29T16:30:01Z")

</div>

Soon you will wish I would just 'go away' !!!!

I have set (and checked) global 'devices' as per your array adding my own details.

I am getting "TypeError: MAC is not iterable". Is is becuase I am using websocket on the network app as opposed to a hardware controller ?

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [29 January 2023 16:37 UTC](https://discourse.nodered.org/t/using-node-red-contrib-unifi-with-a-unifi-wifi-network/74246/56 "2023-01-29T16:37:43Z")

</div>

What does you global object look like?  
it should be similar to the below (ignore alarm state)

 ![Screenshot 2023-01-29 at 16.36.17](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/c/4c03e7bd74de13e881296e9f7fd48f11487fed12.png)

also `devices` or `Devices` 😉

---

<div class="post-metadata">

**Author:** ![ianh33](https://avatars.discourse-cdn.com/v4/letter/i/ea5d25/32.png) [@ianh33](https://discourse.nodered.org/u/ianh33)\
**Post date:** [29 January 2023 16:56 UTC](https://discourse.nodered.org/t/using-node-red-contrib-unifi-with-a-unifi-wifi-network/74246/57 "2023-01-29T16:56:13Z")

</div>

'Devices' plus restart did the trick, thank you

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [30 March 2023 16:56 UTC](https://discourse.nodered.org/t/using-node-red-contrib-unifi-with-a-unifi-wifi-network/74246/58 "2023-03-30T16:56:24Z")

</div>

Closed on 30 March 2023 16:56 UTC

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.

[Previous page](https://discourse.nodered.org/t/using-node-red-contrib-unifi-with-a-unifi-wifi-network/74246.md?page=2)
