Actionable Notification "Media Player Widget"

So recently I have gotten into using Music Assistant and enjoying it. The media player widget on my phone does a decent job to control but I thought I would try to emulate the lock screen widget of an android media player - to save time unlocking my phone etc.

The intended function is for it to appear once music is playing, and then go away once the MA player is turned off. Once working I thought it might be of use to others!

But there are two parts I just cannot figure out…

  • how to get the title of the current song
  • and more importantly, make the buttons work!

I’ve tried building the actionable part in several ways but none of them seem to register the button presses. The yaml as it is now is essentially copy/pasted from the most common templates on the web but no joy still… I scoured for dumb typos so many times but cannot see it, if it’s there! Any help would be appreciated :slight_smile:

alias: Music Notifications
description: ""
trigger:
  - platform: state
    entity_id:
      - media_player.denon_avr_ma
    to: playing
    id: musicon
  - platform: state
    entity_id:
      - media_player.denon_avr_ma
    to: "off"
    id: musicoff
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - musicon
        sequence:
          - variables:
              action_previous: "{{ 'PREVIOUS_' ~ context.id }}"
              action_play: "{{ 'PLAY_' ~ context.id }}"
              action_next: "{{ 'NEXT_' ~ context.id }}"
          - service: notify.mobile_app_galaxy_s20
            data:
              title: Music Assistant
              message: Song 
              data:
                channel: Music Notifications
                clickAction: /d5369777_music_assistant_beta
                sticky: true
                persistent: true
                notification_icon: mdi:audio-video
                tag: musicon
                actions:
                  - action: "{{ action_previous }}"
                    title: Previous
                  - action: "{{ action_play }}"
                    title: Play/Pause
                  - action: "{{ action_next }}"
                    title: Next
      - conditions:
          - condition: trigger
            id:
              - musicoff
        sequence:
          - service: notify.mobile_app_galaxy_s20
            data:
              message: clear_notification
              data:
                tag: musicon
  - wait_for_trigger:
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{ action_previous }}"
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{ action_play }}"
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{ action_next }}"
  - choose:
      - conditions: "{{ wait.trigger.event.data.action == action_previous }}"
        sequence:
          - service: media_player.media_previous_track
            target:
              entity_id: media_player.denon_avr_ma
            data: {}
      - conditions: "{{ wait.trigger.event.data.action == action_play }}"
        sequence:
          - service: media_player.media_play_pause
            target:
              entity_id: media_player.denon_avr_ma
            data: {}
      - conditions: "{{ wait.trigger.event.data.action == action_next }}"
        sequence:
          - service: media_player.media_next_track
            target:
              entity_id: media_player.denon_avr_ma
            data: {}
mode: restart

Automation looks ok try checking companion app logs and HA core logs

Were you ever able to figure this out? I, too, am interested in a more convenient way to control MA via a notification or a widget. I was looking into perhaps a Template Widget, but this looks more promising, given the ephemeral nature. I’m happy to help test.

Sorry I missed this reply! I did get it to work - I can’t remember exactly what I did but it was just some small formatting error or such I think. Here is my current code which wouldn’t take much to adapt, just changing the relevant entities etc :nerd_face:

alias: Music Notifications
description: ""
trigger:
  - platform: state
    entity_id:
      - media_player.denon_avr_ma
    id: musicon
    to:
      - playing
      - paused
  - platform: state
    entity_id:
      - media_player.denon_avr_ma
    to: "off"
    id: musicoff
condition: []
action:
  - variables:
      action_previous: "{{ 'PREVIOUS_' ~ context.id }}"
      action_play: "{{ 'PLAY_' ~ context.id }}"
      action_next: "{{ 'NEXT_' ~ context.id }}"
  - choose:
      - conditions:
          - condition: trigger
            id:
              - musicon
        sequence:
          - service: notify.mobile_app_oneplus_open
            data:
              title: Music Assistant
              message: >-
                {{trigger.to_state.attributes.media_artist}} -
                {{trigger.to_state.attributes.media_title}}
              data:
                channel: Music Notifications
                clickAction: /d5369777_music_assistant_beta
                sticky: true
                persistent: true
                actions:
                  - action: "{{ action_previous }}"
                    title: Previous
                  - action: "{{ action_play }}"
                    title: Play/Pause
                  - action: "{{ action_next }}"
                    title: Next
                notification_icon: mdi:audio-video
                tag: musicon
      - conditions:
          - condition: trigger
            id:
              - musicoff
        sequence:
          - service: notify.mobile_app_oneplus_open
            data:
              message: clear_notification
              data:
                tag: musicon
  - wait_for_trigger:
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{ action_previous }}"
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{ action_play }}"
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{ action_next }}"
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ wait.trigger.event.data.action == action_previous }}"
        sequence:
          - service: media_player.media_previous_track
            target:
              entity_id: media_player.denon_avr_ma
            data: {}
      - conditions:
          - condition: template
            value_template: "{{ wait.trigger.event.data.action == action_play }}"
        sequence:
          - service: media_player.media_play_pause
            target:
              entity_id: media_player.denon_avr_ma
            data: {}
      - conditions:
          - condition: template
            value_template: "{{ wait.trigger.event.data.action == action_next }}"
        sequence:
          - service: media_player.media_next_track
            target:
              entity_id: media_player.denon_avr_ma
            data: {}
mode: restart