#How to play music from a voice assistant automation?

1 messages · Page 1 of 1 (latest)

slender wing
#

Got a Voice Assistant PE, and trying to create a automation that allows me to start playing music via Music Assistant. However, whenever I try to send the media to Music Assistant, it seems to not go through, because Home Assistant decides to instead play either the conversation response, or 'Done', which takes over the media_player. Setting a delay or anything else also doesn't work- the 'response' plays at the end of the automation, regardless of how you set it up.

#

This is my automation (with sections taken from Music Assistant's current WIP local-only voice support)

#
alias: Music Assistant - Local(only) Voice Support
description: ""
triggers:
  - trigger: conversation
    command:
      - >-
        (play|listen to) [the ](album|ep|record|compilation|single) {media_name}
        [by [the ](artist|band|group) {artist}] [(in|on|using) [the
        ]{area_or_player_name}][ (with|using) {radio_mode}]
    id: album
  - trigger: conversation
    command:
      - >-
        (play|listen to) [the ][(track|song) ]{media_name} [by [the
        ][(artist|band|group) ]{artist}] [(in|on|using) [the
        ]{area_or_player_name}][ (with|using) {radio_mode}]
    id: track
  - trigger: conversation
    command:
      - >-
        (play|listen to) [the ](artist|band|group) {media_name} [(in|on|using)
        [the ]{area_or_player_name}][ (with|using) {radio_mode}]
    id: artist
  - trigger: conversation
    command:
      - >-
        (play|listen to) [the ]((radio station)|(radio)|(station)) {media_name}
        [(in|on|using) [the ]{area_or_player_name}]
    id: radio
  - trigger: conversation
    command:
      - >-
        (play|listen to) [the ]playlist {media_name} [(in|on|using) [the
        ]{area_or_player_name}][ (with|using) {radio_mode}]
    id: playlist
#
actions:
  - alias: Define variables to be used in the automation
    variables:
      version: 20250128
      assist_device_id: "{{ trigger.device_id }}"
      default_player_entity_id: >-
        {{ (device_entities(assist_device_id) | expand | selectattr('entity_id',
        'match', '^media_player') | map(attribute='entity_id') | list)[0] |
        default('media_player.navi_intercom_media_player_2') }}
      trigger_id: "{{ trigger.id }}"
      area_or_player_name: "{{ trigger.slots.area_or_player_name | default }}"
      action_data:
        media_id: "{{ trigger.slots.media_name }}"
        media_type: "{{ 'radio' if 'radio' in media_name | lower else trigger_id }}"
        artist: "{{ trigger.slots.artist | default }}"
        radio_mode: "{{ 'radio' in trigger.slots.radio_mode | default | lower }}"
      player_entity_id_by_player_name: |
        {{ integration_entities('music_assistant') | expand 
          | selectattr('name', 'match', area_or_player_name ~ '$', ignorecase=true)
          | map(attribute='entity_id') | list }}
      player_entity_id_by_area_name: >
        {{ areas() | map('area_name') | select('match', area_or_player_name ~
        '$',  ignorecase=true) | map('area_entities') | sum(start=[]) |
        select('in',  integration_entities('music_assistant')) | list }}
      player_entity_id_by_assist_area: >
        {{ area_entities(area_id(trigger.device_id)) | select('in',
        integration_entities('music_assistant')) | list }}
      mass_player_entity_id: |
        {{ player_entity_id_by_player_name or player_entity_id_by_area_name 
        or player_entity_id_by_assist_area or [default_player_entity_id] }}
      mass_player_name: >-
        {{ mass_player_entity_id | map('state_attr', 'friendly_name') | join(',
        ') }}
#
  - alias: Send back the response
    set_conversation_response: "{{ trigger.slots.media_name }} playing on {{ mass_player_name }}"
    enabled: true
  - alias: Send media to selected Music Assistant Player
    action: music_assistant.play_media
    data: "{{ dict(action_data.items() | selectattr('1')) }}"
    target:
      entity_id: "{{ mass_player_entity_id }}"
mode: single
#

no errors or anything in the logs/traces/history, as far as I can tell the action is just getting eaten by homeassistant insisting on having some kind of end-of-automation playback that happens after the automation triggers, I think

wet temple
#

if its saying "done" its likely erroring/failing somewhere. most likely the answer will be in the trace somewhere

slender wing
#

Nah, the 'done' is just if I remove the 'set_conversation_response'

#

otherwise it says 'playing XYZ on ABC' but then never actually does anything

#

if I disable set_conversation_response, it says 'done' at the end, which also clobbers the actual media playback

#

actually hang on, let me try setting the conversation response to ""

wet temple
#

as long as your using the music assistant generated media player entity it should handle TTS over playback

slender wing
#

that's the problem- it IS

#

in the automation, it sends the music to the VA PE

#

then immediately changes the playback to whatever the response is

wet temple
#

i just imported the blueprint to compare what it generates for me

#

did you manually template the default_player_entity_idsection?

slender wing
#

yeah

#

I also just modified it to try the secondary media player for the device- they have two, one for MusicAssistant and one for ESPHome that seem to be tied together

#

works the same way regardless of which you send it to

#

e.g. if it resolves to 'media_player.navi_intercom_media_player' (the esphome media player) or 'media_player.navi_intercom_media_player_2' ( the musicassistant media player) it doesn't matter, behaves the same way

wet temple
#

what happens if you scrap the templating and just have
default_player_entity_id: media_player.navi_intercom_media_player_2

#

when your sending data to to music assistant you only ever want to use the music assistant player anyway which will likely be the _2 one

slender wing
#

hmm. so I think I tracked down the issue testing your suggestion

#

if I hard set default_player_entity_id, it doesn't work.

#

if I hard set the target:entity_id in the 'send media', it does

#

and I think it's because in mass_player_entity_id, for some reason the chunk of code I copied from musicassistant has the default_player_entity_id being passed as an array?

#

lemme see if this works

#

and it does!