How to Change the Fan Speed on Ubiquiti XG 8 PoE

How to control the fan speed on the Ubiquiti Switch Pro XG 8 PoE, and automating the change with a bash script and systemd service.