Isaksson / node-red-contrib-unifi

MIT License
52 stars 20 forks source link

Disable & Enable Client Unifi Controller #28

Closed jeylites closed 4 years ago

jeylites commented 4 years ago

@Isaksson

This seems to be a cool plugin but I'm trying to figure out how to use it correctly as I'm new to Node-red

I manage to link my Unifi controller to Node-red and I see a blue dot. Now how do I create automation.

The idea is to have MQTT to Disable & Enable a specific Client on Unifi Controller by flicking an ON / OFF switch.

A sample of this setup would be nice. Thank you

Isaksson commented 4 years ago

First create a very simple flow just to make sure that you have communication to your controller. firstflow1 If you see the green "dot" on the unifi node and that you get output on your debug tab then you know that you have a working connection between the node and your controller.

To disable a client then you will need to inject the command to the Unifi Node. use a Function node and add this code to it to block a client msg.payload = { command: "blockClient", mac: "client MAC address" } return msg; and use this to enable a client again msg.payload = { command: "unblockClient", mac: "client MAC address" } return msg;

So what you could do to automate this, example if you have a mqtt message with payload block and payload unblock then you could use a function node with this code if (msg.payload === 'block'){ msg.payload = { command: "blockClient", mac: "client MAC address" } } else if (msg.payload === 'unblock'){ msg.payload = { command: "unblockClient", mac: "client MAC address" } } return msg;

firstflow2

Let me know if you need any more help with this.

jeylites commented 4 years ago

@Isaksson

So I tried the first step to make sure I'm communicating with the controller when I debug I get the following and Node Red crashes.

Welcome to Node-RED
===================

21 Apr 12:00:04 - [info] Node-RED version: v1.0.5
21 Apr 12:00:04 - [info] Node.js  version: v12.16.2
21 Apr 12:00:04 - [info] Darwin 19.4.0 x64 LE
21 Apr 12:00:05 - [info] Loading palette nodes
21 Apr 12:00:06 - [info] Settings file  : /Users/jk/.node-red/settings.js
21 Apr 12:00:06 - [info] Context store  : 'default' [module=memory]
21 Apr 12:00:06 - [info] User directory : /Users/jk/.node-red
21 Apr 12:00:06 - [warn] Projects disabled : editorTheme.projects.enabled=false
21 Apr 12:00:06 - [info] Flows file     : /Users/jk/.node-red/flows_Projects.localdomain.json
21 Apr 12:00:06 - [info] Server now running at http://127.0.0.1:1880/
21 Apr 12:00:06 - [warn] 

---------------------------------------------------------------------
Your flow credentials file is encrypted using a system-generated key.

If the system-generated key is lost for any reason, your credentials
file will not be recoverable, you will have to delete it and re-enter
your credentials.

You should set your own key using the 'credentialSecret' option in
your settings file. Node-RED will then re-encrypt your credentials
file using your chosen key the next time you deploy a change.
---------------------------------------------------------------------

21 Apr 12:00:06 - [info] Starting flows
21 Apr 12:00:06 - [info] Started flows
21 Apr 12:00:48 - [info] Stopping modified nodes
21 Apr 12:00:48 - [info] Stopped modified nodes
21 Apr 12:00:48 - [info] Starting modified nodes
21 Apr 12:00:48 - [info] Started modified nodes
21 Apr 12:00:51 - [red] Uncaught Exception:
21 Apr 12:00:51 - TypeError: command.toLowerCase is not a function
    at /Users/jk/.node-red/node_modules/node-red-contrib-unifi/unifi.js:43:32
    at /Users/jk/.node-red/node_modules/node-red-contrib-unifi/unifi-helper.js:1417:25
    at /Users/jk/.node-red/node_modules/async/dist/async.js:321:20
    at check (/Users/jk/.node-red/node_modules/async/dist/async.js:4459:32)
    at /Users/jk/.node-red/node_modules/node-red-contrib-unifi/unifi-helper.js:1369:17
    at next (/Users/jk/.node-red/node_modules/async/dist/async.js:4453:13)
    at Request._callback (/Users/jk/.node-red/node_modules/node-red-contrib-unifi/unifi-helper.js:1402:25)
    at Request.self.callback (/Users/jk/.node-red/node_modules/request/request.js:185:22)
    at Request.emit (events.js:310:20)
    at Request.<anonymous> (/Users/jk/.node-red/node_modules/request/request.js:1154:10)
Projects:~ jk$ 
Isaksson commented 4 years ago

How did you do the installation of the module?

jeylites commented 4 years ago

@Isaksson

As to the Error, I was running Node-Red on MacOSX, I don't know why it keep giving me a crash. It hasn't crashed on me ever since I started running on a Raspberry Pi. Both installed were done through Node-red modules...

Your example is pretty clear and I got it to work. I could be wrong, but it seems like for the MQTT example above you have two topics in one MQTT node.

How did you do that? "Payload=block & unblock"

Would you mind giving me an example of that?

Initially, I was experimenting with blocking & unblocking one MAC address. But since I needed to include a second MAC address, I made a new function node and tying it back to the Unifi Node, like the example below. Is there a way to add more MAC addresses by using one Function Node. Or what I'm doing is the way to go?

Screen Shot 2020-04-22 at 9 32 50 PM

One thing I realize the Site properties in the Unifi Node has to be named default or else it wouldn't work. I'm not sure if it's intentional or a bug but this needs to be explained in the readme

Screen Shot 2020-04-22 at 9 39 16 PM

Isaksson commented 4 years ago

I dont see any problems with the use of two or more function nodes. From my example I just illustrated that the payload from my MQTT topic could be block or unblock so it was not multiple topics. about the Site name, it is the name of your current site. Yes it has to be the name of your site but not always default.

877dev commented 4 years ago

@Isaksson thanks for these nodes, I had some issue due to my lack of knowledge but managed to get the block/unblock client working with my own (inefficient) function: image

I'm using Big Timer to limit access to a client during night hours: image

Thanks :)

alexsahka commented 4 years ago

Anyone experiencing a problem with a client cant gets an IP address after unblocking? I'm using about the same Node Red code as 877dev showing above, for night time device "blockage"

jeylites commented 4 years ago

Have you tried giving the client a static IP?

On Thu, May 14, 2020 at 7:44 PM alexsahka notifications@github.com wrote:

Anyone experiencing a problem with a client cant gets an IP address after unblocking? I'm using about the same Node Red code as 877dev showing above, for night time device "blockage"

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Isaksson/node-red-contrib-unifi/issues/28#issuecomment-628959877, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACGDVQAVXLUON6YAJWGJSS3RRSGAPANCNFSM4MLMDVQA .

-- Sent from Iphone Mobile

alexsahka commented 4 years ago

This client with a static IP and actually all of my client with static IPs

jeylites commented 4 years ago

I have a similar issue as well, but now I’m starting to think if we could do a Auto reconnect of MAC address through the API.

So after it unblock the client, do an auto reconnect after a few seconds...

There’s is reconnect feature on the API, just not sure if it works this way....

alexsahka commented 4 years ago

I was trying manually reconnect the device from UNIFI GUI and not working (for some reasons not assigning IP), and only what is working is pushing USG (router) provisioning. I'm thinking maybe restarting the DHCP server in USG with Node-red SSH node may be a solution?

877dev commented 4 years ago

Anyone experiencing a problem with a client cant gets an IP address after unblocking? I'm using about the same Node Red code as 877dev showing above, for night time device "blockage"

@alexsahka yes I have this happening myself, after unblocking at 7am my son said he could access LAN but not WAN i.e. internet. I set a static IP address and my son reports it has helped. But what really worked I think was restarting the cloud key gen2. Not sure if that can be done via the API.

Isaksson commented 4 years ago

I was trying manually reconnect the device from UNIFI GUI and not working (for some reasons not assigning IP), and only what is working is pushing USG (router) provisioning. I'm thinking maybe restarting the DHCP server in USG with Node-red SSH node may be a solution?

If you have assigned static IP then restarting DHCP should not affect the client.

Do you have this issue every time when disable/enable client or does it happens sometimes?

alexsahka commented 4 years ago

Do you have this issue every time when disable/enable client or does it happens sometimes?

Never had this issue before, as soon I start doing disable/enable a client, USG won't assign properly IP after enabling client, and like I mentioning before, always working solution is to push router provisioning. Side note. This WIFI is WPA Enterprise with Radius Profile and VLAN, but the client connecting to WIFI without any problem, and the only problem is IP address.

Isaksson commented 4 years ago

have you tried the command reconnectClient : Reconnect Client { command: "reconnectClient", mac: "client MAC address" }

So you use dynamic ip address on the client?

alexsahka commented 4 years ago

have you tried the command reconnectClient : Reconnect Client { command: "reconnectClient", mac: "client MAC address" }

So you use dynamic ip address on the client?

Is it different from clicking Reconnect from UNIFI GUI? image

Isaksson commented 4 years ago

That would to the same. Do the same thing happen if you do the block and unblock from the web GUI and not from this Node?

alexsahka commented 4 years ago

I'll try this later, Thanks.

jeylites commented 4 years ago

@Isaksson

I'm currently stuck at trying to get Homebridge MQTT Thing plugin to talk to node-red topics I have created.

So the current setup I have a topic for Unblock and another topic for Block clients. On the MQTT plugin, how do I get this topic to corresponds as an ON / OFF switch?

Screen Shot 2020-05-17 at 11 40 39 AM

alexsahka commented 4 years ago

That would to the same. Do the same thing happen if you do the block and unblock from the web GUI and not from this Node?

Last two days every morning USG assigns IP properly, but not today. And blocking/unblocking even from UNIFI GUI doesn't assign IP properly. So based on this I think this is the UNIFI bug and we cannot use this as scheduling tool reliably. And the only proven solution is to push USG (router) to provision.

Isaksson commented 4 years ago

That would to the same. Do the same thing happen if you do the block and unblock from the web GUI and not from this Node?

Last two days every morning USG assigns IP properly, but not today. And blocking/unblocking even from UNIFI GUI doesn't assign IP properly. So based on this I think this is the UNIFI bug and we cannot use this as scheduling tool reliably. And the only proven solution is to push USG (router) to provision.

I have now added a new function to force a provision. please test that function, its untested from my part. { command: "forceProvision", mac: "device MAC address" } its in version 0.1.8 its pushed right now to NPM and I have requested refresh at node-RED

alexsahka commented 4 years ago

I have now added a new function to force a provision. please test that function, its untested from my part. { command: "forceProvision", mac: "device MAC address" }

Is the mac is routers mac address? Or the client mac address.

Isaksson commented 4 years ago

I have now added a new function to force a provision. please test that function, its untested from my part. { command: "forceProvision", mac: "device MAC address" }

Is the mac is routers mac address? Or the client mac address.

It's the device that you want to force the provision on, so in this case the mac of your USG

alexsahka commented 4 years ago

OK, thanks, that's is a stupid question on my part.

Isaksson commented 4 years ago

@Isaksson

I'm currently stuck at trying to get Homebridge MQTT Thing plugin to talk to node-red topics I have created.

So the current setup I have a topic for Unblock and another topic for Block clients. On the MQTT plugin, how do I get this topic to corresponds as an ON / OFF switch?

Screen Shot 2020-05-17 at 11 40 39 AM

I have no idea Homebridge MQTT Thing plugin works but i guess that you will config a topic for command (that you will use for on / off) and one topic for result (that will respond if it is on / off) So that the plugin could keep track of current status.

But I have never used that plugin so you need to tell me more about it and what you have done so far and what is not working for you.