Get alerted when someone subscribes to your Youtube channel or likes a video

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.

  1. Create YouTube API key
  2. Store API credentials in secret.yaml:
youtube_api_key: XXXXXXXXXXXXXXXXXXXXXXXXXXX
  1. Link hidden password input_text to secret in configuration.yaml:
input_text:
    youtube_api_key:
      initial: !secret youtube_api_key
      mode: password
homeassistant:
  ...
  customize:
    input_text.youtube_api_key:
      hidden: true
  1. Enable YouTube API v3
  2. Restrict API key to only query YouTube API
  3. 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 into sensor.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 }}"
  1. Create automation to trigger REST sensor refresh when input_text to 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
  1. 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
  1. 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