Select a random playlist as an action

I’m using Home Assistant to automate some physical buttons for my Squeezebox media player. I’ve already managed to get this to do volume up/down, pause/play, and skip track. What I want to do now is add an integration for playing a random playlist. Right now, the playlist should be selected from a hard-coded list, but I might later want to make this more sophisticated.

Here is the configuration I’ve written so far:

- id: 'random_playlist'
  alias: Random Playlist
  description: ''
  trigger:
  - platform: device
    domain: mqtt
    device_id: 784b3416e4c5591014c6dcd037215b17
    type: action
    subtype: top_left_hold
    discovery_id: 0x000d6f000fe48b8e action_top_left_hold
  condition: []
  action:
  - service: squeezebox.call_method
    data:
      command: playlist
      parameters:
      - play
      - >-
        {% set chosen = [
          'spotify:playlist:37i9dQZF1DXa8axQnFlj0R', # sweater weather
          'spotify:playlist:37i9dQZF1DX91oIci4su1D', # trance mission
          'spotify:playlist:37i9dQZF1DWSQScAbo5nGF', # indie road trip
          'spotify:playlist:37i9dQZEVXcK41NOc1uUJ9', # discover weekly
          'spotify:playlist:37i9dQZF1DX76Wlfdnj7AP'  # beast mode
        ] | random %}
        {{ chosen }}
    target:
      entity_id: media_player.cube

But right now this gives this error:

2023-12-18 19:50:54.285 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: template value should be a string for dictionary value @ data['action'][0]['data']. Got None. (See /config/configuration.yaml, line 2). 

I also tried this without using the set step first, and using > instead of >-:

    parameters:
      - play
      - >
        {{ [
          'spotify:playlist:37i9dQZF1DXa8axQnFlj0R', # sweater weather
          'spotify:playlist:37i9dQZF1DX91oIci4su1D', # trance mission
          'spotify:playlist:37i9dQZF1DWSQScAbo5nGF', # indie road trip
          'spotify:playlist:37i9dQZEVXcK41NOc1uUJ9', # discover weekly
          'spotify:playlist:37i9dQZF1DX76Wlfdnj7AP'  # beast mode
        ] | random }}

But it gives the same error.

I think it must be to do with the way that I’m trying to use the jinja template to get an item and put it into the yaml list with that >- operator. I can’t seem to find any documentation on > and >- and how they work with jinja/yaml/HA in this context. Can anybody help me with this?

Thanks,

Peter

I think that error means it’s looking for your template to be output as a string, and it’s not at the moment. Should be able to add " | string" after chosen to ensure the data type is a string.

I think it should be a string - it’s a random choice from a list of strings. If I do the same thing in a Python session using jinja it seems to behave as expected:

>>> jinja2.Template('hello {{ [ "one", "two", "three" ] | random }}').render()
'hello three'
>>> jinja2.Template('hello {{ [ "one", "two", "three" ] | random }}').render()
'hello two'
>>> jinja2.Template('hello {{ [ "one", "two", "three" ] | random }}').render()
'hello three'
>>> jinja2.Template('hello {{ [ "one", "two", "three" ] | random }}').render()
'hello two'

This is why I think it’s something about how the jinja template is allowing the string it returns to be used as part of a yaml list in the parameters entry.

I should say I also tried your suggestion:

      parameters:
      - play
      - >-
        {% set chosen = [
          'spotify:playlist:37i9dQZF1DXa8axQnFlj0R', # sweater weather
          'spotify:playlist:37i9dQZF1DX91oIci4su1D', # trance mission
          'spotify:playlist:37i9dQZF1DWSQScAbo5nGF', # indie road trip
          'spotify:playlist:37i9dQZEVXcK41NOc1uUJ9', # discover weekly
          'spotify:playlist:37i9dQZF1DX76Wlfdnj7AP'  # beast mode
        ] | random %}
        {{ chosen | string }}

And I get the same error:

2023-12-19 12:05:00.012 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: template value should be a string for dictionary value @ data['action'][0]['data']. Got None. (See /config/configuration.yaml, line 2).

You put YAML comments inside a Jinja template. Remove them.

:man_facepalming: Great catch - thank you!

If you want to identify each playlist with a title, consider defining a dictionary instead of a list.