Command line switch does nothing

I had a lot of problems with command line switch! it used to give me command failed!!I tried many commands but had no chance!until i added “&” at the end of the command.then the button works and gives me no errors but it does nothing it just turns on and off but it seems like it doesn’t do the command.
Example:

switch:
 - platform: command_line
   switches:
     amp:
       command_on: irsend SEND_ONCE amp KEY_POWER &
       command_off: irsend SEND_ONCE amp KEY_POWER &
1 Like

Hi @bianconero11, does your command work when you’re in a terminal?
When your HA runs in a virtualenv, activate it before running the command.

Edit:
With the & you run your command in the background, so get no errors in HA.

Can you please clear the “&” part? And yes the command works perfectly in the terminal with the HA user

HA catches the exit status of a command to determine if it ran sucessfully.
You can use $? to find out the exit status of a command.

A simple example:

server:~$ echo 'hello'
hello
server:~$ echo $?
0

Zero means command executed successfully. HA shows no error.

Another example which will fail:

server:~$ fantasycommand
-bash: fantasycommand: command not found
server:~$ echo $?
127

127 means command not found. HA shows command failed.

If you start your command with the ‘&’, it fails in the background.

server:~$ fantasycommand &
-bash: fantasycommand: command not found
[1] 25938
[1]+  Exit 127                fantasycommand
server:~$ echo $?
0

Zero means command executed successfully. HA shows no error.

Maybe your command runs with the full path to irsend?
command_on: "/usr/bin/irsend SEND_ONCE amp KEY_POWER"

omg it worked!!! thank you sooooo much !thank you !i can’t say that enough!!! if it doesn’t bother you can you explain to me why this works on HA but the other command doesn’t ?

If you run a command, the linux shell looks in different directories for it.
These directories are listed in the $PATH variable.

server:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

I assume that the vrtualenv where your HA runs in, has a problem with this $PATH variable, so it can’t find irsend.

If you run the command with the full path, HA will find it.

oh okay got it ! thanks a lot!

Depending on how you’ve set up HA the virtual environment may not have the correct environment variables set. There was a problem with the systemd service so I suggest to double check your autostart routine. (see https://home-assistant.io/docs/autostart/)