maurizio53
(Maurizio Fabiani)
May 12, 2022, 10:24am
1
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ā¦
fedot
May 12, 2022, 12:02pm
2
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'] }}
maurizio53
(Maurizio Fabiani)
May 12, 2022, 12:07pm
3
Thanks for the precious infos, but using that template i get:
TypeError: object of type 'NoneType' has no len()
fedot
May 12, 2022, 12:12pm
4
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?
maurizio53
(Maurizio Fabiani)
May 12, 2022, 12:14pm
5
In the first case the result is the same, in the second i get āNoneā
fedot
May 12, 2022, 12:19pm
6
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
maurizio53
(Maurizio Fabiani)
May 12, 2022, 12:21pm
7
Yes, youāre right, my faultā¦ It confirm my still poor knowledgeā¦
The second template is what i need, thanksā¦
maurizio53
(Maurizio Fabiani)
May 12, 2022, 12:53pm
8
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
fedot
May 12, 2022, 1:00pm
9
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"] }}
maurizio53
(Maurizio Fabiani)
May 12, 2022, 1:03pm
10
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
n13ldo
November 21, 2023, 1:19pm
11
Have been looking for something like this. Could you please post the config for the sensor definition? Thanks
1 Like