Trigger Linux Command Using Home Assistant Script

Hi, I have a linux command I would like to trigger using a Home Assistant Script so I can automate it. The command is “vlc --sout=”#chromecast{ip=10.0.1.26}" --demux-filter=demux_chromecast rtmp://10.0.1.2/live/stream"

Which is a command that casts video and audio of an RTMP stream to your chosen google cast device.

I tried to use the Shell service, but not sure how. Does anyone know how to trigger this linux command using a script in HA?

Any help is greatly appreciated.
Thanks.

I would suggest to start with creating an script.sh file and see if that works.
Then I can help integrating this into HA.

Hi, i did create a file in /usr/share/hassio/homeassistant/scripts/test.sh

and in my configuration.yaml:

#shell command tester
shell_command:
  test: /bin/bash -c /usr/share/hassio/homeassistant/scripts/test.sh

Also tried:

shell_command:
  test: /usr/share/hassio/homeassistant/scripts/test.sh

but nothing works, if I run the command /usr/share/hassio/homeassistant/scripts/test.sh by itself in ubuntu server which HA is installed on, it works perfectly and casts to chromecast but its not working through HA.

Once I get the above working I created a script to call it using:

#test shell script
shelltest:
  sequence:
  - service: shell_command.test

I just need the call to the bash script to work through HA.
Thanks.

The home assistant log shows an error:

Error running command: /usr/share/hassio/homeassistant/scripts/test.sh, return code: 127

This error seems to be a PATH issue according to google, but not sure how to fix it, I need to somehow add that test.sh file to the bash path, any ideas?

Anyone have an idea how to solve this error code 127 for the shell command?

Are you running HA in docker? If so the docker image does not have vlc installed so you won’t be able to execute that command. You would need to SSH into a machine on your network that does have vlc installed, most likely the ubuntu host machine.

I use the command below to run scripts from HA on remote servers. Maybe it will help.

ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /config/.ssh/id_rsa [email protected] sh /home/km/testing.sh
1 Like

Hi, ya running HA in docker using this guide “https://www.juanmtech.com/set-up-hassio-in-docker-and-in-an-ubuntu-server/

My HA server is also my Nginx server and I have VLC installed on it. How do I make VLC accessible through docker since VLC, HA are on the same machine?

I mean the short answer is that you can’t. The install process of HA sets it up specifically to insulate it from the host machine for security reasons. Your shell command is running as root in the HA container and you can only see the what that user can see in that container.

ssh is installed though which is why if you look around the forum here, there are many similar posts about shell_command issues directing users to use ssh in order to run the command on the host. If you google ‘home assistant shell command error 127’ you can see there’s a lot of users who have run into this issue and been given a similar solution. Here for instance.

Btw, I find the portainer addon to be particularly useful when trying to get a shell_command working. With that addon you can open up the homeassistant container that HA runs in and open up its console as the root user. This lets you test out scripts by running them in exactly the same way HA will do from a shell_command so you can test it and get it working without frequent restarts.

1 Like

Thanks a for your reply, I will try that addon and see if it works. All this was to achieve a successful casting of an RTMP stream with audio to any chromecast device. I tried every possible way through HA directly sending the RTMP stream to chromecast but doesnt work. The only time it worked was when I added the stream as a camera and then casted that, but the cast from a camera doesnt carry sound.

Thats why I had to go the VLC route by casting through VLC and making HA control triggering the casting command.

This works by the way.
So long as you make the bash script in the docker container that homeassistant is running on and set the correct privileges. Thanks for your suggestion!