Calling a shell script with parameters from picture-elements-card not working

So the script is straight forward (same as my other working ones), only this one I’m trying to make use of a dynamic variable. in this case hostname is what I want to expect:

# define our variable 'hostname'
shell_command:
  kodi_clean: '/opt/homeassistant/kodi_remote.sh "{{ hostname }}" --clean'

If I run the script manually it works great:

/opt/homeassistant/kodi_remote.sh 1.2.3.4 --clean

To debug, in the very first line of the script I added the following:

# What I was trying my hardest to troubleshoot with
# before falling to ask this community:
echo "Called $0 with $@" >> /opt/homeassistant/kodi_remote.sh.log

In the kodi_remote.sh.log logs, I always get this:

Called /opt/homeassistant/kodi_remote.sh with  --clean

Here is how I’m trying to call it:

  1. This appears to be how the tap-action is documented; to use service_data, but it doesn’t work:

     tap_action:
       action: call-service
       service: shell_command.kodi_clean
       service_data:
         hostname: "1.2.3.4"
    
  2. Automations appear to use template_data, but this doesn’t work in this case:

     tap_action:
       action: call-service
       service: shell_command.kodi_clean
       template_data:
         hostname: "1.2.3.4"
    
  3. Automations also appear to support data, but this also doesn’t appear to work in this case either:

     tap_action:
       action: call-service
       service: shell_command.kodi_clean
       data:
         hostname: "1.2.3.4"
    

It’s possible the last 2 options will work if I actually define an automation; i haven’t tried that. This problem specifically focuses on trying to execute a shell_command from lovelace; specifically a picture-elements tap_action.

What am I don’t wrong?

Hey Chris,

when using Lovelace, service_data is definitely the correct syntax.

If I run the script manually it works great:

I guess you’re using the terminal plugin? That’s a good first step, however please note that this environment is different from Home Assistant Core running the shell_command, so things might work in one and not the other.
The most apparent issue I see is the path of the script - please note that the config folder (where configuration.yaml and all the rest of it sits has the path /config/ for the Core. Again - that’s one of the differences between the terminal plugin and the core.
So give this a shot:

# define our variable 'hostname'
shell_command:
  kodi_clean: "/config/kodi_remote.sh {{ hostname }} --clean"

A few other things that can lead to issues:

  • Windows line breaks. If you’ve uploaded the shell script from a windows environment, please copy the contents and paste it into the file editor, save it from there.
  • Path issues: Check that there are no absolute paths to executables in your script, even #!/bin/bash could throw off Core.

Finally, testing. To test your script, running it in the terminal is a good first step as said, however I’d use the Developer Tools - Services UI for that, so that it gets executed by Core (don’t forget to pass your hostname variable there). If that works fine, you can continue and test it from Lovelace.
If the script still fails, you can debug it by putting this into your config:

logger:
  logs:
    homeassistant.components.shell_command: debug

and change your shell command to:

# define our variable 'hostname'
shell_command:
  kodi_clean: "sh -x /config/kodi_remote.sh {{ hostname }} --clean"

Good luck!

You’re awesome! Thank you!

This:

# define our variable 'hostname'
shell_command:
  kodi_clean: 'sh -x /config/kodi_remote.sh "{{ hostname }}" --clean'

to this fixed it:

# define our variable 'hostname'
shell_command:
  kodi_clean: "sh -x /config/kodi_remote.sh {{ hostname }} --clean"

It’s all about the quotes at the end of the day it would seem :slight_smile:

Thank you so much!

1 Like

Nice to hear :slight_smile:
You can remove the -x after sh, this prints out every command you run in addition to the regular output, which I guess you don’t need. Just to keep that in mind when using command_line sensors for example :slight_smile:

1 Like