How to create this intent_script?

I’m hacking on a custom component that uses the intent/handle endpoint to trigger various actions. One behavior I’d like is getting the currently playing song from a given media player. I’ve tried a few things, including these two intent scripts

HassGetCurrentSong:
  action:
    - service: media_player.media_play
      target:
        entity_id: "media_player.{{ name }}"
  speech: 
    text: "this is a test"

The above targets the right media player when i call it with {"intent":"HassGetCurrentSong", "data": {"name": "Office"}}, but invoking media_play is of course not what I want.

HassGetCurrentSong:
  speech: 
    text: "{{ state_attr('media_player.office', 'media_title') }} by {{ state_attr('media_player.office', 'media_artist') }}"

This one returns the currently playing song, but I don’t know how to modify it to target the given media_player.

Welcome! :grin:

I use this to target a particular Sonos media player:

    sequence: 
      - target:
          entity_id:
            - "{{ states('sensor.speaker') }}"
        data:
          announce: true
          media_content_id: |
            media-source://tts/amazon_polly?message="{{ tts_sentence }}"
          media_content_type: music
          extra:
            volume: 50
        action: media_player.play_media

You’d have to substitute the name of your TTS engine for amazon_polly.

This is part of a separate script which handles all TTS - it makes intents much simpler (and saves typing):

# Custom sentence: What time is it?
# sensor.speaking_clock template renders system time in colloquial speech eg "half past five"

CustomWhatTime:
  action:
    - service: script.tts_response
      data:
        tts_sentence: It's {{ states('sensor.speaking_clock') }}

@jackjourneyman Thanks for your response, however I’m still a bit confused. If I understand your example correctly, it looks like your script plays some speech audio from a given speaker. What I’m trying to do is fetch the name and artist of the currently playing song, not play something else.

The confusion might partly be caused by how I’m using speech in my intent script. Since my integration is built on the OpenAI Realtime API, I’m not using separate models for STT, LLM and TTS.

The goal is for the user input to produce a function call like {"intent":"HassGetCurrentSong", "data": {"name": "Office"}} (this already works), and then have the intent script fetch the currently playing song on the media player named office

If the variable name already returns the correct object ID, use that to construct the required entity ID by concatenating the strings:

HassGetCurrentSong:
  speech: 
    text: |
      {% set player = 'media_player.'~ name %}
      {{ state_attr(player, 'media_title') }} by {{ state_attr(player, 'media_artist') }}

Thank you! That works!