Shell command volume slider

I have a volume slider on the GUI that I would like to send a variable shell command to a component.

In automation.yaml in have:

  alias: vol slider
  description: ''
  trigger:
  - platform: state
    entity_id: input_number.218_vol
  condition: []
  action:
  - service: shell_command.218_vol_var
  mode: single`

configuration.yaml I have:

input_number:
  218_vol:
    name: Meridian 218
    initial: 40
    min: 1
    max: 99
    step: 1

and

shell_command: 
  218_vol_var: echo -e '#SVN {{ states('input_number.218_vol') }}' | nc 192.168.1.8 9014

If I replace the argument in the shell with a two-digit number then the device at 192.168.1.8 responds with that command. for example:

shell_command: 
  218_vol_50: echo -e '#SVN 50' | nc 192.168.1.8 9014

This shell would set the volume to ‘50’. Can anyone point out my mistakes as the variable is not working!

Because you have only used single quotes your quotes open and close here:

shell_command: 
  218_vol_var: echo -e '#SVN {{ states('
                       ^----- Pair ----^

Either escape your single quotes (with another single quote) or use double quotes.

shell_command: 
  218_vol_var: echo -e '#SVN {{ states(''input_number.218_vol'') }}' | nc 192.168.1.8 9014
shell_command: 
  218_vol_var: echo -e '#SVN {{ states("input_number.218_vol") }}' | nc 192.168.1.8 9014
1 Like

Unfortunately I have just tried this without success. However I will update my code.

To test this I go to the developer tools and insert a two digit value on the ‘input_number.218_vol’ entity to overcome any potential formatting issues. In addition to this, I am using the GUI slider.
Both scenarios I cant get the variable shell to work. as previously said if I change the variable formatting to a static number in the shell. Then adjust the GUI slider, I get the specified value on the component which is totally expected. This proves to me that my theory is correct but my variable formatting is incorrect.

Put this in the template editor and check what it resolves to:

echo -e '#SVN {{ states("input_number.218_vol") }}' | nc 192.168.1.8 9014

Also, try this command:

shell_command: 
  218_vol_var: 'echo -e ''#SVN {{ states("input_number.218_vol") }}'' | nc 192.168.1.8 9014'
1 Like

I have not used the template editor tool before - what an incredibly useful tool!

I placed this command into the editor:

echo -e '#SVN {{ states("input_number.218_vol") }}' | nc 192.168.1.8 9014

and received this output:

echo -e '#SVN 26.0' | nc 192.168.1.8 9014

It seems we’re very close to having something that works! ‘00.0’ value is not recognised by the component. I need a ‘00’ value (without the decimal point).

For the leading zeros have a play with this formatting option:

echo -e '#SVN {{ "%0.2d"|format(states("input_number.218_vol")) }}' | nc 192.168.1.8 9014

Search for “Python leading zeros” for help. I’d give you the exact format but I can’t remember how it works and am struggling with a very bad internet connection.

Thank you!

218_vol_var: echo -e '#SVN {{ (states("input_number.218_vol") | round(0)) }}' | nc 192.168.1.8 9014

provides the following in the Template editor:

echo -e '#SVN 46' | nc 192.168.1.8 9014

This seems to be exactly what I need! But it is still not controlling the component… As this is a command_line string, does it need to be fired upon every state change of the volume entity in the automation?

Yes, but that is what your existing trigger will do.

for anyone looking in I found the below code to insert a zero in front of a single digit when formating numbers

shell_command:
  218_vol_xx: echo -e '#SVN {{"%02d"|format(states("input_number.218_vol")|int)}}' | nc 192.168.1.8 9014`

still no luck with getting my slider automation to work yet…

I am going round in circles and I’m convinced that the template in command_line is not working.

for a test i change my automation to:

 alias: vol slider
 description: ''
 trigger:
 - platform: state
   entity_id: input_number.218_vol
 condition: []
 action:
 - service: shell_command.218_vol_up
 mode: single`

the difference being the change in service to:

shell_command.218_vol_up

This shell is a fixed command without templating, when I adjust the volume slider up or down, the component’s volume goes up by one. This is expected and demonstrates the automation is working.

As above I have run my template through the editor and I get the result I’m expecting. Is it possible that command_line is unaware of certain types of templates? any workarounds would be greatly appreciated to get these 2 digits into the shell.

What device are you trying to control?

Does it have a log you can interrogate to see what is going on?

Does it have a restful interface?

I use rest commands to control functions of my Yamaha media player that are not implemented in the integration. e.g.

rest_command:
  cinema_dialogue_lift:
    url: "http://10.1.1.17/YamahaExtendedControl/v1/main/setDialogueLift?value={{ ( states('input_number.cinema_dialogue_lift')|float * 2 )|int }}"

Another great suggestion from you! thank you for all your time on this.

The device is an audio pre-amplifier. I setup a telnet connection to see what it was receiving and found the non-formatted shell commands from Home Automation are making their way to the device but the formatted shell commands are not. again, this points towards the dynamic formatting…

I have now added the shell_command logger to my config.yaml

logger:
  default: info
  logs:
    homeassistant.components.shell_command: debug

For ease of testing, I made a script I can trigger on demand. the first line from the logger at 14:36:32 uses the templating shell_command which does not control my IP device as expected. I notice that it seems to self report the actual input number within Home Assistant.

b'#SVN 25 | nc 192.168.1.8 9014\n'

At 14:37:03 uses this shell_command control the device as expected by ‘manually’ adding the value ‘50’. As you can see the device replies with and behaves as expected

b'!PID Product:"218" SerialNumber:"XXXXXX" VersionNumber:"1.1.240" ZoneName:"Lounge" ProtocolVersion:"3"\n*ACK\n'

Here’s the log:

2021-04-02 14:36:32 DEBUG (MainThread) [homeassistant.components.shell_command] Stdout of command: `echo -e '#SVN {{ states("input_number.meridian_vol") | round(0) }}' | nc 192.168.1.8 9014`, return code: 0:
b'#SVN 25 | nc 192.168.1.8 9014\n'

2021-04-02 14:37:03 DEBUG (MainThread) [homeassistant.components.shell_command] Stdout of command: `echo -e '#SVN 50' | nc 192.168.1.8 9014`, return code: 0:
b'!PID Product:"218" SerialNumber:"XXXXXX" VersionNumber:"1.1.240" ZoneName:"Lounge" ProtocolVersion:"3"\n*ACK\n'

Can anyone help me with this?