Shell command with parameters not working

Hi,

I have this input number in configuration:

input_number:
  denon_level_center:
    name: Level Center
    min: -12
    max: 12
    step: 0.5
    unit_of_measurement: 'dB'

Automations have this that gets the number and sends the correct value:


- id: '1625292799637'
  alias: Denon Level Center
  description: ''
  trigger:
  - platform: state
    entity_id: input_number.denon_level_center
  condition: []
  action:
  - service: shell_command.denon_level_center
    data:
      value: "{{ ((states('input_number.denon_level_center')|float*10)+500)|int }}"
  mode: single

This should call this Shell Command, it works fine if I enter the value manually, but doesn’t get the value from automations, service, data, value:

denon_level_center: echo -e PSCLV {{ value }} | nc 192.168.1.218 23

What’s wrong with automations or shell command? Why not passing value?

You can’t pass variables to shell_command, only to scripts.

Why not just

denon_level_center: "echo -e PSCLV {{ ((states('input_number.denon_level_center')|float*10)+500)|int }} | nc 192.168.1.218 23"
1 Like

You code seems perfect! But no idea why this isn’t working, I can send the code manually, but jinja2 template not working in shell commands, it works in automations, but not in shell commands, any idea why?

I don’t really understand what you mean by that…

That code works in automations, I tried sending a push to my mobile and it works perfectly.

But in shell_command it doesn’t work correctly, if I change the jinja2 template to just a number 500 it works fine.

I think I found the trouble, the PSCLV 500 doesn’t work, but “PSCLV 500” works, how do I add double quotes “ to jinja template? Seems like I only need to add that.

Already tried this and doesn’t work:

denon_level_center_set: echo -e "PSCLV {{ ((states('input_number.denon_level_center')|float*10)+500)|int }}" | nc 192.168.1.8 23

But this does work:

denon_level_center_set: echo -e "PSCLV 500" | nc 192.168.1.8 23

Can’t get dynamic values working in shell commands, have been trying for hours.

Try

denon_level_center: 'echo -e "PSCLV {{ ((states(''input_number.denon_level_center'')|float*10)+500)|int }}"  | nc 192.168.1.218 23'

Sorry, I tested again and this works: (without the quotes)

denon_level_center_set: echo PSCLV 500 | nc 192.168.1.8 23

The line you sent, should work like, but it’s not working:

denon_level_center_set: "echo PSCLV {{ ((states('input_number.denon_level_center')|float*10)+500)|int }} | nc 192.168.1.8 23"

Any other idea of what I can try?

I’ve edited the shell command
EDIT: Edited again to remove the (apparently) unneeded double-quotes

If you put the command on a new line you can at least avoid the outer quotes.

shell_command:
  denon_level_center_set: >-
    your_command
1 Like

Also, set you HA in debug to capture the stdout/stderr of the shell command.

1 Like

This one works perfect:

denon_level_center_reset: >-
  echo PSCLV 500 | nc 192.168.1.8 23

This one doesn’t work:

denon_level_center_set: >-
  echo PSCLV {{ ((states('input_number.denon_level_center')|float*10)+500)|int }} | nc 192.168.1.8 23

Logs return this:

2021-07-06 11:59:07 DEBUG (MainThread) [homeassistant.components.shell_command] Stdout of command: echo PSCLV {{ ((states('input_number.denon_level_center')|float*10)+500)|int }} | nc 192.168.1.8 23, return code: 0: b’PSCLV 510 | nc 192.168.1.8 23\n’

Not sure what that b’ means, final command should be echo PSCLV 510 | nc 192.168.1.8 23

Strange. It seems the pipe is passed as such to the echo when using templates.

Anyone haves an idea why this is happening? Thanks in advance!

Without ideas on the root issue, a workaround could be:

  • Create a simple bash script in, e.g., /config/PSCLV.sh
#!bash
echo PSCLV $1 | nc 192.168.1.8 23
  • use
denon_level_center_set: >-
  bash /config/PSCLV.sh {{ ((states('input_number.denon_level_center')|float*10)+500)|int }}

That worked! Thanks!