Volume Slider Using input_number and Shell Command Template

Hello! I have a stereo amp that uses IP controls to adjust settings. To set the volume to 50, I have successfully sent the following using command_shell:

"echo -ne \"-v.50\r\n\" | nc [IP] [PORT]"

I would like to use input_number to input a variable via slider into the command to adjust the volume. Given that the docs says command_shell can’t be used with a template and a pipe operator, is there any way to achieve this?

As reference, the command I initially tried (before I read the part in the docs about the pipe operator) was the following:

`echo -ne \"-v.{{states('input_number.hegel_volume') | int}}\r\n\" | nc [IP] [PORT]`

Which returned:

return code: 0: b'"-v.12rn" | nc 192.168.1.112 50001'

Thank you!

Use bash -c, e.g.

the_command: >
  bash -c 'echo -ne "-v.{{states('input_number.hegel_volume') | int}}\r\n" | nc [IP] [PORT]'
1 Like

Awesome. Thank you, @koying ! I figured there was an easy solution, but my command line knowledge is pretty limited.

This just launches a shell “of your own” (bash) rather than HA doing it. “-c” tells it which command to execute.

1 Like

I am experiencing a similar problem… I want to use the shell_command to write a value to a file.

This works fine:

shell_command:
  rpi_screen_set_50: 'echo 50 > /config/rpi_brightness/brightness'

But the following fails:

shell_command:
  rpi_screen_set_template: 'echo {{ states("input_number.screen_brightness") }} > /config/rpi_brightness/brightness'

Do you have a clue @koying ?

Yeah: Exactly what I said in the solution…

I tried to replicate your solution with my command but I didn’t get it right. Still not working…
Would you be so kind to show me what the command should look like?

rpi_screen_set_template: >
  bash -c 'echo {{ states("input_number.screen_brightness") }} > /config/rpi_brightness/brightness'

Too bad… Not working for me… That is what I tried, tried again, but no success…

Works for me. Any error?

I tried it on another HA installation and it works on that one!
Must be something with the config of my other HA…

I got it working, your solution was the key! I only needed to round to a full integer for my use…

rpi_screen_set_template: >
  bash -c 'echo {{ states("input_number.screen_brightness" | round(0) }} > /config/rpi_brightness/brightness'