Passing parameters through multiple levels: Automation --> Script --> Shell Command

Hi All,

I am confused with the following sentence in this documentation: https://www.home-assistant.io/integrations/shell_command/

“Any service data passed into the service call to activate the shell command will be available as a variable within the template.”

To me this implies that if I call the script from an automation, and that script in turn calls the shell command, the shell command can use the variables that I passed to the script.

Allow me to illustrate with an example. I have a shell_command in configuration.yaml

shell_command:
    play_sound: /usr/bin/omxplayer /home/homeassistant/sounds/{{filename}}

If I call it from a script like so:

play_a_sound:
  alias: Play a sound from a file
  sequence:
  - delay: 00:00:02
  - data:
      filename: Ship_Bell.mp3
    service: shell_command.play_sound

it works fine, however, if I call it from an automation (first removing the filename from the script), like so:

  - data:
      filename: Ship_Bell.mp3
    service: script.play_a_sound

it no longer works. The filename parameter does not get passed down all the way to shell_command despite what it says in the documentation. Perhaps I am misunderstanding that sentence. Anyone has any ideas how to do this correctly?

Yes, at each level you need to pass it on:

automation:
- ...
  action:
  - service: script.play_a_sound
    data:
      filename: Ship_Bell.mp3
script:
  play_a_sound:
    alias: Play a sound from a file
    sequence:
    - delay: 00:00:02
    - data_template:
        filename: "{{ filename }}"
      service: shell_command.play_sound
1 Like