How to access url field in a mobile_app event?

Hello everyone!
I’m trying to build the following system - share a link from an Android phone to the HomeAssistant app, and have it played on a media player (currently hardcoded, but in the future it should be user selectable) controlled by HA.

I am able to listen to the event sent by the mobile_app:

{
    "event_type": "mobile_app.share",
    "data": {
        "caller": "android-app://com.vanced.android.youtube",
        "subject": "Watch \"Rick Astley - Never Gonna Give You Up (Official Music Video)\" on YouTube",
        "url": "https://youtu.be/dQw4w9WgXcQ",
        "device_id": "ee47bf5b74f70aca"
    },
    "origin": "REMOTE",
    "time_fired": "2022-03-02T07:02:48.930463+00:00",
    "context": {
        "id": "b985e0c7bb4bef18989a7cb74c44a9b1",
        "parent_id": null,
        "user_id": "1f269cc5e671416fb656d4f9e3ad1b0c"
    }
}

My automation looks like this:

- id: '1646138151853'                                                                                                                                                                                                                         
  alias: Play shared media from Android to a media player                                                                                                                                                                                     
  description: Share a link to Home Assistant and it gets played on a user-selected                                                                                                                                                           
    media player                                                                                                                                                                                                                              
  trigger:                                                                                                                                                                                                                                    
  - platform: event                                                                                                                                                                                                                           
    event_type: mobile_app.share                                                                                                                                                                                                              
    context:                                                                                                                                                                                                                                  
      user_id:                                                                                                                                                                                                                                
      - 1f269cc5e671416fb656d4f9e3ad1b0c                                                                                                                                                                                                      
      - 4db8fe846923499ea46b207d95af4802                                                                                                                                                                                                      
  condition: []                                                                                                                                                                                                                               
  action:                                                                                                                                                                                                                                     
  - service: script.play_shared_url_to_kids_room_mpd                                                                                                                                                                                          
    data: {}                                                                                                                                                                                                                                  
  mode: single   

Now, if my script looks like this (with the URL hardcoded), it works as expected (playback is triggered by my sharing of any link):

play_shared_url_to_kids_room_mpd:                                                                                                                                                                                                             
  alias: Play shared url to Kids room MPD (audio)                                                                                                                                                                                             
  sequence:                                                                                                                                                                                                                                   
  - service: media_extractor.play_media                                                                                                                                                                                                       
    target:                                                                                                                                                                                                                                   
      entity_id: "media_player.mpd_kids"                                                                                                                                                                                                      
    data:                                                                                                                                                                                                                                     
      media_content_type: MUSIC                                                                                                                                                                                                               
      media_content_id: 'https://youtu.be/dQw4w9WgXcQ'                                                                                                                                                                                   

The question is - what do I need to use in media_content_id so that I can access the event’s data.url in a template?
I haven’t found the relevant documentation about accessing a trigger’s data structure in an automation, so I’d like to learn.
There is a mention of trigger.event.data here: Automation Trigger Variables - Home Assistant, but it doesn’t seem to be working in this setup:

play_shared_url_to_kids_room_mpd:                                                                                                                                                                                                             
  alias: Play shared url to Kids room MPD (audio)                                                                                                                                                                                             
  sequence:                                                                                                                                                                                                                                   
  - service: media_extractor.play_media                                                                                                                                                                                                       
    target:                                                                                                                                                                                                                                   
      entity_id: "media_player.mpd_kids"                                                                                                                                                                                                      
    data:                                                                                                                                                                                                                                     
      media_content_type: MUSIC                                                                                                                                                                                                               
      media_content_id: '{{ trigger.event.data.url }}'

When I share a link with this setup, I get this error:

2022-03-02 09:19:27 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'trigger' is undefined when rendering '{{ trigger.event.data.url }}'
2022-03-02 09:19:27 ERROR (MainThread) [homeassistant.components.script.play_shared_url_to_kids_room_mpd] Play shared url to Kids room MPD (audio): Error executing script. Error for call_service at pos 1: Error rendering data template: UndefinedError: 'trigger' is undefined
2022-03-02 09:19:27 ERROR (MainThread) [homeassistant.components.automation.play_shared_media_from_android_to_a_media_player] Play shared media from Android to a media player: Error executing script. Error for call_service at pos 1: Error rendering data template: UndefinedError: 'trigger' is undefined
2022-03-02 09:19:27 ERROR (MainThread) [homeassistant.components.automation.play_shared_media_from_android_to_a_media_player] Error while executing automation automation.play_shared_media_from_android_to_a_media_player: Error rendering data template: UndefinedError: 'trigger' is undefined

So - how am I supposed to access the URL sent by the mobile_app in a script? Can somebody point me to some relevant documentation/examples? Is there something like setting a breakpoint in an automation to see what variables are passed on?
Thanks!

1 Like

Ok, I’ve advanced a bit with this task. It’s not yet pollished, but it works. Here’s what I did:

  1. I created a input_text entity to hold the URL:
input_text:
  url_for_playback:                                                                                                                                                                                                                           
    name: URL for playback                                                                                                                                                                                                                    
    initial: ""               
  1. Inside the automation, I save the URL from the trigger to input_text.url_for_playback:
- id: '1646138151853'                                                                                                                                                                                                                         
  alias: Play shared media from Android to a media player                                                                                                                                                                                     
  description: Share a link to Home Assistant and it gets played on a user-selected                                                                                                                                                           
    media player                                                                                                                                                                                                                              
  trigger:                                                                                                                                                                                                                                    
  - platform: event                                                                                                                                                                                                                           
    event_type: mobile_app.share                                                                                                                                                                                                              
    context:                                                                                                                                                                                                                                  
      user_id:                                                                                                                                                                                                                                
      - 1f269cc5e671416fb656d4f9e3ad1b0c                                                                                                                                                                                                      
      - 4db8fe846923499ea46b207d95af4802                                                                                                                                                                                                      
  condition: []                                                                                                                                                                                                                               
  action:                                                                                                                                                                                                                                     
  - variables:                                                                                                                                                                                                                                
      url: "{{ trigger.event.data.url }}"                                                                                                                                                                                                     
  - service: input_text.set_value                                                                                                                                                                                                             
    data:                                                                                                                                                                                                                                     
      entity_id: "input_text.url_for_playback"                                                                                                                                                                                                
      value: "{{ url }}"                                                                                                                                                                                                                      
  - service: script.play_shared_url_to_kids_room_mpd                                                                                                                                                                                          
    data: {}                                                                                                                                                                                                                                  
  mode: single                                                                                                                                                                                                                                
  1. I’ve changed the script so that it now reads the URL from the input_text entity:
play_shared_url_to_kids_room_mpd:                                                                                                                                                                                                             
  alias: Play shared url to Kids room MPD (audio)                                                                                                                                                                                             
  sequence:                                                                                                                                                                                                                                   
  - service: media_extractor.play_media                                                                                                                                                                                                       
    target:                                                                                                                                                                                                                                   
      entity_id: "media_player.mpd_kids"                                                                                                                                                                                                      
    data:                                                                                                                                                                                                                                     
      media_content_type: MUSIC                                                                                                                                                                                                               
      media_content_id: '{{states("input_text.url_for_playback")}}'  

And now I can share a URL and have it played by the media player. Yay!
The next step will be using persistent notifications to select on which media player I want to play the media!

1 Like