TTS Script error: Invalid data for call_service at pos 2

I’ve copied (and slightly modified) a neat script to allow TTS to interrupt a music player and then resume, (source here: https://community.home-assistant.io/t/how-continue-play-the-previous-music-after-a-tts-voice-announcement/253053/10?u=crowbarhero) however I’m getting an error on execution:

tts and resume: Error executing script. Invalid data for call_service at pos 2: required key not provided @ data['media_player_entity_id']

Looking at the traces, I think it’s an issue with how the variables (player id and message) are passed, in that it’s not picking up the player ID properly.

What’s wierd is that it is suceeding in setting the volume level on that player, but not in passing the TTS bit??

Executed: December 31, 2024 at 08:52:54
Error: required key not provided @ data['media_player_entity_id']
Result:
params:
  domain: tts
  service: speak
  service_data:
    entity_id: media_player.squeezeoffice
    language: en
    message: Test message
  target: {}
running_script: false

Any thoughts as to how I can get this working… ?

Full script below:

alias: tts and resume
variables:
  mediaplayer_State: "{{ states(tts_entity) }}"
  mediaplayer_media_content_id: "{{ state_attr(tts_entity,'media_content_id') }}"
  mediaplayer_Source: "{{ state_attr(tts_entity,'media_channel') }}"
  mediaplayer_volume_level: "{{ state_attr(tts_entity,'volume_level') }}"
  mediaplayer_content_type: "{{ state_attr(tts_entity,'media_content_type') }}"
sequence:
  - data:
      entity_id: "{{ tts_entity }}"
      volume_level: 0.75
    action: media_player.volume_set
  - data:
      entity_id: "{{ tts_entity }}"
      language: en
      message: "{{ msg }}"
    action: tts.speak
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - data:
      entity_id: "{{ tts_entity }}"
      volume_level: "{{ mediaplayer_volume_level }}"
    action: media_player.volume_set
  - data:
      entity_id: "{{ tts_entity }}"
      media_content_id: "{{ mediaplayer_media_content_id }}"
      media_content_type: "{{ mediaplayer_content_type }}"
    action: media_player.play_media
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ mediaplayer_Source != None }}"
        sequence:
          - data:
              entity_id: "{{ tts_entity }}"
              source: "{{ mediaplayer_Source }}"
            action: media_player.select_source
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ mediaplayer_State != 'playing' }}"
        sequence:
          - data:
              entity_id: "{{ tts_entity }}"
            action: media_player.media_pause
mode: single
fields:
  tts_entity:
    description: media player entity id
    required: true
    name: tts_entity
  msg:
    name: msg
    description: message
    required: true

Please show how you are calling this script.

Hi Tom - thanks for the swift response… The script is being called from an automation, currently it’s just the action end as I’m triggering it manually:

alias: TTS test2
description: ""
triggers: []
conditions: []
actions:
  - target:
      entity_id: script.tts_and_resume
    data:
      variables:
        msg: Test message
        tts_entity: media_player.squeezeoffice
    action: script.turn_on
mode: single

That looks ok. Does this media player support TTS media_player.squeezeoffice?

Not all do.

I reckon so, I’ve managed to directly play TTS through that media player (it’s the Lyrion Music server integration -formerly logitech/squeezebox)

the following works fine:

action: tts.speak
metadata: {}
data:
  cache: false
  media_player_entity_id: media_player.squeezeoffice
  message: test message
target:
  entity_id: tts.piper

though I notice that tts.piper is referred to explicitly in this case, and the media player is referred to with a different key… could that be the issue?

Probably. Try changing the action from:

  - data:
      entity_id: "{{ tts_entity }}"
      language: en
      message: "{{ msg }}"
    action: tts.speak

to

  - action: tts.speak
    metadata: {}
    data:
      cache: false
      media_player_entity_id: "{{ tts_entity }}"
      message: "{{ msg }}"
    target:
      entity_id: tts.piper

Thanks Tom,

That’s gotten past the error, and the script now executes without any errors.

it’s not doing quite what I want, but that’s another problem… :slight_smile:

1 Like