Data template help - processing arrays with variables

I have a logitech media player which takes JSON RPC api requests. Service call works fine from Hass.io service UI

entity_id: “media_player.sbhifi_touch”
command: “playlist”
parameters: [“play”,“iplayer://live?dash=dash://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/dash/uk/dash_full/ak/bbc_6music.mpd”]

With the player, playing the music successfully :slight_smile:

I am trying to convert this to an automation called by a webhook whereby the URL is passed in as a trigger.event.data.parameter

How can I create an automation template to make the following service request?

My current version which works for most commands is as follows. But the parameters array screws it up.

- id: 04727b5d6a074b6494859a35676ac4fc
  alias: IFTTT Squeezebox
  trigger:
  - platform: event
    event_type: ifttt_webhook_received
    event_data:
      action: 'call_service_squeezebox'
  action:
  - service_template: '{{ trigger.event.data.service }}'
    data_template:
      entity_id: '{{ trigger.event.data.entity_id }}'
      command: '{{ trigger.event.data.command }}'
      parameters: '{{ trigger.event.data.parameters }}'

The above works for most my commands other than this playlist command. It seems to fall apart because parameters takes and array of “play” and the $URL.

How can I pass the array with one static item and one variable into the automation??

Thanks -

- id: 04727b5d6a074b6494859a35676ac4fc
  alias: IFTTT Squeezebox
  trigger:
  - platform: event
    event_type: ifttt_webhook_received
    event_data:
      action: 'call_service_squeezebox'
  action:
  - service_template: '{{ trigger.event.data.service }}'
    data_template:
      entity_id: '{{ trigger.event.data.entity_id }}'
      command: '{{ trigger.event.data.command }}'
      parameters: ["play", '{{ trigger.event.data.parameters }}']

The above as a hard code bodge has since worked.

Would be great to know how to pass “play” in as a second variable, akin to parameters[0] = play and parameters[1] = URL.

A template can only output a single string. It is not possible to create a YAML list from a template because the template is rendered after the YAML is processed. If you try to create what looks like a YAML array from a single template what you’ll get is a string that just looks like a YAML list.

The only way (that I know of) to do what you want is to make the two parameters of the list separate items in trigger.event.data, something like:

- id: 04727b5d6a074b6494859a35676ac4fc
  alias: IFTTT Squeezebox
  trigger:
  - platform: event
    event_type: ifttt_webhook_received
    event_data:
      action: 'call_service_squeezebox'
  action:
  - service_template: '{{ trigger.event.data.service }}'
    data_template:
      entity_id: '{{ trigger.event.data.entity_id }}'
      command: '{{ trigger.event.data.command }}'
      parameters: ['{{ trigger.event.data.parameter1 }}', '{{ trigger.event.data.parameter2 }}']

or:

- id: 04727b5d6a074b6494859a35676ac4fc
  alias: IFTTT Squeezebox
  trigger:
  - platform: event
    event_type: ifttt_webhook_received
    event_data:
      action: 'call_service_squeezebox'
  action:
  - service_template: '{{ trigger.event.data.service }}'
    data_template:
      entity_id: '{{ trigger.event.data.entity_id }}'
      command: '{{ trigger.event.data.command }}'
      parameters:
        - '{{ trigger.event.data.parameter1 }}'
        - '{{ trigger.event.data.parameter2 }}'