Help with passing variables to script

Tags: #<Tag:0x00007f326a475ad8> #<Tag:0x00007f326a4758f8>

I’m following the documentation for scripts and passing variables but I’m having some issues.

I have the following tap-action in a button. It fires my script.

type: 'custom:button-card'
entity: media_player.sonos_bedroom_beam
name: Bedroom
tap_action:
  action: call-service
  service: script.test
  data:
    coordinator: Ceiling Speakers # I put quotes around these but saving removes them.
    speaker: Bedroom # I put quotes around these but saving removes them.

And I have the following script.

test:
  alias: Test
  fields:
    coordinator:
      description: The main speaker in the group
      example: Deck
    speaker:
      description: The speaker joining the group
      example: Living Room
  sequence:
  - service: mqtt.publish
    data: # Tried both data and data_template
      topic: "home-assistant/sonos/group/{{ coordinator }}"
      payload: "{{ speaker }}"
  mode: single

Which sends the mqtt message but neither coordinator nor speaker have a value. Can anyone spot where I’ve gone wrong?

Try single quotes instead of double quotes ?

In tap_action, replace:

data:

with

service_data:

From the documentation:

service_data string (Optional, default: none)
Service data to include (e.g., entity_id: media_player.bedroom) when action defined as call-service

That’s what I get for looking at the example from the scripts documentation and assuming it had the same structure. Question - is the example at https://www.home-assistant.io/integrations/script/#passing-variables-to-scripts incorrect or does one case use data and another use service_data?

There are differences between Lovelace’s syntax and what you see elsewhere in Home Assistant. For example, you may have noticed the use of a hyphen such as in call-service whereas hyphens are never used elsewhere for options and service names (the underscore is used).