Can someone help me figure out why I can't run a script from a card?

Can someone help me figure out why I can’t run a script from a card?
Here is my card code:

type: custom:button-card
name: Play File 1
tap_action:
  action: call-service
  service: script.turn_on
  service_data:
    entity_id: script.play_mp3_file
  target:
    entity_id:
      - script.play_mp3_file
  data:
    file: 1

No errors, but the file is not playing.

From developer tools, I can play file 1:

service: script.play_mp3_file
data: 
 file: 1

Here is the code for the script:

alias: play_mp3_file
sequence:
  - service: esphome.dfplayer_dfplayer_play
    data:
      file: "{{ file }}"
mode: single
icon: mdi:music

Any tips would be appreciated.

I’m not entirely sure, but service_data was deprecated I think. Try without, you shouldn’t need it anyway.

tap_action:
  action: call-service
  service: script.turn_on
  target:
    entity_id:
      - script.play_mp3_file
  data:
    file: 1

If i remember right, you’re overwriting data with service_data or vice versa. :laughing:

You should just copy what you did in developer tools. Ie.

type: custom:button-card
name: Play File 1
tap_action:
  action: call-service
  service: script.play_mp3_file
  data: 
   file: 1

Thanks for the tip, but

type: custom:button-card
name: Play File 1
tap_action:
  action: call-service
  service: script.turn_on
  target:
    entity_id:
      - script.play_mp3_file
  data:
    file: 1

gives this error:
“Failed to call service/script.turn_on. must contain one of entity_id, device_id or area_id.”

I am sure that I tried this, but

type: custom:button-card
name: Play File 1
tap_action:
  action: call-service
  service: script.play_mp3_file
  data: 
   file: 1

gives this error:
Failed to call service/script.play_mp3_file. expected int for dictionary value @ data[“file”]

The last time I looked, 1 was an int?

That format is working for me with a script that requires an int, post the trace of the script.

Here are the Step Details:

Result:
params:
  domain: esphome
  service: dfplayer_dfplayer_play
  service_data:
    file: ''
  target: {}
running_script: false

Interesting that file: is null?

I have another test script that does not take an argument- the file number is hard coded:

alias: play_mp3_file_4
sequence:
  - service: esphome.dfplayer_dfplayer_play
    data:
      file: 4
mode: single
icon: mdi:music-note-eighth

This one works and the trace shows file: 4
It’s only when trying to pass the file number as an argument that it fails.

Honestly this is the first script that I have written that takes a parameter.
Here, again for reference, is the failing script:

alias: play_mp3_file
sequence:
  - service: esphome.dfplayer_dfplayer_play
    data:
      file: "{{ file }}"
mode: single
icon: mdi:music

I don’t see anything glaringly wrong… I would try it with defined variable fields and force the template to output an integer. It’s probably just coincidence, but I’ve had a few instances where a variable would not pass correctly until I set up the fields block.

alias: play_mp3_file
fields:
  file:
    name: File
    description: "The number of the file"
    required: true
    example: 1
    selector:
      number:
        max: 10000
        min: 1
        step: 1
sequence:
  - service: esphome.dfplayer_dfplayer_play
    data:
      file: "{{ file | int(1) }}"
mode: single
icon: mdi:music

Thanks for the tip, but I am getting another error:
Failed to call service script/play_mp3_file. Error rendering data template: UndefinedError: 'file' is undefined

Reading this (passing variables to scripts) confused me more.