UI Scripts - thlucas1/homeassistantcomponent_spotifyplus GitHub Wiki

The following scripts have been created for various user requests, so I thought I would post them here in case anyone else finds them useful.

Index

Script examples follow this line.

Play Playlist with Shuffle

Transfers control to the specified Spotify Connect device, enables shuffle mode, and starts playing the specified playlist.

Example Change the following script details for your environment:

  • YOUR_SPOTIFYPLUS_ENTITY - your SpotifyPlus entity_id.
  • Bose-ST10-1 - your Spotify Connect device name; or you can omit the device_id arguments to target the currently active device.
sequence:
  - action: spotifyplus.player_transfer_playback
    alias: Transfer Control to specified device
    data:
      entity_id: media_player.YOUR_SPOTIFYPLUS_ENTITY
      device_id: Bose-ST10-1
      play: false
  - action: spotifyplus.player_set_shuffle_mode
    alias: Enable Shuffle mode
    data:
      entity_id: media_player.YOUR_SPOTIFYPLUS_ENTITY
      device_id: Bose-ST10-1
      state: true
      delay: 2
  - action: spotifyplus.player_media_play_context
    alias: Start playing the specified Playlist
    data:
      entity_id: media_player.YOUR_SPOTIFYPLUS_ENTITY
      context_uri: spotify:playlist:5v5ETK9WFXAnGQ3MRubKuE
alias: SpotifyPlus Play Playlist with Shuffle
description: "Transfers control to the specified Spotify Connect device, enables shuffle mode, and starts playing the specified playlist."

Pause Podcast within 10 seconds of end

Pauses the SpotifyPlus media player when podcast episode estimated play time remaining is less than 10 seconds.

Example Change the following script details for your environment:

  • YOUR_SPOTIFYPLUS_ENTITY - your SpotifyPlus entity_id.
alias: SpotifyPlus Media Player Podcast Pause Player at End
description: >-
  Pauses the SpotifyPlus media player when podcast episode estimated play time
  remaining is less than 10 seconds.
triggers:
  - trigger: state
    alias: When SpotifyPlus estimated play time remaining changes
    entity_id:
      - media_player.YOUR_SPOTIFYPLUS_ENTITY
    attribute: sp_play_time_remaining_est
conditions:
  - alias: If SpotifyPlus is playing something
    condition: state
    entity_id: media_player.YOUR_SPOTIFYPLUS_ENTITY
    state: playing
    enabled: true
  - alias: If SpotifyPlus is playing podcast content
    condition: state
    entity_id: media_player.YOUR_SPOTIFYPLUS_ENTITY
    attribute: sp_item_type
    state: podcast
    enabled: true
  - alias: If SpotifyPlus is playing an episode
    condition: state
    entity_id: media_player.YOUR_SPOTIFYPLUS_ENTITY
    attribute: sp_playing_type
    state: episode
    enabled: true
  - alias: If less than 10 seconds remain of playing media content
    condition: template
    value_template: >-
      {% if ((state_attr('media_player.YOUR_SPOTIFYPLUS_ENTITY','sp_play_time_remaining_est') | int(0)) < 10) %}
      true 
      {% else %} 
      false 
      {% endif %}
    enabled: true
actions:
  - action: media_player.media_pause
    alias: Pause SpotifyPlus media player
    data:
      entity_id: media_player.YOUR_SPOTIFYPLUS_ENTITY
    enabled: true
mode: single

Check out the following Template Editor test to watch the sp_play_time_remaining_est attribute in action. Just copy / paste the following into the Developer Tools \ Template editor tool.

{% set mediaPlayer = 'media_player.YOUR_SPOTIFYPLUS_ENTITY' %}
{% set playDuration = (state_attr(mediaPlayer, 'media_duration') | int(0)) %}
{% set playPosition = (state_attr(mediaPlayer, 'media_position') | int(0)) %}
{% set playRemaining = (state_attr(mediaPlayer, 'sp_play_time_remaining_est') | int(0)) %}

Player State:         {{ states(mediaPlayer) }}
Media Duration Raw:   {{ state_attr(mediaPlayer, 'media_duration') }}
Media Position Raw:   {{ state_attr(mediaPlayer, 'media_position') }}
SP+ Remaining Raw:    {{ state_attr(mediaPlayer, 'sp_play_time_remaining_est')  }}

** HA Spotify attrs only update at scan interval (30 secs):
Media Duration:       {{ playDuration }}
Media Position:       {{ playPosition }}
Media Difference:     {{ playDuration - playPosition }}

** SP+ attr updates at scan interval (30 secs) initially, then every second on last scan interval:
SP+ Time Remaining:   {{ playRemaining }}

Within 90 seconds of end? {{ playRemaining <= 90 }}
Within 60 seconds of end? {{ playRemaining <= 60 }}
Within 30 seconds of end? {{ playRemaining <= 30 }}
Within 20 seconds of end? {{ playRemaining <= 20 }}
Within 10 seconds of end? {{ playRemaining <= 10 }}
Within 05 seconds of end? {{ playRemaining <= 5 }}
Within 04 seconds of end? {{ playRemaining <= 4 }}
Within 03 seconds of end? {{ playRemaining <= 3 }}
Within 02 seconds of end? {{ playRemaining <= 2 }}
Within 01 seconds of end? {{ playRemaining <= 1 }}
Within 00 seconds of end? {{ playRemaining <= 0 }}

Template: Less Than 10 Seconds? {{ ((state_attr('media_player.YOUR_SPOTIFYPLUS_ENTITY', 'sp_play_time_remaining_est') | int(0)) < 10) }}

Play First Context of Category Playlists

Gets the Spotify Jazz Category Playlists and plays the context returned from the first returned result.

Example
Change the following script details for your environment:

  • YOUR_SPOTIFYPLUS_ENTITY - your SpotifyPlus entity_id.
  • YOUR_DEVICE_NAME - your Spotify Connect device name.
alias: SpotifyPlus Play First Context of Category Playlists
description: >-
  Gets the Spotify Jazz Category Playlists and plays the context returned from
  the first returned result.
sequence:
  - service: spotifyplus.get_category_playlists
    data:
      entity_id: media_player.YOUR_SPOTIFYPLUS_ENTITY
      category_id: jazz
      limit: 1
    response_variable: search_result
  - service: spotifyplus.player_media_play_context
    data:
      entity_id: media_player.YOUR_SPOTIFYPLUS_ENTITY
      context_uri: "{{ search_result.result[\"items\"][0].uri }}"
      device_id: YOUR_DEVICE_NAME

Play Spotify Discover Weekly

Retrieves Spotify Discover Weekly Playlist ID from Made For You category list and starts play on the specified device id.

Example
Change the following script details for your environment:

  • YOUR_SPOTIFYPLUS_ENTITY - your SpotifyPlus entity_id.
  • YOUR_DEVICE_NAME - your Spotify Connect device name.
alias: SpotifyPlus Play Spotify Discover Weekly
description: "Retrieves Spotify Discover Weekly Playlist ID from Made For You category list and starts play on the specified device id."
sequence:
  - action: spotifyplus.get_category_playlists
    data:
      entity_id: media_player.YOUR_SPOTIFYPLUS_ENTITY
      category_id: 0JQ5DAt0tbjZptfcdMSKl3
      limit_total: 100
    response_variable: playlists_data
  - variables:
      discover_weekly_uri: >
        {{ playlists_data['result']['items'] | selectattr('name', 'eq', 'Discover Weekly') | map(attribute='uri') | first }}
  - action: spotifyplus.player_media_play_context
    data:
      entity_id: media_player.YOUR_SPOTIFYPLUS_ENTITY
      context_uri: "{{ discover_weekly_uri }}"
      device_id: YOUR_DEVICE_NAME
mode: single

Search Spotify Tracks and Play First Match

Searches Spotify for the given track, and plays the first result returned in the list.

Example
Change the following script details for your environment:

  • YOUR_SPOTIFYPLUS_ENTITY - your SpotifyPlus entity_id.
  • YOUR_DEVICE_NAME - your Spotify Connect device name.
alias: SpotifyPlus Search Spotify Tracks and Play First Match
description: >-
  Searches Spotify for the given track, and plays the first result returned in
  the list.
sequence:
  - action: spotifyplus.search_tracks
    data:
      entity_id: media_player.YOUR_SPOTIFYPLUS_ENTITY
      criteria: "journey don't stop believing"
      limit_total: 5
    response_variable: search_result
  - action: spotifyplus.player_media_play_tracks
    data:
      entity_id: media_player.YOUR_SPOTIFYPLUS_ENTITY
      uris: "{{ search_result.result[\"items\"][0].uri }}"
      device_id: YOUR_DEVICE_NAME

Philips Hue Lights to Currently Playing Vibrant Color

Changes Philips Hue lighting color to vibrant color of currently playing Spotify track cover art.

Example Change the following script details for your environment:

  • YOUR_SPOTIFYPLUS_ENTITY - your SpotifyPlus entity_id.
  • YOUR_HUE_LIGHT_ENTITY - your Philips Hue light entity_id.
alias: SpotifyPlus Hue Lights to Currently Playing Spotify Cover Art Vibrant Color
description: >-
  Changes Philips Hue lighting color to vibrant color of currently playing
  Spotify track cover art.
triggers:
  - trigger: state
    alias: Trigger when SpotifyPlus media player Track ID changes
    entity_id:
      - media_player.YOUR_SPOTIFYPLUS_ENTITY
    attribute: media_content_id
conditions:
  - condition: state
    alias: If SpotifyPlus media player is playing something
    entity_id: media_player.YOUR_SPOTIFYPLUS_ENTITY
    state: playing
actions:
  - action: spotifyplus.get_image_vibrant_colors
    alias: Get vibrant colors from currently playing Spotify cover art image
    data:
      entity_id: media_player.YOUR_SPOTIFYPLUS_ENTITY
      color_count: 64
      color_quality: 5
    response_variable: vibe_colors
  - action: light.turn_on
    alias: Change Hue light color to vibrant color value
    data:
      entity_id: light.YOUR_HUE_LIGHT_ENTITY
      brightness_pct: 100
      rgb_color: "{{ vibe_colors.result.vibrant.rgb }}"
mode: single