Spotify Playlist template

Having a sensor who has as attributes the following items:

playlists: 
- uri: spotify:playlist:6R2R0VEqvpLdbUgOPMMY4I
  name: Personale
- uri: spotify:playlist:4Wc3emFezHEeBJFFLz5R2m
  name: Discover Weekly Archive
- uri: spotify:playlist:37i9dQZF1E36buj8U8F3MT
  name: Daily Mix 1
- uri: spotify:playlist:37i9dQZF1E4jWiRDHZQfH1
  name: John Mayall Radio
- uri: spotify:playlist:0vq3VuMsNlTnDkdmta8kk2
  name: 'Anthony Phillips: The Complete Discography'
- uri: spotify:playlist:37i9dQZF1E8A8A10sOSftn
  name: Evil and Here to Stay Radio
- uri: spotify:playlist:4gO3G8XVKyj4t4FNfcFFMo
  name: Rain And Tears
- uri: spotify:playlist:37i9dQZF1DX9loJQLuEvap
  name: Great Female Voices
- uri: spotify:playlist:37i9dQZF1DWULEW2RfoSCi
  name: 70s Soul Classics
- uri: spotify:playlist:37i9dQZF1DWWvhKV4FBciw
  name: Funk & Soul Classics

last_update: 2022-05-12T12:20:23.294710+02:00
friendly_name: Playlists sensor

How to create a template who randomly choose one of the playlists listed?

I tried this:

'{{ state_attr("sensor.playlist_sensor", "items") | list | random }}'

But i get this error:

TypeError: 'NoneType' object is not iterable

Sorry, maybe the answer is really simple, but i still have many difficulties creating templatesā€¦

Youā€™re trying to access the attribute with the name items, but thereā€™s no attribute with that name.
Here are the templating docs in case you didnā€™t stumble across them yet.
Something like this should work:

{{ state_attr("sensor.playlist_sensor", "playlists") | random }}

If you just want the uri:

{{ (state_attr("sensor.playlist_sensor", "playlists") | random)['uri'] }}

Thanks for the precious infos, but using that template i get:

TypeError: object of type 'NoneType' has no len()

Alright, does

{{ state_attr("sensor.playlist_sensor", "playlists") | random }}

work at least?
If not, whatā€™s the result of

{{ state_attr("sensor.playlist_sensor", "playlists") }}

In the template editor?

In the first case the result is the same, in the second i get ā€˜Noneā€™

Are you sure the entity_id is correct?
Since your friendly name is ā€œPlaylists sensorā€ Iā€™d assume the sensors id is sensor.playlists_sensor instead of sensor.playlist_sensor
So maybe try again with

{{ state_attr("sensor.playlists_sensor", "playlists") | random }}

or

{{ (state_attr("sensor.playlists_sensor", "playlists") | random)['uri'] }}

depending on what data you need.

1 Like

Yes, youā€™re right, my faultā€¦ It confirm my still poor knowledgeā€¦
The second template is what i need, thanksā€¦

Using this template i get in the developers/tool models

uri: spotify:playlist:6R2R0VEqvpLdbUgOPMMY4I

How to toggle the ā€˜uriā€™ voice? If i use this in a script i get

uri: uri: spotify:playlist:6R2R0VEqvpLdbUgOPMMY4I

Even in the template editor you shouldnā€™t get uri: ... in the output, somethingā€™s wrongā€¦
Are you sure youā€™re using the one with the [ā€˜uriā€™] part?
Maybe try double quotes?

{{ (state_attr("sensor.playlists_sensor", "playlists") | random)["uri"] }}

Ok, thanks again, in this way all is working goodā€¦[quote=ā€œmaurizio53, post:10, topic:421053, full:trueā€]

Ok, thanks again, in this way all is working goodā€¦

1 Like

Have been looking for something like this. Could you please post the config for the sensor definition? Thanks :smiley:

1 Like