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

"Error: No Site Context"

Do it with the default site:

/api/s/default/stat/health

same result

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

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.

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" ?

Well - I'm not done yet :wink:

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:

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!

1 Like

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

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

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

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

1 Like

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

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:

Thanks!!

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

1 Like

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

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:

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:

[{"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

Have fun!

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 ?

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

also devices or Devices :wink:

'Devices' plus restart did the trick, thank you

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