Skip to content

Music Assistant Automation ​

Follow my other guide to Install Music Assistant on unRAID before proceeding. Or you could install the Music Assistant integration via HomeAssistant HACS.

Automation Examples ​

  • Search MA for tracks/album withfinal fantasy , limited to 500 items
  • Shuffle all 500 of them.
    `{{ results.track | shuffle` didn’t work, so we have to use some clever trick.
  • For the first track, clear the play queue and add a random item out of 500
  • Queue only 50 out of the shuffled 500 items.

Also shared to https://github.com/orgs/music-assistant/discussions/2400

alias: "[Template] Play Random Album <USE THIS AS BASE>"
sequence:
  - variables:
      search_name: final fantasy
      search_limit: 500
      play_queue_item_limit: 5
      search_media_type:
        - track
        - album
      target_entity_id: media_player.shield_3
      log: false
  - service: mass.search
    data:
      limit: "{{ search_limit }}"
      name: "{{ search_name }}"
      media_type: "{{ search_media_type }}"
    response_variable: results
  - variables:
      all_items: |
        {% set items = [] %} {% if results.tracks is defined %}
          {% set items = items + results.tracks %}
        {% endif %} {% if results.albums is defined %}
          {% set items = items + results.albums %}
        {% endif %} {{ items }}
      selected_items: >
        {% set items = all_items %} {% set ns = namespace(items=[]) %} {% for i
        in range(play_queue_item_limit) %}
          {% set item = items | random %}
          {% set ns.items = ns.items + [item] %}
          {% set items = items | reject('equalto', item) | list %}
        {% endfor %} {{ ns.items }}
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ log }}"
        sequence:
          - service: logbook.log
            data:
              name: Search Results
              message: "Selected Items: {{ selected_items | tojson }}"
  - service: mass.play_media
    data:
      media_id: "{{ selected_items[0].uri }}"
      enqueue: replace
    target:
      entity_id: "{{ target_entity_id }}"
  - repeat:
      count: "{{ play_queue_item_limit - 1 }}"
      sequence:
        - service: mass.play_media
          data:
            media_id: "{{ selected_items[repeat.index].uri }}"
            enqueue: add
          target:
            entity_id: "{{ target_entity_id }}"
mode: single
icon: mdi:album