Skip to content
archang3l-mediaPublic

About

Small Python Script that leverages uhubctl to turn some USB Fans inside my rack on/off depending on the Temperature

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

RackTemp - Automated Rack Fan Controller

Temperature-based USB fan controller that monitors Home Assistant temperature sensors and automatically controls USB-powered fans via uhubctl.

Features

  • Monitors temperature via Home Assistant API
  • Hysteresis control (turns on at 28°C, off at 25°C)
  • Failsafe: activates fans on API errors
  • Automatic restart on failure
  • Systemd service integration

Requirements

System Dependencies

sudo apt install uhubctl

Python Dependencies

pip install requests

Or using requirements.txt:

pip install -r requirements.txt

Installation

  1. Clone or copy files to your target directory (e.g., /home/<username>/PiTemp)

  2. Make the script executable:

chmod +x racktemp.py
  1. Set your Home Assistant configuration:
export HASS_API_TOKEN="your_token_here"
export HASS_URL="your_home_assistant_url"
export HASS_SENSOR="sensor.your_sensor_name"
  1. Test the script manually:
python3 racktemp.py

Systemd Service Setup

  1. Edit temp.service and update:

    • User= with your username
    • WorkingDirectory= with your installation path
    • ExecStart= with the correct path to racktemp.py
    • Environment=HASS_API_TOKEN= with your actual token
    • Environment=HASS_URL= with your Home Assistant URL
    • Environment=HASS_SENSOR= with your sensor name
    • StandardOutput= and StandardError= paths
  2. Create log directory:

mkdir -p ~/PiTemp/log
  1. Copy service file:
sudo cp temp.service /etc/systemd/system/rack-fan-control.service
  1. Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable rack-fan-control.service
sudo systemctl start rack-fan-control.service
  1. Check status:
sudo systemctl status rack-fan-control.service

Configuration

Edit racktemp.py to customize:

  • TEMP_ON = 28 - Temperature threshold to turn fans ON (°C)
  • TEMP_OFF = 25 - Temperature threshold to turn fans OFF (°C)
  • HASS_URL - Your Home Assistant URL (set in environment)
  • HASS_SENSOR - Your sensor name (set in environment)
  • control_usb_port("1-1", "1,2,3,4", state) - USB hub location and ports

Systemd Service File

[Unit]
Description=Rack Fan Control Service
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=<username>
WorkingDirectory=/home/<username>/PiTemp
ExecStart=/usr/bin/python3 /home/<username>/PiTemp/racktemp.py
Environment=HASS_API_TOKEN=<insert_your_token_here>
Environment=HASS_URL=<your_home_assistant_url>
Environment=HASS_SENSOR=sensor.<your_sensor_name>
Restart=always
RestartSec=10
StandardOutput=append:/home/<username>/PiTemp/log/rack-fan-control.log
StandardError=append:/home/<username/PiTemp/log/rack-fan-control.log

[Install]
WantedBy=multi-user.target

Troubleshooting

View logs:

# Service logs
sudo journalctl -u rack-fan-control.service -f

# Application logs
tail -f ~/PiTemp/log/rack-fan-control.log

Common issues:

  • uhubctl not found: Install with sudo apt install uhubctl
  • Permission denied: Ensure user has USB control permissions or run service as root
  • API errors: Verify HASS_API_TOKEN and network connectivity

License

MIT

About

Small Python Script that leverages uhubctl to turn some USB Fans inside my rack on/off depending on the Temperature

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages