Music Assistant 2.0: Your Music, Your Players

variables do work, I tested them. You have to send the variable to the script from the automation like so:-

actions:
  - action: script.play_random_songs_from_music_assistant
    data:
      loopsize: 20

I am running it as a script, so how to do that in my script? Sorry, not so expert…

whats triggering the script ?
Usually thats an automation, or a button press from the UI

This automation triggers the script when the voice assist hears a sentence, but your trigger could be anything, like when a motion sensor detects you in a room:

alias: Voice - Play (some) Music
description: ""
triggers:
  - trigger: conversation
    command:
      - >-
        play [some | any] [random] (music | tunes | tracks)
conditions: []
actions:
  - action: script.play_random_songs_from_music_assistant
    data:
      loopsize: 20
mode: single
1 Like

Actually I call it with assist.

The above automation should work then. I have actually extended the script so that music plays in whatever room your voice assist is in, assuming you’ve setup areas for your MA media players and voice assistants

May i have that code?

This is the extended ‘smarter’ version…

First the automation:

alias: Voice - Play (some) Music
description: ""
triggers:
  - trigger: conversation
    command:
      - >-
        play [some | any] [random] (music | tunes | tracks) [in] [the]
        [{def_area}]
conditions: []
actions:
  - action: script.play_random_songs_from_music_assistant
    data:
      loopsize: 20
      target_area: >-
        {{
        area_id(trigger.slots.def_area) if trigger.slots.def_area is defined
        else area_id(trigger.device_id) }}
mode: single

And then the script:

alias: Play Random Songs From Music Assistant
description: ""
mode: single
sequence:
  - repeat:
      count: "{{ loopsize }}"
      sequence:
        - action: music_assistant.get_library
          data:
            config_entry_id: putyouridhere
            limit: 1
            media_type: track
            order_by: random
          response_variable: random_tracks
        - action: music_assistant.play_media
          data:
            media_id: "{{ random_tracks['items'][0].uri }}"
            media_type: track
            enqueue: "{% if repeat.index == 1%} play {%else%} add {%endif%}"
          target:
            area_id: "{{target_area}}"
variables:
  loopsize: "{{loopsize if defined else 10}}"
  target_area: "{{target_area if defined else 'lounge'}}"

On the last line the word ‘lounge’ is a fallback for an area incase the automation doesn’t send one, or mishears you. Put whatever area name you want in there as your fallback. It has to match one of your defined areas for it to work.

Or just remove if defined else ‘lounge’ from the line and it will just silently fail.

1 Like

Last question: is there a way to automatically clean the MA code before sending those commands? I noticed if i do the command, the previous code is played just after the first random song.
Thanks for the support.

Do you mean you want to clear the playlist first of anything before the command sends the new lists of songs ?

Yes, exactly…

Try changing your script to this:

alias: Play Random Songs From Music Assistant
description: ""
mode: single
sequence:
  - action: media_player.clear_playlist
    metadata: {}
    data: {}
    target:
      area_id: "{{target_area}}"
  - repeat:
      count: "{{ loopsize }}"
      sequence:
        - action: music_assistant.get_library
          data:
            config_entry_id: youridhere
            limit: 1
            media_type: track
            order_by: random
          response_variable: random_tracks
        - action: music_assistant.play_media
          data:
            media_id: "{{ random_tracks['items'][0].uri }}"
            media_type: track
            enqueue: "{% if repeat.index == 1%} play {%else%} add {%endif%}"
          target:
            area_id: "{{target_area}}"
variables:
  loopsize: "{{loopsize if defined else 10}}"
  target_area: "{{target_area if defined else 'lounge'}}"
2 Likes

Thanks a lot, i’ll try it as back home.

Random Power Off coming up while playing music?

Yep. I just discovered that although my router is allowing mDNS through, something with my firewall is still blocking the ability to play anything. I really wish there was a way this all worked with just IP addresses instead of local hostnames… grrrrrrr.

My hack to playing genre’s of music in Music Assistant. I’m using voice to trigger it, but your triggers could be anything you like.

First, you need to create a bunch of playlists and put your appropriate genre music files in them. You can call the playlists anything you like, it doesn’t matter, but I’ve called mine by the genre they represent.

Add the playlists to MA.

Add the following automation to HA:

alias: Play music by playlist genre
description: ""
triggers:
  - trigger: conversation
    command:
      - play [some] [{genre}] (music | tunes | tracks) [in] [the] [{def_area}]
conditions: []
actions:
  - action: music_assistant.play_media
    metadata: {}
    data:
      media_id: >
        {% set genre = trigger.slots.genre | replace(' ', '')  | replace('_',
        '') | lower %}  {% set genre = '_' + genre %} {% set genreMap = { 
        '_deephouse' : 'library://playlist/23', '_dance' :
        'library://playlist/24', '_chillout' : 'library://playlist/25', '_' :
        'library://playlist/5'} %} {{ genreMap[genre] }}
      enqueue: replace
    target:
      area_id: |
        {{ trigger.slots.def_area if defined else area_id(trigger.device_id) }}
    enabled: true
  - set_conversation_response: I've put on some {{ trigger.slots.genre }} music.
    enabled: true
mode: single
variables:
  number_of_tunes: 20

The bits you need to change are in the genreMap section.
Change the name you want spoken to whatever you want, mine are ‘_deephouse’, ‘_dance’ etc. (You don’t say the underscore when speaking, its added later). If you want to say multiple words, thats fine, but put them all together as one long word in the genreMap.

Your names should be in lower case and start with an underscore character.
The last item in the genreMap is a name just called ‘_’ which is the fallback if you don’t say a genre when speaking the command, I’ve got mine set to MA’s random 500 tracks playlist.

Change the playlist library Uri’s to whatever yours are. You can get these by using the dev tools and calling the music assistant get library call, selecting playlists as one of the options.

That should do it.

If you don’t mention where to play the music, it will try to find a media player in the same area as the voice assistant, otherwise it will find the media player in the area you mention and play the music there.

1 Like

Love this. Is there a way to use this as a media source with the native HA media browser?

If you select a MA media player from within that browser you will see the MA library although the interface is limited compared to the MA UI

Anyone have fix for Sonos S1 music turning off? It looks like MA or something is shutting the player off or putting them into standby? Some times it will play for over an hour and other times it will play 2 songs then the music stops. If I disable MA and just use the S1 App everything works as it should. Attached are settings:



i have a similar issue but no fix sorry. Although the sonos has excellent sound it constantly drops off my network, disconnects from google home and just generally is a pain in the arse