Using base_url in templates?

Am I missing something here? I’m trying to play a local audio file on a chromecast device and apparently local files are not supported, which is a bummer. I guess what is supported (only after blocking DNS requests to Google’s name servers), is supplying the local IP/hostname of my HA instance (127.0.0.1/localhost doesn’t work?):

      - service: media_player.play_media
        data:
          entity_id: media_player.some_speaker
          media_content_id: http://xxx.xxx.xxx.xxx:8123/local/sounds/some_file.mp3
          media_content_type: music

My question, is since I’m already supplying base_url in configuration.yaml (via secrets.yaml), is there a way I can use the value of base_url in my automation?

I mean, I define the hostname/ip:port in secrets, use !secrets base_url in configuration.yaml, then I have to hard code the xxx.xxx.xxx.xxx:8123 IP:port again in my automation because I am unable to use defined secrets in templates? That seems redundant I guess. It would be awesome to just simply use something like the following instead:

      - service: media_player.play_media
        data_template:
          entity_id: media_player.some_speaker
          media_content_id: http://{{ base_url }}//local/sounds/some_file.mp3
          media_content_type: music
1 Like

I suppose a REST sensor would work, it would still be nice to have without creating another sensor though:

sensor:
  - platform: rest
    resource: http://localhost:8123/api/discovery_info
    name: base_url
    value_template: '{{ value_json.base_url }}'

script:
  some_script:
    sequence:
      - service: media_player.play_media
        data_template:
          entity_id: media_player.some_speaker
          media_content_id: "{{ states('sensor.base_url') }}/local/sounds/some_file.mp3"
          media_content_type: music
1 Like