Using helper value in python script, return string and use string in automation

Hi,

I try to achieve the following: I want to play one random one of 10 pre-specified spotify albums using the spotify integration.

To do so, I have so far defined a random number helper. Now I want to use a phyton script which uses this helper as input and selects from a list (external as .txt or within the python script) a URL at the position in the list that corresponds to the random number.

Then, I want to use this URL in an automation to play the Album on spotify.

At the moment, I have set up the python_script integration (which apparently does not support external libraries?). I need help on how to use the value of the helper as input in the python script, how to return the URL as a string in the python script and then how to access the returned URL it in the automation.

Your help is much appreciated :slight_smile:

Why go out to python for this? Stay within HA where the helper is available and do it here.

Actually, I think jinja2 has a random filter and can probably be done without a helper.

Well, the reason is that I had no idea how this could be implemented using home assistant.

On the other hand, when using phython I am still asking for help.

Any tips on how this could be implemented in home assistant directly?

Gotcha.

So, in a script,
Scripts - Home Assistant (home-assistant.io)
you can set a variable with …

variables:
  album: |-
    {{['album1',
       'album2',
       'album3',
       'album4',
       'album5',
       'album6',
       'album7',
       'album8',
       'album9',
       'album10']  | random }}

I don’t use Spotify. So, I don’t know how it is invoked. But, you should be able to use that variable in the command. You should be able to get the rest from the Spotify integration.

1 Like