Similar to this, I created a rest sensor and template to fetch a YouTube video url when an artist and song name input is provided. When the input_text var is changed, it will trigger an automation that will trigger a refresh of the REST sensor. Which will capture the first result matching the artist name and song provided. It will fetch the youtube title, videoId and thumbnail url. With this a direct youtube url can be build which can then be used to stream/cast.
- Create YouTube API key
- Store API credentials in
secret.yaml:
youtube_api_key: XXXXXXXXXXXXXXXXXXXXXXXXXXX
- Link hidden password
input_textto secret inconfiguration.yaml:
input_text:
youtube_api_key:
initial: !secret youtube_api_key
mode: password
homeassistant:
...
customize:
input_text.youtube_api_key:
hidden: true
- Enable YouTube API v3
- Restrict API key to only query YouTube API
- Add REST sensor to query YouTube API, set high scan interval so REST call will only be executed when a refresh would be triggered.
configuration.yaml(or can be split intosensor.yaml):
sensor:
- platform: rest
name: Get YouTube first search result
scan_interval: 99999999
resource_template: "https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&type=video&&format=json&q={{states('input_text.search_mp3_youtube')}}&key={{states('input_text.youtube_api_key')}}"
value_template: "{{value[:250]}}"
json_attributes:
- items
- platform: template
sensors:
youtube_search_song_video_url:
value_template: "https://www.youtube.com/watch?v={{state_attr('sensor.get_youtube_first_search_result','items')[0].id.videoId }}"
- platform: template
sensors:
youtube_search_song_video_title:
value_template: "{{state_attr('sensor.get_youtube_first_search_result','items')[0].snippet.title }}"
- platform: template
sensors:
youtube_search_song_video_thumbnail:
value_template: "{{state_attr('sensor.get_youtube_first_search_result','items')[0].snippet.thumbnails.high.url }}"
- Create automation to trigger REST sensor refresh when
input_textto search is changed
- id: 'XXXXXXX'
alias: Search YouTube URL
description: ''
trigger:
- platform: state
entity_id: input_text.search_mp3_youtube
to:
condition: []
action:
- service: homeassistant.update_entity
target:
entity_id: sensor.get_youtube_first_search_result
mode: single
- Create script to cast first found YouTube result based on search input text. With a short delay to make sure the rest sensor is updated after input has changed. In
scripts.yaml:
play_youtube_search_selection:
alias: Play YouTube search
sequence:
- delay:
hours: 0
minutes: 0
seconds: 3
milliseconds: 0
- service: media_extractor.play_media
data_template:
entity_id: media_player.chromecast
media_content_id: '{{ states(''sensor.youtube_search_song_video_url'') }}'
media_content_type: video/youtube
mode: single
icon: mdi:youtube
- Add input text and button to trigger script into lovelace. Thumbnail and title could also be added based on sensors above:
- entity: input_text.search_mp3_youtube
type: custom:text-input-row
- type: button
tap_action:
action: toggle
entity: script.play_youtube_search_selection