Randomize and enqueue items to media player (req. MusicAssistant Integration)

A script that allow you to define how many items to enqueue to media_player. Best used with RFID Tag based automation, e.g. a tag scan that randomly play all final fantasy album/tracks, but limited in scope to reduce the amount of API calls.

You can edit the variables ONLY in the first block. The rest are templated.

####################################### 
# By D.Sync (dsync89)
# Last Update: June 2, 2024
# Version: 1.0
# Inspired by @ministryofsillywalks
# This script further enhance the script by @ministryofsillywalks to enqueue the tracks
####################################### 
alias: Play Random Track/Album limited to user defined number
sequence:
  - variables: # user-defined variables
      search_name: final fantasy # keyword to search  
      search_limit: 500  # how many items to search, default is very little, so we have to explicitly set it here
      play_queue_item_limit: 5 # how many items out of the search_limit to add to the play queue. NOTE: the limit does not apply if album is included in search_media_type because it will add all tracks in that album
      search_media_type: # currently support track and album
      - track
      - album
      target_entity_id: media_player.shield_3 
      log: false # set to true to log to logbook
#######################################      
# DO NOT MODIFY
#######################################
  - service: mass.search
    data:
      limit: "{{ search_limit }}"
      name: "{{ search_name }}"
      media_type: "{{ search_media_type }}"
    response_variable: results
  # - service: logbook.log
  #   data:
  #     name: "Search Results"
  #     message: "{{ results.tracks | tojson }} items found."    
  - 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 # replace the existing play queue
    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 # add the remaining items to the play queue
          target:
            entity_id: "{{ target_entity_id }}"  
mode: single
icon: mdi:album