This is an automated archive made by the Lemmit Bot.
The original was posted on /r/homeassistant by /u/Initial-Influence-22 on 2025-03-13 23:14:49+00:00.
I moved to VoicePE since it was release and replaced it with my Alexa.
One feature, that I missed instantly, is the possibility to play any song from Spotify on any speaker using my voice.
Hence, I came up with a couple of scripts to do it on my own and I thought this is worth sharing with you. (Spoiler: It does not require Music Assistant)
The idea is to use a custom sentence in HA that goes like: "Play in ".
To do that, first of all a Spotify DEV account and a client ID + secret must be created.
ID and secret must be stored base64 converted in the secrets.yaml in HA.
It is important to encode it like that "ClientID:Secret".
Now, two REST commands must be created in the configuration.yaml in HA.
The first one is used to get the access token and the second one is used for the actual search.
rest_command:
access_spotify:
url: https://accounts.spotify.com/api/token
method: POST
headers:
authorization: !secret spotify_token
content_type: 'application/x-www-form-urlencoded'
payload: "grant_type=client_credentials"
find_spotify:
url: https://api.spotify.com/v1/search?q=%7B%7Bsearch_string%7D%7D&%3Btype=track&%3Blimit=1&%3Boffset=0
method: GET
headers:
authorization: "Bearer {{token}}"
content_type: 'application/json; charset=utf-8'
This can now be used in the custom sentence automation (German):
triggers:
- trigger: conversation
command: Spiele {search_track} [im|in|auf] {room}
alias: Spotify
id: Spotify
actions:
- alias: Access
action: rest_command.access_spotify
response_variable: access_result
data: {}
- alias: Search
action: rest_command.find_spotify
data:
token: "{{ access_result['content']['access_token'] }}"
search_string: "{{ trigger.slots.search_track }}"
response_variable: spotify_result
- alias: Play
if:
- condition: template
value_template: "{{ spotify_result['status'] == 200 }}"
then:
- alias: Parse data
variables:
url: spotify_result['content']['tracks']['items'][0]['external_urls']['spotify']
track: spotify_result['content']['tracks']['items'][0]['artists'][0]['name']
artist: spotify_result['content']['tracks']['items'][0]['artists'][0]['name']
player: >-
{% if trigger.slots.room == 'büro' %} media_player.buro
{% elif trigger.slots.room == 'wohnzimmer' %} media_player.wohnzimmer
{% elif trigger.slots.room == 'schlafzimmer' %} media_player.schlafzimmer
{% else %} error {% endif %}
error: "{{ spotify_result|length < 0 }}"
- alias: Check and Play
if:
- condition: or
conditions:
- condition: template
value_template: "{{ error }}"
- condition: template
value_template: "{{ player == 'error' }}"
then:
- set_conversation_response: Keine Titel oder Player gefunden
else:
- set_conversation_response: Spiele {{ track }} von {{ artist }} auf {{ trigger.slots.room }}
- action: media_player.play_media
metadata: {}
target:
entity_id: "{{ player }}"
data:
media_content_id: "{{ url }}"
media_content_type: music
Now go ahead and roast me for this complicated (but working!) setup 😊