Randomly Pick Action in "Choose" Automation

I am looking to set up a single automation to play music from a voice command. I currently have it set up to listen for a sentence, and then I have a “Choose” action set up with several different play lists that I would like it to randomly select from to play on the given media device. As it stands now, it looks like it will only play the first one (I am trying to avoid tying the choose options to custom trigger_ids to make the selection). Below is an example of what I have so far:

alias: Voice - Play Music
description: ""
trigger:
  - platform: conversation
    command: play music
  - platform: conversation
    command: play something else
condition: []
action:
  - choose:
      - conditions: []
        sequence:
          - service: media_player.play_media
            target:
              entity_id: media_player.spotify_device
            data:
              media_content_id: spotify:playlist:example
              media_content_type: spotify://playlist
            metadata:
              title: Title
              thumbnail: >-
                https://mosaic.scdn.co/640/ab67616d0000b2736da502e35a7a3e48de2b0f74ab67616d0000b2738f6b4035c82eb9cf42e9d8d7ab67616d0000b273d1fd8f264564d9eef7617a45ab67616d0000b273f9b3ece3271d3a5fa73d3759
              media_class: playlist
              children_media_class: track
              navigateIds:
                - {}
                - media_content_type: spotify://current_user_playlists
                  media_content_id: current_user_playlists
                - media_content_type: spotify://playlist
                  media_content_id: spotify:playlist:example
      - conditions: []
        sequence:
          - service: media_player.play_media
            target:
              entity_id: media_player.spotify_device
            data:
              media_content_id: spotify:playlist:example
              media_content_type: spotify://playlist
            metadata:
              title: Title
              thumbnail: >-
                https://mosaic.scdn.co/640/ab67616d0000b2732b617b9b905a71a023912f14ab67616d0000b2739d9f314922f1decc81349d93ab67616d0000b273b42c10508ff1318d68663421ab67616d0000b273e673240af5b70b96fddac9dc
              media_class: playlist
              children_media_class: track
              navigateIds:
                - {}
                - media_content_type: spotify://current_user_playlists
                  media_content_id: current_user_playlists
                - media_content_type: spotify://playlist
                  media_content_id: spotify:playlist:example
      - conditions: []
        sequence:
          - service: media_player.play_media
            target:
              entity_id: media_player.spotify_device
            data:
              media_content_id: spotify:playlist:example
              media_content_type: spotify://playlist
            metadata:
              title: Title
              thumbnail: https://i.scdn.co/image/ab67706f000000039205ddd6990d57d56db91b81
              media_class: playlist
              children_media_class: track
              navigateIds:
                - {}
                - media_content_type: spotify://categories
                  media_content_id: categories
                - media_content_type: spotify://category_playlists
                  media_content_id: id_here
                - media_content_type: spotify://playlist
                  media_content_id: spotify:playlist:example
mode: single
action:
  - variables:
      i: "{{ [1,2,3] | random }}"
  - choose:
      - conditions: "{{ i == 1 }}"
        sequence:
           ... etc ...
      - conditions: "{{ i == 2 }}"
        sequence:
           ... etc ...
      - conditions: "{{ i == 3 }}"
        sequence:
           ... etc ...

FWIW, it isn’t necessary to use choose for this application. It can be done by templating the options of a single media_player.play_media service call.

3 Likes

I’m new to HA and I’m mildly familiar with templates within the HA ecosystem but I don’t understand YAML very well yet. Could someone explain how you’d go about doing that? Even a quick psuedocode mockup with a description of how to implement it would be more than enough, thanks!

Unfortunately I don’t have any free time available for this topic. Hopefully someone else can help you.