Command line switch problem

I have a working hyperion ambilight and I have also successfully integrated Philips hue to act like ambilight. I have a problem setting up command_line switch, to turn on and off Philips Hue as ambilight. If I put the following commands to terminal everything works greatly. My commands:

To turn Philips Hue ambilight on:
sudo /usr/bin/hyperiond /etc/hyperion/hyperionhue.config.json

To turn Philips Hue ambilight off:
sudo pkill -f hyperionhue

My code in switches.yaml:

- platform: command_line
  switches:
    hyperionhue:
      command_on: '/home/hass/.homeassistant/hyperion/hyperionhuestart.sh'
      command_off: '/home/hass/.homeassistant/hyperion/hyperionhuestop.sh'

And .sh files (they only have one line)

hyperionhuestart.sh
sudo /usr/bin/hyperiond /etc/hyperion/hyperionhue.config.json

hyperionhuestop.sh
sudo pkill -f hyperionhue

Home Assistant logs the following error, when I try to turn on the switch (it loads successfully in frontend).
homeassistant.components.switch.command_line: Command failed: /home/hass/.homeassistant/hyperion/hyperionhuestart.sh

Any help would be greatly appreciated

do the shell scripts themselves work when you run them?

No, unfortunately scripts themselves don’t run…there must be a problem. But the exact same commands work in terminal without any issues…

If I make a code like this:

- platform: command_line
  switches:
    hyperionhue:
      command_on: "sudo /usr/bin/hyperiond /etc/hyperion/hyperionhue.config.json"
      command_off: "sudo pkill -f hyperionhue"

I get the following error:

homeassistant.components.switch.command_line: Command failed: sudo /usr/bin/hyperiond /etc/hyperion/hyperionhue.config.json

Have you granted the scripts so that the homeassistant user can execute them? (location, chmod, chown)

The HA user by default does not have sudo permissions. So your script is failing when it gets to the sudo password which doesn’t exist for your HA user.

When you ran the commands you were likely under user “pi” or whatever your default user is. Go to the page for the installer you used and there are instructions for changing to your HA user. When testing anything for HA to use it should be from this user.

@silvrr I am in the same boat as @SmartValley in trying to run a rsync command.

The All-In-One installer documentation page states to change to the HA user:

sudo su -s /bin/bash homeassistant

However, I am not running the rsync command as sudo.

rsync -azh -e ssh --delete --exclude '*.db' /home/hass/.homeassistant/ root@pi_ipaddress:/mnt/usb_1/FileSync/AllFiles/HomeAssistant/

I can run it just like that from the terminal but I get the same error as mentioned by Smart Valley. When I run the rsync command over ssh and use sudo it still asks for a password to run the command so I dropped the sudo and it ran perfectly.

While I am at it, if I may impose, when you create this, it creates an on-off type switch in HA. I don’t need a ‘command_off’ switch, so what happens when the command completes successfully? Will the switch just go back to the off position?

Thanks in advance for any help you can give.

Not sure why the rsync command won’t run. One item to check is if your HA directory is at /home/homeassistant/.homeassistant or /home/hass/.homeassistant. It was changed at some point and new installs are the former I believe.

If you don’t need a switch you can just create a shell command

Put that in a script.

And then you can either call that script in a automation, or if you want a manual button add script.nameyouchose to your front end and there is a ‘Activate’ link you can click to run the script. If you don’t want the manual option you can just call the shell command from a automation.

Thank you @silvrr. I looked at the Scripts page but it did not seem to be what I needed. When I see scripts, I think shell commands or .sh files anyway and that page showed only template type scripts so I thought it it pertained to that and not running shell scripts or commands.

I wish I were more confident with HA, I would offer up some examples, changes, something to the documentation because it does not seem really geared to spinning up new users to such point they are comfortable with HA.

Anyway, yes, I do want the ‘Activate’ link so I can run it on demand. Thank you for your help, it is VERY much appreciated.

I kind of think of scripts as automatons without a trigger. You can either call them with the activate link or call them in an automation to make the run. They don’t really do anything themselves, they call on services or other components (shell commands) and put them in a nice package with a sequence.

Here is an example of using shell commands and scripts if it helps. It also shows what the activation option for a script looks like.

There isn’t a ton of detailed guidance on the HA site. I think for a lot of things the options are to great and the directions would be to complex and long for a lot of users. Even with the existing documentation a lot of people skip over a lot and ask well documented questions. Your best bet is to ask questions on this forum, its what it is here for, there are a ton of people who are willing to help.

Also, if you are getting into complex automation’s and if you know python, AppDaemon is a wonderful addon to Home Assistant. It gives you significantly more control. I’m in the process of converting almost all of my automation’s over to AppDaemon.

I saw that post but had trouble following where everything went and did not have a lot of time to sit down and sort it out. Though I am hoping that in the next few days I can sit back down, on a day off, and just work through it. It was nice and detailed, but I have split up my configuration and was trying to figure out where to put everything and make it link together, etc.

I will give it another stab soon. Thanks for taking the time to help out and for the detailed information in the other post.

@turboc I want to set up AppDaemon and it is on the list, but I also want to know this side of it as well. I suspect a great many new users might be thrown by the documentation (as I have been a few times) and I would like to know how to do most things outside of AppDaemon so that I may be able to help a few of them.

I keep fairly detailed notes and then find out I have been thwarted by the documentation and have to rework it all. It takes time but I want to learn. I don’t think most new users are going to jump into HA and immediately into AppDaemon, though, they probably should from what I have seen and read, but I don’t see it happening so my aim is to learn what I can from this perspective before going over to AppDaemon.

Thanks a ton @silvrr, that seemed to be the problem. I just had to drop “sudo” from my commands and now they work great under user hass in terminal.

A different problem appeared now in Home Assistant. When I turn command switch on, it doesn’t stay on. After a few seconds it automatically switches itself to ‘off’ position and it doesn’t trigger the off command. So if I check in terminal, my program, which was started when I turned my switch on is still running.

The switch would just have to stay in ‘on’ position until I turn it ‘off’ and it executes the off command.

What could be the problem?

There is nothing to tell the switch to stay on I believe. This is where the command_state and value template options come in.

Aha, I understand.

With command pgrep -f hyperionhue I can check if my program is running. In case it is running, this command’s output is process ID (e.g 15278), if it’s not running there is no output at all. Is there any way I could use that for command_state or value_template?

Understood. Some folks beat themselves over the head when they realize that a programatic interface like AppDaemon exists after they have spent hours fighting with YAML. Just wanted to make sure you were aware of the option before you got too frustrated. It was challenges like you are facing that made me look at AD and then it was like a breath of fresh air. :slight_smile:

I have looked at command_line switch documentation (maybe I missunderstood it?) and wrote a simple shell script that looks if my program is running or not. If it is running it returns 0, if it isn’t it returns value 1. I put it in my code, but unfortunately it doesn’t work. Command_on works, but switch still turns off by itself after a few seconds:

My code:

- platform: command_line
  switches:
    hyperionhue:
      command_on: '/home/hass/.homeassistant/hyperion/hyperionhuestart.sh'
      command_off: '/home/hass/.homeassistant/hyperion/hyperionhuestop.sh'
      command_state: '/home/hass/.homeassistant/hyperion/hyperionhuetest.sh'

My hyperionhuetest.sh:

if pgrep -f "hyperionhue" > /dev/null
then
    echo 0
else
    echo 1
fi

One thing that may be causing a problem, is your script is checking the entire command line for the string “hyperionhue”, while pgrep won’t find itself, it will find the shell script that is calling it which it may be doing in your case since your shell script has that string in it. I’m not sure if that’s the problem, but it probably isn’t helping.

Hello! I had a similar problem yesterday when setting up a TV. I wanted to get the command line to work, and was having difficulties. As it turned out, the issue was mainly with the fact that I was trying to execute a command on files that were in my pi home directory, and not in my homeassistant home directory. All I had to do was copy those files to a directory while signed in as homeassistant. (I have more detailed info here: https://github.com/alexanderldavis/tvcontroller

To sign into homeassistant: sudo su -s /bin/bash homeassistant

This was my configuration.yaml file:

switch:
 platform: command_line
 switches:
   television:
     command_on: "~/arduino-serial/arduino-serial -b 9600 -p /dev/ttyACM0 -s 1"
     command_off: "~/arduino-serial/arduino-serial -b 9600 -p /dev/ttyACM0 -s 1"
     value_template: '{{ return_value == "1" }}'
     friendly_name: TV

and ~/arduino-serial/ had to be a directory in my homeassistant folder for the whole shindig to work.

Hope this helps (if not OP at least someone else!).