Shell command with variable not working

Hello HA community. I’m new to HA and am having trouble calling a shell script from a button WITH a variable. It works fine without the variable. I’ve tried various permutations of with and without single and double quotes. I have also tried to call the shell command directly from the button and it display same pattern as above (works without a variable, but not with).

What am I doing wrong or missing?

My button is configured as:

type: custom:button-card
name: Shade Down
tap_action:
  action: call-service
  service: script.send_to_somfy
  service_data:
    somfy_cmd: 0109D

My script is configured as:

send_to_somfy:
  sequence:
    - service: shell_command.send_to_somfy
      target:
        entity_id: shell_command.send_to_somfy
      data_template:
        somfy_command: "{{ somfy_cmd }}"
    - service: system_log.write
      data:
        message: "{{ somfy_cmd }}"  # <---- This gets written correctly to the log
        level: warning

My configuration.yaml has this shell_command:

shell_command:
  send_to_somfy: echo {{ somfy_command }} >> /dev/ttyS0  # <---- This DOESN'T work
  shade_mancave_up: echo "0109U" >> /dev/ttyS0  # <---- This works!!
  shade_mancave_up2: echo 0109U >> /dev/ttyS0  # <---- This works!!

I’m very very very much a rookie in this, but have you tried it with a single quote ’ vs. " ?

CFC

Thank you. I did, it did not change the outcome.

I figured it out. I need to call a bash script that fires off the echo command.

shell_command:
  send_to_somfy: /bin/bash -c "echo '{{ somfy_command }}' >> /dev/ttyS0"

And as an added bonus, I can skip the script part and call the shell command directly from the button.

Now I can control all my awnings and shades with just this one command! Yah.

1 Like