Error with a simple command_line switch

Hello. What’s wrong with this command line switch?

- platform: command_line
  switches:
    sirena:
      command_on: "/usr/bin/touch /home/pi/foobar"
      command_off: "/usr/bin/touch /home/pi/fred"     

When I execute it by pressing the button, the log shows this error:

2019-04-07 21:16:37 ERROR (SyncWorker_12) [homeassistant.components.command_line.switch] Command failed: /usr/bin/touch /home/pi/foobar

Does it work in a terminal, as homeassistant user with venv activated (depending on your installation method)?

I installed hassio and don’t have any “homeassistant” user. Nor I installed venv.
How do I have to proceed ?

If you installed hass.io, i assume (i’m not using it) there is no user pi, and so there is no directory /home/pi.
The question is, what should your switch do?

Sorry, I forgot to tell you that I installed hassio with docker on raspbian.
Anyway, I got some progress:

  1. with command_on: “echo foobar” it works
  2. with command_on: “echo foo > foobar” it works
  3. with command_on: “echo foo > /home/pi/foobar” it doesn’t work
  4. with command_on: “/usr/bin/touch /home/pi/foobar” it doesn’t work

So:

  1. there’s a path problem (“echo” is inside /bin, while “touch” is inside /usr/bin
  2. there seems to be a permission problem (because 2) works while 3) doesn’t)

This is very buggy… is there a way to fix that?

Thanks

It’s not buggy… it’s just those commands are executed in the Home Assistant container and those directories /usr/bin/touch and home/pi are not mapped or available in the container.

Having spent a week screwing around with a shell script, the easiest way I found to debug what is and isn’t available and what you can and cannot use is to open a console in the Home Assistant container (use the Portainer addon) and execute your commands in there. If they work there, they will work in Home Assistant shell_command/command_line switch.

1 Like

you are right,

Is there a way to expose them to the docker container?
Otherwise a workaround would be to execute a command through ssh (docker has ssh in its stuff), but I would like to avoid that

Not if you run hass.io The shell is very limited in what you can do. As an example, the date command date -%z (or is it +) should return the Time Zone and it does in the normal bash shell but it doesn’t work in the home assistant container… That and about 100 other things I was trying to do. I found convoluted workarounds to all my issues though.

The O/S can see all the HA config folder though so why don’t you attack it by creating files in /config or a sub directory and then just use cron or something else in the host to copy or move the files to where you really want them? There’s always a way… just need to think outside the box and work within the constraints imposed by hass.io.

maybe I could create links inside the hassio dir…

Inside /config along with the configuration.yaml - yes for sure. I used a sub directory of config. The config folder is persistent so a good choice.

unfortunately, there’s no way to call host’s executables from a docker container.
A solution seems to make a python script with a MQTT client that catches msgs on a reserved topic outside docker and then execs the commands on the host’s env.
Then no “command_line” input is necessary.
I can share the code, if anyone is interested

No matter what, you’re constrained by what you can run from a shell in the Home Assistant container. In my case I was using curl and date and jq… stuff like that… I wanted to publish json to my MQTT broker… but yeah… no mosquitto_pub in the container for that so I just ended up using a long term token and curl and it published the json to the sensor I wanted… Always multiple ways to get where you want to go.

make sure you whitelist directories you plan to touch or create files … in configuration.yaml
whitelist_external_dirs:
- ‘/tmp’

1 Like

that’s actually only necessary if you use a file sensor…

not entirely correct.

https://hastebin.com/sojasolite.sql

I don’t use hassio but I run HA in docker and that procedure worked well for me.