Passing arguments to shell_cmd

Hi,
I’m new to Home Assistant and ran into the following problem. I got lots of google results but no one helped.
I need to send a udp command to netcat via shell_command. That works fine:

shell_command:
  cmd_led_set: 'echo -e led 2 0\r | nc -u 192.168.0.109 10000'

Now I want to use some arguments inside this call. So I changed this to:

shell_command:
  cmd_led_set: 'echo -e led {{chan}} {{pwm}}\r | nc -u 192.168.0.109 10000'

This does not work. For testing I used the services tab und development tools with this code:

service: shell_command.cmd_led_set
data:
  chan: 2
  pwm: 0

I don’t get the point. What is wrong with this code?
The udp command parser is quite simple. It is important to have the cmd exactly as shown above (space, carriage return, etc.)
Can someone help?

Try this and see if it helps:

shell_command:
  cmd_led_set: 'echo -e led "{{chan}}" "{{pwm}}"\r | nc -u 192.168.0.109 10000'

You may need to quote your fields, I know I do on some of my shell commands.

unfortunally CO_4X4 idea does not work.

I tried some other versions, with no success:

  cmd_led_set: >-
    {{'echo -e led : %d %d\r | nc -u 192.168.0.109 10000' % (chan, pwm) }}

  cmd_led_set1: "{{'echo -e led : %d %d\r | nc -u 192.168.0.109 10000' % (2, 100) }}"

  cmd_led_set2: >
    'echo -e led "{{chan}}" "{{pwm}}"\r | nc -u 192.168.0.109 10000'

  cmd_led_set2: >-
    {{'echo -e led : %d %d\r | nc -u 192.168.0.109 10000' % (chan, pwm) }}

  cmd_led_set3: >-
    echo -e led 2 100\r | nc -u 192.168.0.109 10000

  cmd_led_set4: >-
    echo -e led 2 100\\r | nc -u 192.168.0.109 10000

  cmd_led_set5: >-
    echo -e "led 2 100\r" | nc -u 192.168.0.109 10000

  cmd_led_set6: >-
    echo -e "led 2 100\\r" | nc -u 192.168.0.109 10000

  cmd_led_set7: >-
    echo -e led "{{chan}}" "{{pwm}}"\r | nc -u 192.168.0.109 10000

Even I don’t use argument variables none of the “>” multiline statements work.

I tried the code in the template section.

{% set chan = 2 %}
{% set pwm = 0 %}
{{'echo -e led : %d %d\r | nc -u 192.168.0.109 10000' % (chan, pwm) }}

But it provides exactly what I thought of:

echo -e led : 2 0 | nc -u 192.168.0.109 10000

Obviously the interpreation of the configuration.yaml deferes from the Jinja2 Template. What is the difference?

See if you can put your shell command into a script with parameters and then call the script instead. You can start by SSH’ing to your HA system and writing a script file that works properly (I use config/scripts for that), once it works in script then your command is just calling the script with args that does what you want.

Start with seeing if the script will everything hard coded works from SSH by itself:

led.sh:

echo -e led 2 0\r | nc -u 192.168.0.109 10000

then:

chmod 0755 led.sh

finally:

./led.sh

If that works as expected, try it with variables:

echo -e led $1 $2\r | nc -u 192.168.0.109 10000

And call with:

./led.sh 2 0

And if all that does as planned then you change your shell command to something like this:

shell_command:
  cmd_led_set: /config/led.sh {{chan}} {{pwm}}

or

shell_command:
  cmd_led_set: /config/led.sh "{{chan}}" "{{pwm}}"

Thanks to CO_4X4 detailed explanatin it is working now within these 3 variants:

shell_command:
  cmd_led_set: >-
    bash /config/led.sh {{chan}} {{pwm}}
  cmd_led_set0: bash /config/led.sh {{chan}} {{pwm}}
  cmd_led_set1: bash /config/led.sh "{{chan}}" "{{pwm}}"

I needed to add the bash statement. Now it works.

It seems to me that shell_command: can not accept variables as it is depicted in the HA documentation.
I found several threats about this issue now:

This goes back to Sep 2016. Is there any bugreport on this topic?

I’ve also spend hours on this, to no avail. Tried everything from escaping double quotes to building the entire shell command line in an input string and sending it to the shell command; quite annoying that this is not working; now I have to use eight different shell commands (fully written out) to get very inflexible functionality; for each new device I have to add many more.

Pretty useless, this quick-and-dirty integration, if one has no freedom to specify a command at the higher level.

I think the alternative is to write my own python code to do this simple trick properly.

What exactly are you trying that doesn’t work? I use shell command quite a bit, from executing a local script to SSH’ing to a different computer to issue commands there - all with command line options. To say it’s useless is a blanket statement that doesn’t seem accurate to me, perhaps the problem is more in how you are trying to implement rather than how HA does its job.