Logitech Squeezebox - examples of how to use the API in HA

I only recently discovered Squeezebox/Logitech Media Server, and have built a piCoreplayer/server with a Raspberry Pi (and have two PCs using Squeezelite-X for players). Fun and highly recommended! I’ve been playing with how to integrate them into HA today, and figured out how to create some automations after a lot of trial and error, so I thought I’d share. The documentation about the API that can be found at http://HOST:PORT/html/docs/cli-api.html?player= is a bit obtuse, but now that I have a start I can continue on from here.

I’m using my Magic Cube as a trigger in the following automations because it was an easy way to test what I was doing, but you can obviously use whatever you want. I’m going to get some NFC tags to play with soon.

Example automation that plays a local playlist called “My Local Playlist”:

alias: SQ Play Local Playlist
trigger:
  platform: event
  event_type: deconz_event
  event_data:
    id: mi_magic_cube
    event: 2000 # slide side 2 up
action:
  - service: squeezebox.call_method
    data:
      entity_id: media_player.picoreplayer
      command: 'playlist'
      parameters:
        - play
        - 'My Local Playlist'

Automation to pause the player (this acts like a toggle-if I pause the player, I can slide the cube another time to start it playing again):

alias: SQ pause toggle
trigger:
  platform: event
  event_type: deconz_event
  event_data:
    id: mi_magic_cube
    event: 6000 # slide side 6 up
action:
- service: squeezebox.call_method
  data:
    entity_id: media_player.picoreplayer
    command: 'pause'

This service call lists the first 20 playlists and shows the extid tag, which is needed for Spotify (or other external) playlists. You can see the info in the media player’s attribute query.

service: squeezebox.call_query
data:
  entity_id: media_player.picoreplayer
  command: playlists
  parameters:
    - '0'
    - '20'
    - 'tags:E'

One entry extracted from the query attribute looks like this (this one is fake):

- id: 29753 playlist: 'Spotify: My Great Playlist' extid: 'spotify:playlist:48wQfF1DX9wR2KY45plZ' 

Example automation that plays a Spotify playlist, whose big long id I got from the service call above:

alias: SQ Play My Spotify Playlist
trigger:
  platform: event
  event_type: deconz_event
  event_data:
    id: mi_magic_cube
    event: 4000 # slide side 4 up
action:
- service: squeezebox.call_method
  data:
    entity_id: media_player.picoreplayer
    command: 'playlist'
    parameters:
      - play
      - 'spotify:playlist:48wQfF1DX9wR2KY45plZ'

This is just the tip of the iceberg, and may seem simplistic, but it wasn’t obvious to me at first how to even get started. I hope it gives someone else a bit of a start on how to use the API with HA. :slightly_smiling_face:

12 Likes

Thanks for sharing… been very helpful. If you have any other examples, would love to see them.

Taking inspiration from your post, I’ve created a simple script to turn on my kitchen Squeezebox Radio, set the volume to 24 (in case someone had it previously up high) and play a radio station via TuneIn. Still playing around with it:

Script excerpt:

alias: Squeezebox - Play 2FM Radio @ Volume 24)
sequence:
  - service: squeezebox.call_method
    data:
      entity_id: media_player.squeezelite_x
      command: mixer
      parameters:
        - volume
        - 24
  - service: squeezebox.call_method
    data:
      entity_id: media_player.squeezelite_x
      command: playlist
      parameters:
        - play
        - >-
          http://opml.radiotime.com/Tune.ashx?id=s15065&formats=aac,ogg,mp3&partnerId=16&serial=79232d52020eef518cef71462c5571c1
mode: single

I’ve also got it working to play Spotify Playlists (similar to your initial post). Next thing I want to try and figure out something more advanced, like playing recently added albums or random mix from the current year…hmmm…

4 Likes

That’s great, thanks for posting this! I haven’t had much time lately to do more with it, but will post if I come up with anything interesting.

1 Like

Thanks for your great posting. This was exactly what is was searching for.
Your Example with the Spotify playlist works fine but i want to shuffle the playlist everytime the automation is running.
I tried to figure it out how i can solves this. But until yet, i don’t get it. Do you have any hint for me?

Thank you for the inspiration.

Although I was able to start radio stations and Last Mix Playlists from my LMS favorites this way, I was not able to trigger a Tidal Playlist saved as a favorite in LMS.
I used “click the three dots” “Edit” “URL” for all three.

Does anybody know how to trigger Tidal Playlists?

Figured it out myself. One must not view LMS in Material but normal mode. In that mode the play button of a e.g. Tidal Playlist will show a link when you hover you mouse over it. Copy that link and use it, that worked for me.

1 Like

I’m trying to achieve the same. It was working great with spotify calling “spotify:album/playlist:12345” but with tidal I can’t get this working. Under “more” i found the external ID “wimp:album:12345” but calling this via the CLI didn’t work. (the ID here is the same as you can find in the URL when you share a tidal album)
I tried what you did and switched to the classic view but I can’t see the link when hovering my mouse of the play button.
Are you inside you library or inside the tidal app integrated into LMS?

EDIT: Found out its just the full URL to the Tidal Album/Playlist/Track. So sending https://tidal.com/browse/album/66084578 will play the album.

2 Likes

Hi everyone,
how can i replace media_content_id with the following.
service: squeezebox.call_method

I’ve tried it, but it doesn’t work quite right.
Maybe you can help me.
Thank you.

service: squeezebox.call_method
data_template:
  entity_id: media_player.{{states.input_select.chromecast_radio_speakers.state}}
  command: favorites
  parameters: >
    {% if is_state('input_select.chromecast_radio_station', 'ProFM') %}
      ["playlist", "play", "item_id:0"]
    {% elif is_state('input_select.chromecast_radio_station', 'KissFM') %}
      ["playlist", "play", "item_id:1"]
    {% elif is_state('input_select.chromecast_radio_station', 'EuropaFM') %}
      ["playlist", "play", "item_id:2"]
    {% elif is_state('input_select.chromecast_radio_station', 'RadioZu') %}
      ["playlist", "play", "item_id:3"]
    {% elif is_state('input_select.chromecast_radio_station', 'DigiFM') %}
      ["playlist", "play", "item_id:4"]
    {% elif is_state('input_select.chromecast_radio_station', 'MagicFM') %}
      ["playlist", "play", "item_id:5"]
    {% elif is_state('input_select.chromecast_radio_station', 'VirginRadio') %}
      ["playlist", "play", "item_id:6"]
    {% elif is_state('input_select.chromecast_radio_station', 'Popular') %}
      ["playlist", "play", "item_id:7"]
    {% elif is_state('input_select.chromecast_radio_station', 'WefunkRadio') %}
      ["playlist", "play", "item_id:8"]
    {% elif is_state('input_select.chromecast_radio_station', 'FunkRadio') %}
      ["playlist", "play", "item_id:9"]
    {% endif %}

Try this

entity_id: media_player.{{ states('input_select.chromecast_radio_station') }}
2 Likes

Hi guys.
Actually I try to map some functions of my squeezeboxes to zigee remotes. Works good so far, but i have one annoying problem.
I dont understand how to use the button command for volume_up / volume_down, to increase or decrease the volume.
Can someone give me an example of this function?


service: squeezebox.call_method
target:
  entity_id: media_player.{{states.input_select.lms_player.state}}
data:
  command: mixer
  parameters:
    - volume
    - "{{states.input_number.lms_volume.state}}"

3 Likes

Thanks a lot. Was a good Idea, solved my problem.

For those interested, this is how I call the random mix

service: squeezebox.call_query
data:
  command: randomplay
  parameters:
    - tracks
target:
  entity_id: media_player.name
2 Likes

Spent a while today trying to figure out how to play a specific album…took a bit of testing to figure out how to specify the parameters. The documentation for the CLI under “Playlist Commands and Queries” says

<playerid> playlist loadalbum <genre> <artist> <album>

The "playlist loadalbum" command puts songs matching the specified genre artist and album criteria on the playlist. Songs previously in the playlist are discarded.

Examples:

Request: "04:20:00:12:23:45 playlist loadalbum Rock Abba *<LF>"
Response: "04:20:00:12:23:45 playlist loadalbum Rock Abba *<LF>"

This is the format of the automation action that works for me:

action:
  - service: squeezebox.call_method
    data:
      entity_id: media_player.picoreplayer
      command: playlist
      parameters:
        - loadalbum
        - Rock # genre
        - Eagles # artist
        - 'The Very Best Of' # album
  - service: squeezebox.call_method
    data:
      entity_id: media_player.picoreplayer
      command: playlist
      parameters:
        - play
mode: single

The loadalbum command clears the queue and adds the album specified; there is also an addalbum command to append to the bottom of the queue without changing what is already there.

Hope that’s helpful to someone :slight_smile:

2 Likes

Hi everyone,

How can I query the status of the player?
Better said, I want to know which players are currently running.

1 Like

@Anwen Thank you so much for your examples. I finally managed to start a Spotify-playlist with the Spotty-plugin in LMS from Home Assistant. For me the media_content_id changed daily when I used media_player.play_media.

1 Like

Thanks for posting, the information has been very useful in terms of automating some of my audio.

Just wondering if anyone knows how to play a podcast episode via cli. I tried with a link but it plays one particular podcast episode where I want to play the latest episode only.

Thanks again for the info provided.

Here is an example: (it’s a dutch podcast)

 - platform: command_line
   name: Latest vroege vogels
   scan_interval: 7200
   command: curl -k -s https://podcast.npo.nl/feed/vroegevogels.xml | grep -E -o 'https?://[^"]+mp3' | uniq | head -n 1

This sensor gives me the url to the latest (weekly) podcast.
You can than make an automation to play it when you press an input button or whatever trigger you want.

- id: '1655028732393'
  alias: vroege vogels auto
  description: ''
  trigger:
  - platform: state
    entity_id:
    - input_button.vroege_vogels
  condition: []
  action:
  - service: media_player.play_media
    data:
      entity_id: media_player.radio_keuken
      media_content_id: "{{ states('sensor.laatste_vroege_vogels') }}"
      media_content_type: music

I have combined examples from others to what works for me. Perhaps there is still room for improvement and I would like to hear about it.
Hope it’s usefull to you.

1 Like

Wow, thanks man. There is no way I would have figured that out. I’ll give that a try for sure. Thanks so much for that.

Thanks, but unfortunately I cannot locate the podcast on an RSS / XML page or on a webpage with a link anywhere. I can see it in apple podcasts, within LMS and on a webpage for the latest episode, but nothing I can ‘scrape’ to get the link.