How to execute .sh file from HA

I am a new user of home assistant, and I’m loving it so far. I have also recently discovered how to use irsend and lirc to transmit IR to devices. I have one r.Pi3 running HA, and another r.Pi3 on the network setup for lirc. I have written a .sh script on the HA Pi that connects to the remote Pi and executes the infrared trasmit command. This works perfectly running it from the command line directly “/home/pi/sewing_ac_on.sh”. I tried placing a shell command and a script to call it in HA, but that doesn’t work properly. Here’s the piece of my configuration.yaml file.

shell_command:
ac_on_s: irsend SEND_ONCE DELONGHI AC_64
ac_off_s: irsend SEND_ONCE DELONGHI AC_OFF
ac_sewing_on_s: /home/pi/sewing_ac_on.sh

script:
ac_on:
sequence:
- service: shell_command.ac_on_s
ac_off:
sequence:
- service: shell_command.ac_off_s
sewing_ac_power:
sequence:
- service: shell_command.ac_sewing_on_s

The local irsend commands work correctly, but the running of the .sh file doesn’t seem to work at all.

My .sh file contents (that works fine when run by itself):

#! /bin/bash

ssh [email protected] ‘irsend SEND_ONCE haier KEY_POWER’

/bin/bash

Any ideas to get the script to execute properly via HA?

The file should only contain the command with complete paths and doesn’t need .sh

I think he does have the complete path.

Here are my suggestions

  1. Use a period before the command like so

./home/pi/sewing_ac_on.sh

  1. Make sure you have /home/pi in your PATH variable and just use without the full path like so

sewing_ac_on.sh

Thanks for your help! I tried adding a period before the command - didn’t change anything. I also added /home/pi in the PATH variable, which also didn’t change anything. I also tried copying the script to /home/hass (as I used the rPi AiO installer), and adding that to the PATH variable for the hass user, but that didn’t do it either. I also tried making a file without .sh and with just the command but that didn’t work. Any other thoughts as to what I could do to get this working?

Did you try executing the script from the command line or did you only try executing the script content?

I can run ./sewing_ac_on.sh from the command line and it works correctly - ssh into the remote pi and execute the irsend command.

What about:

shell_command:
  ac_on_s: irsend SEND_ONCE DELONGHI AC_64
  ac_off_s: irsend SEND_ONCE DELONGHI AC_OFF
  ac_sewing_on_s: sh /home/pi/sewing_ac_on.sh

Have you tested calling the service from the dev tools? Do you get anything in the logs, when you do?

Try using full paths…

ac_on_s: /usr/bin/irsend SEND_ONCE DELONGHI AC_64
ac_off_s: /usr/bin/irsend SEND_ONCE DELONGHI AC_OFF

Your irsend may be located elsewhere. Try “which irsend” to find the correct path.

@martinhjelmare I will try that when I get home - thanks!

@VladTepz - the local irsend commands are not what I’m having issue with - they work perfectly fine. Only the execution of the .sh file to issue commands remotely is what is not running correctly via HA.

Ah, my bad.

The only other thing I can think of is to check to make sure its executable “sudo chmod +x /home/pi/sewing_ac_on.sh” and the user Hass is running as has permission to execute.

@martinhjelmare I tried adding sh to the beginning of the shell command like you suggested - no luck. I also tried calling the service from the dev tools, which produced no results and no log entries.

@VladTepz I did make the file executable as well as I checked the permissions and it is set so anyone can read and execute the file.

You should always see something in the logs after calling a service. Otherwise you’re logging isn’t set up or the log level is not verbose enough.

Do you have an entry in your config for the logger component? You can change the log level with that component. During troubleshooting I like to set default to INFO.

I know it has been a while but did this end up getting solved.

I have the same problem - .sh files are executable and functioning from putty and appear to be executing from home assistant but are not actually functioning.

I have the same issue, did you fix this?

Hi,

I installed HA with All in one installer on a raspberry pi 3. I also had no luck with executing any shell command.

After hour of finding a solution, I changed the home-assistant.service from user hass to user pi and give the .homeassistant folder 777 rights.
Now I can execute any script or command also with sudo etc…

I dont know if this is the perfect solution, but it works for me.

Thanks for the tip, that gave me the idea to add the hass user to all of the groups that the pi user is in - but it doesn’t look like its working either.

Its strange that the error doesn’t show up in the log file, that would sure make it a lot easier to diagnose.

The script is in the hass user home folder with 777 rights so hass shouldn’t have any issue running it. I’ve tried it with and without sudo without any luck.

Same like me. I cant find anything that helps and there was nothing in the logs.
The only way was to run as pi.

Sort of. I can now successfully fire some scripts, but not all. The main problem was my !includes setup with directories.

Some scripts that I can definitely execute with hass user from putty still wont work if started by HASS though. I’m still trying to work it out.

I also need some help with executing a .sh file. This .sh file should start when my doorbell rings. I have tried the shell_command but it still doesn’t execute.