Music Assistant Playlist Automation (with Play/Pause)

I’m pretty good with If/Then statements but this had me stumped for an embarassingly long time understanding how homeassistant nested statements and triggers. So I thought I’d share for the community because I’ve been unable to find anything else like it.

I wanted a single button (Insteon Keypad scene switch) to trigger a particular YoutubeMusic playlist to start playing, but I wanted the button to pause the music when the button was pressed again, and then play again when pressed a third time without starting the entire station over again.

I also had to add some safegaurds so if the station timed out or HA was restarted, the station would also be spooled up to play.

Couple that with the fact that the Youtube playlists would start from the beginning every time it played the station. So I had to add a shuffle function to “True” after it starts playing (because I often toggle shuffe back off)

So this automation will start the requested station if its not already set to play, and just toggle the play function if it is. It will pause the music when the switch is in the off state, so its ready for the play button to toggle again and you don’t have to start a new song every time you pause.

alias: Garage Keypad (A) Top 40
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.keypadlinc_on_off_20_41_d0_button_a
condition: []
action:
  - if:
      - condition: state
        for:
          hours: 0
          minutes: 0
          seconds: 0
        entity_id: switch.keypadlinc_on_off_20_41_d0_button_a
        state: "on"
      - condition: and
        conditions:
          - condition: or
            conditions:
              - condition: state
                entity_id: media_player.mass_living_room_speaker
                state: "off"
              - condition: or
                conditions:
                  - condition: state
                    entity_id: media_player.mass_living_room_speaker
                    state: standby
              - condition: or
                conditions:
                  - condition: state
                    entity_id: media_player.mass_living_room_speaker
                    state: idle
    then:
      - service: media_player.play_media
        target:
          entity_id: media_player.mass_living_room_speaker
        data:
          media_content_id: media-source://mass/database://playlist/3
          media_content_type: audio/flac
        metadata:
          title: Today's Hits Clean 2023 - Clean Songs Playlist - Clean Music 2023
          thumbnail: >-
            https://images.weserv.nl/?w=200&url=https://yt3.googleusercontent.com/k7lNzFinL4OYymm5XnFmyDaLqpTBNQ4xyBrtGLW9NIQqOQG_RaCKZDBnQn3G8qK8N3LGsdwT7Fo=s1200
          media_class: playlist
          children_media_class: null
          navigateIds:
            - {}
            - media_content_type: app
              media_content_id: media-source://mass
            - media_content_type: music
              media_content_id: media-source://mass/playlists
      - service: media_player.shuffle_set
        data:
          shuffle: true
        target:
          entity_id: media_player.mass_living_room_speaker
    else:
      - if:
          - condition: state
            entity_id: switch.keypadlinc_on_off_20_41_d0_button_a
            state: "on"
          - condition: and
            conditions:
              - condition: state
                entity_id: media_player.mass_living_room_speaker
                state: paused
        then:
          - service: media_player.media_play
            data: {}
            target:
              entity_id: media_player.mass_living_room_speaker
  - if:
      - condition: state
        entity_id: switch.keypadlinc_on_off_20_41_d0_button_a
        state: "off"
    then:
      - service: media_player.media_pause
        data: {}
        target:
          entity_id: media_player.mass_living_room_speaker
mode: single

1 Like

This is very impressive. I have a question for you as I’m trying to do something much simpler. I’m trying to do an automation with the music assistant: queue command. I have Spotify and You Tube Music. With Spotify I can just state the name of the playlist and it works, but I can’t figure out how to get it to play a You Tube Playlist. Can you help me out or point me in the right direction? Thank You!

1 Like

I can try, I sort of cheat and make GUI automation first to get the framework then hand edit the fun stuff.

So for a YouTube Playlist, making sure it’s visible under Music Assistant and then Playlists, I pick “Play Media” and follow the prompts. Should look like attached.

Did you ever get this to work? This looks great. And i could have 6 Playlists - choose the playlist and choose destination (downstairs, upstairs, outside, everywhere). That’s all i need in a simple tablet gui. Thanks!

I have this working perfectly (most of the time) with Music Assistant beta now. Every once and a while the automation and Chrome get out of sync of each other and I have to run the “kill playlist” automation to reset things back to ground level and start over. I think it will be very solid once Music Assistant gets its final release.

Would you mind sharing your code? The Lovelace seems very interesting.

Thanks