Struggling to send entity value over to shell command

Hi all,

Hoping someone can help me out here as I’m pulling my hair out!

I’m trying to send the Spotify picture_entity URL over to a script so I can convert it from JPG to PNG using magick. If i run the script manually through terminal everything works as it should, but when it’s triggered via an automation, I get the 127 error code.

My config.yaml looks like this:

shell_command:
  convert_artwork: /bin/bash /homeassistant/convert_artwork.sh "{{ artwork_url }}"

My automation yaml looks like this:

action: shell_command.convert_artwork
data:
  artwork_url: "http://192.168.0.100:8123{{ state_attr('media_player.spotify_terrorfall', 'entity_picture')}}"

I’ve tried with/without quotes (both single and double), I’ve tried passing over the raw text not as a variable as well, but nothing is getting picked up by the script.

The sh script for completeness is:

#!/bin/sh
artwork_url=$1

magick $artwork_url -resize 250x250 /config/www/convert.png

This works when I call it from terminal with the following command: (Actual token redacted)

./homeassistant/convert_artwork.sh "http://192.168.0.100:8123/api/media_player_proxy/media_player.spotify_terrorfall?token=redacted&cache=f5870161b96cc521"

But via automation I am getting:

2025-01-16 17:43:38.502 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: /bin/bash /homeassistant/convert_artwork.sh "{{ artwork_url }}", return code: 127

You have to quote $artwork_url in your shell script.

Also recommended, log the actual bash commands with -x.

Thanks @Rudd-O have made that update, but still no joy, getting closer though!

Looks like there is some weird location or permission issue though, running the command from the ‘Execute Shell Command’ option in file editor I was getting messages about magick not existing, so updated the script to check for it and install if not there (think this may be down to a reboot or update, so thought it best to add it in permanently to be on the safe side)

Everything seems to be working ok from an image generation perspective, but the script now can’t output the file it creates:

Script executed from: /homeassistant
Script location: /homeassistant
artwork_url=‘http://192.168.0.100:8123/api/media_player_proxy/media_player.spotify_terrorfall?token=&cache=e4dc6d94ae8d8cc9
echo ‘Script executed from: /homeassistant’
dirname /homeassistant/convert_artwork.sh
BASEDIR=/homeassistant
echo ‘Script location: /homeassistant’
test -f /usr/bin/convert
magick ‘http://192.168.0.100:8123/api/media_player_proxy/media_player.spotify_terrorfall?token=&cache=e4dc6d94ae8d8cc9’ -resize 250x250 convert.png
Command executed: /bin/bash -x /config/www/convert_artwork.sh “http://192.168.0.100:8123/api/media_player_proxy/media_player.spotify_terrorfall?token=&cache=e4dc6d94ae8d8cc9”: 127
/bin/bash: /config/www/convert_artwork.sh: No such file or directory

As you can see from the log I moved the script to the same location as where it needs to drop the file (/config/www/, will move this out for security once done) but I’m getting the message that it can’t find the folder it’s executing from

Yes, that folder does not exist in your system. /config is not a directory within the root file system to my knowledge. How about you try running this:

bash -c 'pwd ; ls -la'

in the command, to orient yourself as to which folder the script convert_artwork.sh is located? Perhaps all it is necessary is to use a relative path…

Thanks @Rudd-O that got me to the right place and this is all working now. Thanks for your help.

Scripts all run from /homeassistant folder so the config yaml needed to be updated to remove that folder

shell_command:
  convert_artwork: /bin/bash -x ./convert_artwork.sh "{{ artwork_url }}"

Then the script needed the same treatment and place the converted file in the relative directory

magick "$artwork_url" -resize 250x250 www/convert.png
1 Like

It makes me enormously happy that my pointers have helped you get to a solution. Have a great day.