Help on passing entity state to a command shell line

Apologies if I am asking a basic question. I am learning from scratch.

I am trying to pass the value of an entity (set up as a slider) to a command shell line in order to change the volume on my Trinnov surround processor - which can be controlled by tcp/ip commands but which doesn’t have an available integration.

I have this line in my config file:

shell_command:
  set_volume_to_slider: echo -e 'id Trinnov; volume {{ states.input_number.trinnov_volume.state | int}} ;' | nc 192.168.1.110 44100

where input_number.trinnov_volume is the entity, defined as the following:

{
    "version": 1,
    "key": "input_number",
    "data": {
        "items": [
            {
                "min": -60.0,
                "max": 20.0,
                "name": "Trinnov Volume",
                "unit_of_measurement": "dB",
                "mode": "slider",
                "step": 1.0,
                "id": "trinnov_volume"
            }
        ]
    }
}

I then have an automation that runs when that entity’s state changes

alias: Change Trinnov Volume
description: ''
trigger:
  - platform: state
    entity_id: input_number.trinnov_volume
condition: []
action:
  - service: shell_command.set_volume_to_slider
mode: single

When I do this, although the automation runs, the processor doesn’t change.

Everything works if I change my line in my config file to a static test, e.g.

shell_command:
  set_volume_to_slider: echo -e 'id Trinnov; volume -35 ;' | nc 192.168.1.110 44100

But if I want to pass the value from my slider entity trinnov_volume to dynamically replace that -35, it falls over …

If anybody could kindly give me some help, I would really appreciate it.

You need to pass the state as a parameter in the automation to the script. I say “parameter” since the docs are a bit confusing here. In the automation you use the keyword variables but in a scripts it’s called fields.

EDIT: My apologies. You’re using shell_command not script. You can do it. Will post another update in a moment.

EDIT: I do it like this. The host in the command is assumed to be a parameter.

shell_command:
  shudown_remote_host: "/home/homeassistant/scripts/shutdown_remote_host.sh {{ host }}"

And gets passed like this.

    - service: shell_command.shudown_remote_host
      data:
        host: "example.com"

In your case (untested):

shell_command:
  set_volume_to_slider: echo -e 'id Trinnov; volume {{ volume }} ;' | nc 192.168.1.110 44100

    data:
      volume: "{{ input_number.trinnov_volume }}"

Thank you so much for all your help.

I realised I had got things back to front - I needed to pass back, like you said.

In addition to that, I was needed to pass back a state, not a variable. And, finally, a shell command can’t contain any pipe modifiers after it, if it contains anything from a passed template. Which meant that I then needed to learn how to write a bash shell file, and call that file from shell command, and then do all my echoing with the pipe modifier within that file.

I have learnt more in two days than I thought possible!!