Sample UnifiPOE - Supergiovane/node-red-contrib-knx-ultimate GitHub Wiki
KNX-Ultimate can turn on or off POE on a single port or multiple port of an unifi switch
To make this possible, you need an unifi controller, an unifi switch and the node node-red-contrib-unifi
Firs of all, configure the unifi node, by setting the IP, username and password of your unifi controller. Then open the function node and do the following: Write the unifi switch "id" field, in the sDevice_id field. Then write the port number in the "port_idx" field.
Copy this code and paste it into your flow
View code
Adjust the nodes according to your setup
[{"id":"b6ea19e2.81cdf","type":"Unifi","z":"d61f370f.0413c","name":"","ip":"192.168.1.3","port":8443,"site":"default","command":"70","unifios":false,"x":690,"y":420,"wires":[["d5503273.a295"]]},{"id":"a68490a6.358ef8","type":"function","z":"d61f370f.0413c","name":"Yealink T45G","func":"// Write the unifi switch \"id\" field, in the sDevice_id field.\n// Then write the port number in the \"port_idx\" field. \n// In this example, the port number is 2\n\n// Switch to be controlled\nvar sDevice_id=\"PUT HERE YOUR SWITCH'S ID\";\n// Port to be controlled\nvar iPort=10; // PUT HERE YOUR SWITCH'S ID\n\nnode.status({fill:msg.payload ? \"green\" : \"red\",shape:\"dot\",text: \"Unifi \" + sDevice_id});\n\n\n// You can control more than one port at the same time, by\n// by replacing \"port_overrides:\" section of the JSON with\n// a comma separated list of ports, like this:\n// port_overrides: [\n// {\"port_idx\": 2, \"poe_mode\": sPOE },\n// {\"port_idx\": 3, \"poe_mode\": sPOE },\n// {\"port_idx\": 4, \"poe_mode\": sPOE }\n// ]\n\nvar sPOE = \"auto\";\nif (msg.payload === true){\n // POE on\n sPOE =\"auto\";\n}else\n{\n // POE off\n sPOE =\"off\";\n}\nmsg.payload = { command: \"setPortProfiles\",\ndevice_id: sDevice_id, \n\nport_overrides: [\n{\"port_idx\": iPort, \"poe_mode\": sPOE }\n\n]}; // You can add many ports you want, just add another row, separated by a comma.\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":220,"wires":[["7f880c2e.140a5c"]]},{"id":"2647f7d5.e2db68","type":"link in","z":"d61f370f.0413c","name":"Unifi controller","links":["7f880c2e.140a5c"],"x":115,"y":420,"wires":[["efc4a4d3.565378"]]},{"id":"7f880c2e.140a5c","type":"link out","z":"d61f370f.0413c","name":"","links":["2647f7d5.e2db68"],"x":455,"y":220,"wires":[]},{"id":"efc4a4d3.565378","type":"delay","z":"d61f370f.0413c","name":"","pauseType":"rate","timeout":"10","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":240,"y":420,"wires":[["d2896974.c47f9"]]},{"id":"d5503273.a295","type":"function","z":"d61f370f.0413c","name":"Elaboro","func":"// Leggo gli overrides porte attuali, e poi ci aggiungo quello \n// salvato nel flow, perchè all'unifi devo rimandare sempre tutte\n// le porte, altrimenti fa l'override solo della porta richiesta\n// e resetta le altre a default.\n\nlet oMsg = flow.get(\"payload\");\nlet oElencoUnifi = msg.payload;\nif (oElencoUnifi === undefined) {\n node.status({ fill: \"red\", shape: \"dot\", text: \"Flow non impostato\" });\n return;\n}\nlet oFoundUnifi = oElencoUnifi[0].filter(x => x._id === oMsg.device_id);\nif (oFoundUnifi === undefined) {\n node.status({ fill: \"red\", shape: \"dot\", text: \"Device non trovato \" + oMsg.device_id });\n return;\n}\nlet oDeviceUnifi = oFoundUnifi[0];\n\n// Cerco ed elimino il precedente override della stessa porta\n//console.log(oDeviceUnifi.port_overrides[0].port_idx);\nfor (let index = 0; index < oDeviceUnifi.port_overrides.length; index++) {\n const element = oDeviceUnifi.port_overrides[index];\n if (element.port_idx === oMsg.port_overrides[0].port_idx) {\n //console.log(element);\n oDeviceUnifi.port_overrides.splice(index, 1);\n //console.log(oDeviceUnifi.port_overrides)\n break;\n }\n}\n// Aggiungo l'override passato dal msg, all'elenco overrides\nfor (let index = 0; index < oDeviceUnifi.port_overrides.length; index++) {\n const element = oDeviceUnifi.port_overrides[index];\n oMsg.port_overrides.push(element);\n}\nnode.status({ fill: \"green\", shape: \"dot\", text: \"Ports override: \" + oMsg.port_overrides.length });\nreturn {payload:oMsg};","outputs":1,"noerr":0,"initialize":"","finalize":"","x":820,"y":420,"wires":[["f7890cfa.97af68"]]},{"id":"d2896974.c47f9","type":"change","z":"d61f370f.0413c","name":"","rules":[{"t":"set","p":"payload","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":414,"y":420,"wires":[["ba7b3caf.9acfe8"]]},{"id":"ba7b3caf.9acfe8","type":"change","z":"d61f370f.0413c","name":"Query","rules":[{"t":"set","p":"payload","pt":"msg","to":"Banana","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":564,"y":420,"wires":[["b6ea19e2.81cdf"]]},{"id":"f7890cfa.97af68","type":"Unifi","z":"d61f370f.0413c","name":"","ip":"192.168.1.3","port":8443,"site":"default","command":"70","unifios":false,"x":950,"y":420,"wires":[[]]},{"id":"8a4c3a9.dfcaf48","type":"Unifi","z":"d61f370f.0413c","name":"","ip":"192.168.1.3","port":8443,"site":"default","command":"70","unifios":false,"x":390,"y":100,"wires":[["15e5e962.43e4ef"]]},{"id":"eecc2793.b952e8","type":"inject","z":"d61f370f.0413c","name":"Discovery","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":100,"wires":[["8a4c3a9.dfcaf48"]]},{"id":"15e5e962.43e4ef","type":"debug","z":"d61f370f.0413c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":550,"y":100,"wires":[]},{"id":"b1029521.bdf4","type":"comment","z":"d61f370f.0413c","name":"This sends a POE command, by preserving the state of other ports","info":"","x":340,"y":380,"wires":[]},{"id":"7a1eb8b0.cc736","type":"comment","z":"d61f370f.0413c","name":"Press Discovery and search the ID of the switch to be controlled","info":"","x":370,"y":60,"wires":[]},{"id":"688861e3.7016a","type":"inject","z":"d61f370f.0413c","name":"ON","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":170,"y":200,"wires":[["a68490a6.358ef8"]]},{"id":"7fd730ff.bc65d8","type":"inject","z":"d61f370f.0413c","name":"OFF","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":170,"y":240,"wires":[["a68490a6.358ef8"]]},{"id":"d09052ed.87f73","type":"comment","z":"d61f370f.0413c","name":"In the function, put the switch ID and the port to be controlled","info":"","x":480,"y":180,"wires":[]}]