Command line switch - Not working

Hi

I’m trying to get a command line switch working, it seems that I’m missing something critical, possibly permissions. I’m a windows guy so HA is my way or learning linux.

the command i want to execute, which works in the terminal (also works when running it as the user homassistant in the active virtual environment) onkyo system-power=on which turns my onkyo receiver on

This is my command switch

platform: command_line
switches:
onkyo_power:
command_on: “sh /home/homeassistant/.homeassistant/shell_commands/onkyo_on.sh”
command_off: “sh /home/homeassistant/.homeassistant/shell_commands/onkyo_off.sh”

the SH script looks like this

#!bin/sh
onkyo system-power=on

I’ve tried the following:

command_on: “bash /home/homeassistant/.homeassistant/shell_commands/onkyo_on.sh”
command_on: “onkyo system-power=on”
command_on: ‘onkyo system-power=on’

Im restarting HA between every change, and I keep getting the same error no matter what i enter after “command_on:”

17-03-21 21:55:33 INFO (Thread-4) [homeassistant.components.switch.command_line] Running command: bash /home/homeassistant/.homeassistant/shell_commands/onkyo_on.sh

17-03-21 21:55:33 ERROR (Thread-4) [homeassistant.components.switch.command_line] Command failed: bash /home/homeassistant/.homeassistant/shell_commands/onkyo_on.sh

Any idea what to start with to understand why it wont execute any of my commands?

What system do you use? If it’s a systemd service, how is that service set up?

The onkyo tools are from this github: https://github.com/miracle2k/onkyo-eiscp

installed outside of the virtual env with “easy_install onkyo-eiscp”
I couldn’t install it inside the virtual env because it required sudo to modify some folders, could that be the reason? If so, how can I install it withing the virtal env?

If it works when you’re inside your venv as hass user, it should work also in HA
Does this work in terminal?
sh /home/homeassistant/.homeassistant/shell_commands/onkyo_on.sh

Yepp, works fine from the venv

Adding this in case there’s something wrong

my configuration yaml has this line: switch: !include_dir_list switches/

and in the folder “switches” i have onkyo_power.yaml with this code

and the .sh script again

and the output in home-assistant.log as a picture

Hmm, maybe a PATH problem?
Could you type the full path to the onkyo executable in your script?
You should get it with which onkyo

pi@homeAssistant:~$ which onkyo
/usr/local/bin/onkyo

So type:
/usr/local/bin/onkyo system-power=on
in your script and click the switch again.
Don’t need to restart HA,

Done, but I’m getting the same error in the HA log, tried restarting HA as well without any luck =/
And I tried the .sh script from the terminal to make sure that it worked when i added /usr/local…

Shit!
Do you have any other command_line switch that works, or is this the first?
For testing please try something like this in your switch yaml:

command_on: "echo ON >> /home/homeassistant/test.txt"
command_off: "echo OFF >> /home/homeassistant/test.txt"

After clicking the switch, there should be a test.txt in /home/homeassistant/

Just a thought, is your .sh file executable and owned by HA?

You can try using the absolute path in your sh script.
What I originally was asking was which OS are you using? Is this on hassbian? Can you please post your systemd service file for home assistant?

VDrainer: The test commands worked fine! So I’m guessing the problem is getting the HA user to use the onkyo files, should I change any permissions on /usr/local/bin/onkyo to make sure that the user homeassistant can execute them? Or should I add the HA user to any specific group? I dont have a big issue with giving HA higher privileges, I’m running HA within my local network anyways (with VPN for accessing it from my phone)

Keithh666: They were owned by root, and executable, i changed it to homeassistant and tried it again without luck.

cgtobi: I’m runnig HA on Ubuntu 16.10 and this is my systemd service for it

Hmm, just tried it in my DEV environment instead, got it to work by skipping the use of .sh files and just letting the command line switch execute the command, sorry VDrainer if this was what you meant earlier, I just updated the onkyo_on.sh with the absolut path at that point… :confused:

But I’ll still need to solve how to execute .sh scripts from HA, I’ll continue to try different things.

Could you try it with
command_on: "/home/homeassistant/.homeassistant/shell_commands/onkyo_on.sh"
Without the “sh”.

It might not fix your problem but it’s worth a try. I advice you to change your systemd config to look like this:

[Unit]
Description=Home Assistant
After=network.target

[Service]
Type=simple
User=homeassistant
ExecStart=/srv/homeassistant/bin/hass -c "/home/homeassistant/.homeassistant"

[Install]
WantedBy=multi-user.target

Tried it but no luck, still can’t execute any .sh scripts =/

Hey @martikainen
I was blind. I think the problem ist the “shebang” in your scripts.
Please change
#!bin/sh
to
#!/bin/sh
or
#!/bin/bash
in your script and call it with
command_on: "/home/homeassistant/.homeassistant/shell_commands/onkyo_on.sh"