OpenWRT WiFi switch not working — commands work in terminal, but not in Home Assistant

Hi everyone,

I’m trying to control the WiFi of my OpenWRT router from Home Assistant using a command_line switch. The goal is to enable or disable a wireless interface (wifinet2). I’ve set up the switch using the new-style command_line configuration (not under switch: directly).

The problem:

All SSH commands (command_on, command_off, and command_state) work perfectly when I run them manually from the Home Assistant terminal (SSH add-on or shell).

In Home Assistant, the switch shows up in the UI, but toggling it doesn’t seem to do anything.

The state is never updated correctly — it always shows as “off”, regardless of the actual WiFi status.

Here is my current configuration:

command_line:

  • switch:
    name: “OpenWRT WiFi Control”
    command_on: “ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no usuario@ip_router ‘uci set wireless.wifinet2.disabled=0 && uci commit wireless && wifi reload’”
    command_off: “ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no usuario@ip_router ‘uci set wireless.wifinet2.disabled=1 && uci commit wireless && wifi reload’”
    command_state: “ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no usuario@ip_router ‘uci -q get wireless.wifinet2.disabled || echo 0’”
    value_template: “{{ value == ‘0’ }}”
    scan_interval: 60

Additional info:

I’m using key-based SSH authentication (no password).

I’ve verified that Home Assistant’s user has SSH access to the router.

The commands are effective when run directly from the terminal, including the exact uci and wifi reload syntax.

Any idea what might be going wrong? Could it be related to how Home Assistant executes the commands or reads the output? Thanks in advance!

The SSH addon is another container with its own environment, so you might have set up SSH there, but when you run the command line as a script in HA, then it is run in the HA container with another environment, where SSH is not configured.

Thank you for pointing me in the right direction! The issue was indeed related to SSH environment isolation between HA’s container and the SSH add-on. Here’s how I solved it for future reference:

:wrench: Solution

  1. SSH Setup in HA Container:

    • Generated SSH keys inside HA’s container (critical!):
      ssh-keygen -t ed25519 -f /config/.ssh/id_ed25519
      ssh-copy-id -i /config/.ssh/id_ed25519.pub [email protected]
      
    • Verified permissions:
      chmod 600 /config/.ssh/id_ed25519
      
  2. YAML Configuration:
    Used absolute paths and strict options to avoid conflicts:

    command_line:
      - switch:
          name: "OpenWRT WiFi"
          command_on: "/usr/bin/ssh -i /config/.ssh/id_ed25519 -o StrictHostKeyChecking=no [email protected] 'uci set wireless.wifinet2.disabled=0 && uci commit wireless && wifi reload'"
          command_off: "/usr/bin/ssh -i /config/.ssh/id_ed25519 -o StrictHostKeyChecking=no [email protected] 'uci set wireless.wifinet2.disabled=1 && uci commit wireless && wifi reload'"
          command_state: "/usr/bin/ssh -i /config/.ssh/id_ed25519 -o StrictHostKeyChecking=no [email protected] 'uci -q get wireless.wifinet2.disabled || echo 1'"
          value_template: '{{ value == "0" }}'
    

:rotating_light: Key Insights

  • Context Matters: HA runs commands in its own container, so SSH keys must be generated there (not in the SSH add-on).
  • Debug Tip: Test commands manually first via HA’s terminal:
    sudo -u homeassistant ssh -i /config/.ssh/id_ed25519 [email protected] "uci get wireless.wifinet2.disabled"
    

:star2: Alternative Approaches

For those still struggling:

  • Option 1: Use the SSH & Web Terminal add-on with allow_exec: true in its config.
  • Option 2: Expose OpenWRT’s REST API (via luci-app-uhttpd) and use HA’s rest_command.

Thanks again for the help! This thread saved me hours of debugging.

1 Like

I use this script since month for do same thing but from HA version 2025.5 it doesn’t work. Hope it will fix soon