Squeezebox & Youtube API: David Lynch Weather Report Alarm Clock

I know this is ridiculously specific, but I guess you could say, when it comes to Squeezebox… those who like it, like it a lot. You could say the same about David Lynch. That being said, I think this example could be emulated to search for and play YouTube videos, to Squeezebox and probably other media players, on a schedule or other trigger, fairly easily. IMHO, his would be a lofty project for a newb, but, hey, if your’e feeling brave…

Read 'Em and Weep

  • Squeezebox. Also, Logitech Media Server. But, Squeezebox.
  • Big Hitch: You’re going to need to have a YouTube API Key and know what that is. I’m not going to go into that. If you’re a Squeezebox person, you’re going to learn how to get one by installing and setting up the YouTube plugin for Logitech Media Server anyway. Still with me?
  • Got *nix? I’m also going to use curl, a *nix command line tool to access the YouTube API, find the latest video in a YouTube Channel and store the URL and stuff in a sensor. That command will run in the Home Assistant bash shell, so some familiarity with the *nix command line will help to understand this solution.
  • Some Home Assistant platforms don’t have a built-in bash shell, so you’ll need to set that up. Is that true? I think I’m using HASSOS, on VMware? Anyway, if you don’t have a bash shell on your Home Assistant, get one.
  • The template I use for extracting the data I need from the YouTube search is a little tricky too. It loops through results from the YouTube API to find the video I’m looking for.

If you’ve got that stuff covered, you’re laughin’! LOL Let’s dig in…

Definitions

LMS: Logitech Media Server. Also Squeezebox.

Variables

YOUR-YOUTUBE-CHANNEL-ID-HERE: UCDLD_zxiuyh1IMasq9nbjr (David Lynch Theatre)
YOUR-YOUTUBE-API-KEY-HERE: Get your own.

YouTube API Call Sensor

Here’s how I use curl to make a call to the YouTube API to get a list of the most recent 5 videos for a specific Channel. You’re going to need to substitute your YouTube API key for [YOUR-YOUTUBE-API-KEY-HERE] and the Channel ID for [YOUR-YOUTUBE-CHANNEL-ID-HERE]. you might want to run the curl command in your actual bash shell (the Home Assistant bash shell)to test, and to see the results, which will help you understand the value_template here. Notice how I’m capturing the curl command exit code into a variable (ECODE) and returning the error code instead of the value if there’s a problem, i.e. if the exit code is not zero?

  - platform: command_line
    name: DLT Recent Videos
    unique_id: "DLT Recent Videos"
    command: RESPONSE=$( curl -s -A "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.87 Safari/537.36" "https://www.googleapis.com/youtube/v3/search?key=[YOUR-YOUTUBE-API-KEY-HERE]&channelId=[YOUR-YOUTUBE-CHANNEL-ID-HERE]&part=snippet,id&order=date&maxResults=5" ); ECODE=$?; test $ECODE -gt 0 && echo "ERROR $ECODE" || echo $RESPONSE
    json_attributes:
      - "items"
    scan_interval: 10800
    value_template: "report" 
    command_timeout: 15
  - platform: template
    sensors:
      dlt_report_for_today:
        friendly_name: "DLT Todays Weather Report"
        value_template: >-
          {% for i in state_attr('sensor.dlt_recent_videos', 'items') %}
            {% set x = now().year | string %}
            {% if ( "Weather Report " + now().month | string + "/" + now().day | string + "/" + x[-2:] ) in i.snippet.title %}
              youtube://www.youtube.com/v/{{ i.id.videoId }}
            {% endif %}
          {% endfor %}

So there’s that.

LMS YouTube Alarm

The other half of the component is pretty simple, but this is the part that is specific to LMS. This Alarm plays the latest YouTube instalment whenever the value in the sensor we created changes. You could just as easily play it at a certain time of day, or whatever. That part is also pretty clumsy because it just vetos whatever else was splaying, plays the audio from the YouTube result, and does not resume what was playing before. I guess that’s phase two of the project. I’m not there yet. I searched around, and there are other solutions for LMS that remember what was playing, play something else, and then resume what was playing before. So, it’s out there. I think the interesting part here is how the parameters are passed to the LMS to make the YouTube plugin play the audio in question.

alias: David Lynch New Weather Report
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.dlt_report_for_today
    for:
      hours: 0
      minutes: 5
      seconds: 0
condition: []
action:
  - service: squeezebox.call_method
    data:
      command: playlist
      parameters:
        - play
        - "{{ states('sensor.dlt_report_for_today') }}"
        - DLT
        - 2
    target:
      entity_id:
        - media_player.boom
        - media_player.radio
mode: single

Okay, that’s enough typing for now. I know there’s going to be questions, but I don’t know how to save this post as a draft - is that possible? - so I’ll just post it and be back to build it out a bit… promise!

I learned lots and had fun putting this together - I hope you do too!

3 Likes