One of my favorite Home Assistant use cases is to control as much as possible using voice assistants. I enjoy the freedom of just shouting Alexa and asking her to do things and Home Assistant allows you to do so much more than what is available just from the Amazon Alexa smart home features.

I sometimes enjoy playing podcasts or an audiobook at bedtime and it became very tedious to open my phone and get something playing on the Chromecast Audio that lives in my bedroom. There's no doubt that this seems like a first-world issue, but too many times my phone's display has blinded my eyes or I wasn't able to get the phone lined back up on the wireless charger correctly, etc.

Enter Home Assistant

Like everything else, Home Assistant usually has a way to solve these problems.

I use Plex to house all of my media, this includes everything from movies, TV shows, music, podcasts, audiobooks, etc. and I have a few playlists set up there which are useful to throw on and set the sleep timer for bedtime.

Here's an example of how you can have Home Assistant start playing one of these playlists from Plex Media Server directly to a supported media player like a Chromecast device.  You can also see the Google Cast Integration for more information here.

 script:
  playplexplaylist:
    alias: "Play Plex Playlist for 2hr"
    sequence:
      - service: media_player.play_media
        entity_id: media_player.master_bedroom_speaker
        data:
          media_content_type: PLAYLIST
          media_content_id: 'plex://{ "playlist_name": "Bedtime Podcasts", "shuffle": "1" }'
      - delay: '02:00:00'
      - service: media_player.media_stop
        data:
          entity_id: media_player.master_bedroom_speaker

The script calls the `media_player` service called `play_media` and points the source directly to Plex with some JSON to identify the playlist. What's nice about this is that Home Assistant is just directing the media player to reach out to Plex directly for the content. Its role is over after this script executes.

For me, I put in a delay of 2 hours where I tell the Chromecast audio to stop playing.  if I'm still awake, I'll ask Alexa to run the script again.

Since Home Assistant isn't performing the playback, there's no way to stop the playback with Alexa if I needed to between that two-hour delay. So create another script to trigger the media_player.media_stop service.

script:
  stopplexplaylist:
    alias: "Stop Playback on Bedroom Chromecast Audio"
    sequence:
      - service: media_player.media_stop
        entity_id: media_player.master_bedroom_speaker

The nice thing about this script is that it's not tied to Plex it just commands the Chromecast audio to stop playing whatever it might be playing.

Setting up the Alexa Intents

Add some intents to your Alexa configuration for these new scripts. I like to give Alexa some personality so I have multiple responses available and I let Home Assistant pick one at random:

intent_script:
  PlexPlayliststart:
    action:
      service: homeassistant.turn_on
      entity_id: script.playplexplaylist
    speech:
      type: plain
      text:          >
          {{ [
          "Starting your playlist, sleep well! See you in the morning",
          "You got it, your playlist is coming up!",
          "Done!"
          ] | random }}


  PlexPlaylistStop:
    action:
      service: homeassistant.turn_on
      entity_id: script.stopplexplaylist
    speech:
      type: plain
      text: Done!

Next, follow the directions for the Alexa Skill Integration to log into the Alexa Developer Console and add your new intents and utterances. Build your skill and test it out!

Check out my other posts on Home Automation and other Smart Home technologies here!