Variables and entity_id

Hello,

I’m trying to pass two variables from an automation to a script to play a sound.

The script works if I hardcode the values, it is only when I try to use the variables that I get errors. I feel like I’m missing something obvious, any suggestions?

My automation:

- id: test_play_sound
  alias: Testing Playing a Sound
  initial_state: 'off'
  trigger:
    - platform: sun
      event: sunset
      offset: "00:15:00"
  action:
    - service: script.play_sound
      data:
        sound_file: 'http://10.5.1.90:8123/local/notification_chime.m4a'
        speaker_location: 'media_player.kitchen_1'

My script:

play_sound:
  alias: "Play a sound effect"
  sequence:
#
    - service: media_player.sonos_unjoin
      data_template:
        entity_id: >
          '{{speaker_location}}'

    - service: media_player.volume_set
      data_template:
        entity_id: >
          '{{speaker_location}}'
      data:
        volume_level: 0.4

    - service: media_player.play_media
      data_template::
        entity_id: >
          '{{speaker_location}}'
        media_content_id: >
          '{{sound_file}}'
        media_content_type: >
          'music'

This does not work, you cannot mix data_template and data. Simply remove the data: line, so that volume_level becomes part of the data_template: section. Like this:

- service: media_player.volume_set
  data_template:
    entity_id: '{{speaker_location}}'
    volume_level: 0.4

That is good to know. I’m still getting a startup error though even with the change.

The error is:

Invalid config for [script]: [data_template:] is an invalid option for [script]. Check: script->script->play_sound->sequence->2->data_template:. (See /home/ha/.homeassistant/configuration.yaml, line 87). Please check the docs at https://home-assistant.io/components/script/`

I’m importing my scripts in my configuration.yaml file with:

script: !include_dir_merge_named scripts

Could this be the issue; I tried putting the script directly in the configuration.yaml file but I still received the same error?

Here is what my current script looks like:

play_sound:
  alias: "Play a sound effect"
  sequence:
#
    - service: media_player.sonos_unjoin
      data_template:
        entity_id: '{{speaker_location}}'

    - service: media_player.volume_set
      data_template:
        entity_id: '{{speaker_location}}'
        volume_level: 0.4

    - service: media_player.play_media
      data_template::
        entity_id: '{{speaker_location}}'
        media_content_id: '{{sound_file}}'
        media_content_type: 'music'

There’s a second colon after data_template, turning that attribute into “data_template:” as stated in the error message.

Thank you!! I totally overlooked that typo; I am going to need to install a YAML lint plugin and start using an IDE - maybe develop a build sequence before deploying.

Also, I don’t know why, but my mind just kept focusing on the media_player.volume_set sequence when I saw “2” in the error - I just blanked and I didn’t realize that the error message was an array and I should of went to the third service.

I can’t tell you how long I stared at this code thinking everything looks fine. I guess using IDEs all day have made be lazy and not so sharp…

Thanks again for the help. Everything is working fine.

Also, as a side note to anyone who has this similar issue, I had to remove the quotes in the automation variables to get everything to work.

That single-quote-in-the-variable-thing had me stumped for 24 hours, until I stumbled on this! You should put it in big bold letters, saved my (second) day!
Thanks