Using variables in shell_command is broken?

Running version 0.81.2.

I’m trying to pass a variable to a shell_command, here is a short script I use to test it:

test_shell:
    sequence:
      - service: shell_command.command_on
        data_template:
          duration: "30"

The shell_command looks like this:

  command_on:  'some path to script "{{ duration }}"'

I’ve tried w/o single/double quotes and whatever… nothing works!
Tried passing the variable as int, didn’t work either
If I remove the {{duration}}, the command executed successfully so it’s not the script fault or its path.

I’ve also tried running the same exact command from within the docker container and it works (obviously writing 30 instead of the {{duration}})

On logs I get the following:

2018-11-05 21:14:36 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `some path to script "{{ duration }}" > /dev/null`, return code: 2

I’m assuming you replaced the actual script pathname with some path to script, yes?

Try:

  command_on: some path to script {{ duration }}

And if that doesn’t work, try:

  command_on: some path to script "{{ duration }}"

Hi
Thanks for replying.
Yes, the path is replaced and I’ve already tried both your suggestions.

Well, unfortunately the error message the shell_command outputs shows the command before template rendering, so it’s not 100% clear what command it actually tried to execute. But the return code may be useful. It may depend on what command you’re actually trying to run, but return code 2 generally seems to mean “Misuse of shell builtins”, although other things I’ve read might imply a permissions issue.

Have you tried changing command_on to use 30 instead of the template and see if it works? It may work when you run the command manually (i.e., outside HA), but not from HA. The typical reasons are HA’s working directory might be different from what you expect (so it’s often necessary to use a full pathname to the command or script), or HA runs as a different user than when you run it manually (so you might have to make sure HA has permission to use the command and/or access the same directories/files.)

The script I’m trying to run isn’t even called, the rc 2 isn’t coming from it.

I’ve tried with 30 hard coded in the shell_command and it works.
This also rolls out the permission/wrong path option. (I’m also able to execute it from within the docker container using the same user/mounts)

Maybe you’re getting bit by this (from the shell_command doc page):

The commands can be dynamic, using templates to insert values for arguments. When using templates, shell_command runs in a more secure environment which doesn’t allow any shell helpers like automatically expanding the home dir ~ or using pipe symbols to run multiple commands.

I’ve found the issue.
I passed 30 and the value of an input_number.
Apparently, the value changed to 30.0 for some reason (even if it’s a string).
After I’ve enabled the debug logs for shell_commands it was pretty clear.
I used the round(0) method and it now works.

Thank you for your assistance !

Ah. I always have DEBUG enabled (for most things), so when you didn’t say you got additional output in the logs, I just assumed there wasn’t any. :slight_smile: Glad you got it working.

So, just for the record, was the return code of 2 from your command/script after all?

Yeap, my script indeed, just as it tried to parse the arguments