Toggle POE State of UniFi Switch Ports
This flow supports to enable/disable the POE power of UniFi Switches. This flow makes use of the node-red-contrib-unifi-os plugin.
Flow input variables:
- deviceId: the ID of the UniFi switch
- port: The port number of the switch you want to manage
- poe_state: Either "auto" or "off" to enable/disable the port
[{"id":"ba3e07e41cc5fcc4","type":"subflow","name":"Toggle POE Port","info":"","category":"","in":[{"x":120,"y":240,"wires":[{"id":"b2f1627fd3a65a47"}]}],"out":[{"x":1510,"y":220,"wires":[{"id":"f6c8745bbd7b12f2","port":0}]}],"env":[{"name":"device_id","type":"str","value":"","ui":{"icon":"font-awesome/fa-desktop","label":{"en-US":"Device ID"},"type":"input","opts":{"types":["str"]}}},{"name":"port","type":"num","value":"","ui":{"label":{"en-US":"Port"}}},{"name":"poe_state","type":"str","value":"","ui":{"icon":"font-awesome/fa-check-square-o","type":"select","opts":{"opts":[{"l":{"en-US":"Auto"},"v":"auto"},{"l":{"en-US":"Off"},"v":"off"}]}}}],"meta":{"version":"0.0.1","author":"Fritz Richter <[email protected]>","keywords":"Unifi, POE"},"color":"#3FADB5","outputLabels":["State"],"icon":"node-red-contrib-unifi-os/unifi.png","status":{"x":1340,"y":220,"wires":[{"id":"d9dcf77c67ec8b0f","port":0},{"id":"c475b51701139440","port":0}]}},{"id":"b2f1627fd3a65a47","type":"unifi-request","z":"ba3e07e41cc5fcc4","name":"Get all devices","accessControllerNodeId":"311744fa7b96aaf5","endpoint":"/proxy/network/api/s/default/stat/device","method":"GET","data":"","dataType":"json","responseType":"json","x":260,"y":240,"wires":[["148e0bb40930ead8"]]},{"id":"148e0bb40930ead8","type":"function","z":"ba3e07e41cc5fcc4","name":"Prepare Port Override API Request","func":"var poe_mode = env.get(\"poe_state\");\nvar poe_port = env.get(\"port\");\nvar device_id = env.get(\"device_id\");\n\n//node.warn([\"poe_mode=\", poe_mode]);\n//node.warn([\"poe_port=\", poe_port]);\n//node.warn([\"device_id=\", device_id]);\n\nmsg.payload.data.forEach(device => {\n if (device._id == device_id) {\n var changed = false;\n var newPort = true;\n var port_overrides = [];\n\n //node.warn([\"found device=\", device]);\n\n //get all current overrides\n device.port_overrides.forEach(port => {\n if (port.port_idx == poe_port) {\n if (port.poe_mode != poe_mode) {\n //node.warn([\"switching port to=\", poe_mode]);\n changed = true;\n port.poe_mode = poe_mode;\n }\n newPort = false;\n }\n port_overrides.push(port)\n });\n \n if (newPort) {\n //Get information from the new port to create override\n device.port_table.forEach(port => {\n if (port.port_idx == poe_port) {\n port_overrides.push({ \"port_idx\": port.port_idx, \"poe_mode\": poe_mode, \"portconf_id\": port.portconf_id, port_security_mac_address: [], stp_port_mode: true, autoneg: true, port_security_enabled: false })\n }\n });\n changed = true;\n }\n if (changed) {\n msg.payload = {\n \"endpoint\": \"/proxy/network/api/s/default/rest/device/\" + device_id,\n \"method\": \"PUT\",\n \"data\": { \"port_overrides\": port_overrides }\n };\n }\n\n\n\n }\n \n});\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":240,"wires":[["8afb7599e6322a20","ee4ef7a3cde9e44f"]]},{"id":"907638a51d72e333","type":"unifi-request","z":"ba3e07e41cc5fcc4","name":"Port override","accessControllerNodeId":"311744fa7b96aaf5","endpoint":"","method":"PUT","data":"","dataType":"str","responseType":"json","x":930,"y":200,"wires":[["d9dcf77c67ec8b0f","f6c8745bbd7b12f2"]]},{"id":"d9dcf77c67ec8b0f","type":"function","z":"ba3e07e41cc5fcc4","name":"Format Status","func":"msg.payload = ({ fill: \"green\", text: \"Success\" });\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1120,"y":200,"wires":[[]]},{"id":"8afb7599e6322a20","type":"switch","z":"ba3e07e41cc5fcc4","name":"","property":"payload.endpoint","propertyType":"msg","rules":[{"t":"nnull"},{"t":"null"}],"checkall":"false","repair":false,"outputs":2,"x":750,"y":240,"wires":[["907638a51d72e333"],["c475b51701139440"]]},{"id":"c475b51701139440","type":"function","z":"ba3e07e41cc5fcc4","name":"Format Status","func":"msg.payload = ({ fill: \"red\", text: \"Error\" });\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1120,"y":260,"wires":[[]]},{"id":"f6c8745bbd7b12f2","type":"function","z":"ba3e07e41cc5fcc4","name":"Return Value","func":"var poe_mode = env.get(\"poe_state\");\nmsg.payload = (poe_mode == \"auto\") ? true : false;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1110,"y":140,"wires":[[]]},{"id":"ee4ef7a3cde9e44f","type":"debug","z":"ba3e07e41cc5fcc4","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":700,"y":400,"wires":[]},{"id":"311744fa7b96aaf5","type":"unifi-access-controller","name":"Default","controllerIp":"10.0.1.1","controllerPort":"443","controllerType":"UniFiOSConsole"}]