Google home play a song /Playlist/sound automatically using Automation or script

ok, that will require some coding, but I like a challenge :slight_smile:
The random bit is fairly easy.
Let’s assume you call your mp3 Day0.mp3, Day1.mp3 […] Day9.mp3
the random bit will look like this:
Day{{range(0,10) | random}}.mp3

You can try and copy/paste this in the template editor, each time to press enter a new mp3 will be generated:

Actually going through the list randomly so that an MP3 is not repeated until you’ve gone through the whole list will take me a few tries, so bear with me.
In the meantime you can create a script to play random mp3 this way:

morning_greeting:
  alias: Greets each morning with a random message
  sequence:
    - service: media_player.play_media
      data_template:
        entity_id: media_player.living_room_speaker
        media_content_id: 'ipaddress of HA Pi/local/audio/Day{{range(0,10) | random}}.mp3'
        media_content_type: 'audio/mp4'
1 Like

I coundnt get it working. Nothing happens when the below script is run

My Script looks as follows:
'1534950734984':
alias: 01 GA RANDOM
sequence:
- data:
entity_id: media_player.living_room_speaker
media_content_id: http://ip address/local/audio/Random/Day{{range(0,3)| random}}.mp3
media_content_type: audio/mp3
service: media_player.play_media

can you format your code using the blue bar on top of the page?

1 Like

Done @lolouk44 hope i did it right

your indentations are not right.
also you have data, not data template, and you don’t appear to have single quotes
This is advanced script so you’ll need to edit the configuration.yaml file for this, not use the front-end ui to recreate
Should look like this:

'1534950734984':
  alias: 01 GA RANDOM
  sequence:
    - service: media_player.play_media
      data_template:
        entity_id: media_player.living_room_speaker
        media_content_id: 'ipaddress of HA Pi/local/audio/Day{{range(0,10) | random}}.mp3'
        media_content_type: 'audio/mp4'
1 Like

I “think” I got it…
It basically requires a few things:

a scrape sensor that will get a list of numbers from 0 to 9 in a random order
an input_select to store the list of above numbers
a counter to keep track of where we are in the list of random numbers
an automation to initialise the input_select
a script to play the correct Dayx.mp3 file where x is a number
an automation that calls the script on a daily basis (for you to create based on whichever trigger you want)
So…
Add this to your config.yaml: (replace ipaddress of HA Pi with the actual IP address)

counter:
  random_list_position:
    initial: 0
    step: 1

input_select:
  random_list:
    options:
      - " "

sensor:
  platform: scrape
    resource: https://www.calculatorsoup.com/calculators/statistics/number-generator.php?num_samples=10&range=9-0&duplicates=no&commas=no&action=solve
    name: Random_list
    select: 'div[id="answer"]'
    value_template: '{{ value | replace (" ", "") }}'


script:
  morning_greeting:
    alias: Greets each morning with a random message
    sequence:
      - service: media_player.play_media
        data_template:
          entity_id: media_player.living_room_speaker
          media_content_id: >-
			{% if states.counter.random_list_position.state == '0' %}ipaddress of HA 
			Pi/local/audio/Day{{states.input_select.random_list.attributes.options[0][0]}}.mp3
			{% elif states.counter.random_list_position.state == '1' %}ipaddress of HA 
			Pi/local/audio/Day{{states.input_select.random_list.attributes.options[0][1]}}.mp3
			{% elif states.counter.random_list_position.state == '2' %}ipaddress of HA 
			Pi/local/audio/Day{{states.input_select.random_list.attributes.options[0][2]}}.mp3
			{% elif states.counter.random_list_position.state == '3' %}ipaddress of HA 
			Pi/local/audio/Day{{states.input_select.random_list.attributes.options[0][3]}}.mp3
			{% elif states.counter.random_list_position.state == '4' %}ipaddress of HA 
			Pi/local/audio/Day{{states.input_select.random_list.attributes.options[0][4]}}.mp3
			{% elif states.counter.random_list_position.state == '5' %}ipaddress of HA 
			Pi/local/audio/Day{{states.input_select.random_list.attributes.options[0][5]}}.mp3
			{% elif states.counter.random_list_position.state == '6' %}ipaddress of HA 
			Pi/local/audio/Day{{states.input_select.random_list.attributes.options[0][6]}}.mp3
			{% elif states.counter.random_list_position.state == '7' %}ipaddress of HA 
			Pi/local/audio/Day{{states.input_select.random_list.attributes.options[0][7]}}.mp3
			{% elif states.counter.random_list_position.state == '8' %}ipaddress of HA 
			Pi/local/audio/Day{{states.input_select.random_list.attributes.options[0][8]}}.mp3
			{% elif states.counter.random_list_position.state == '9' %}ipaddress of HA 
			Pi/local/audio/Day{{states.input_select.random_list.attributes.options[0][9]}}.mp3
			{% endif %}
          media_content_type: 'audio/mp4'

automation:
  alias: Initialize list of greetings
    trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: counter.random_list_position
        to: '10'
    action:
    - service: input_select.set_options
      data_template:
        entity_id: input_select.random_list
        options: '{{states.sensor.random_list.state}}'
    - service: counter.reset
      entity_id: counter.random_list_position

I think that’s it, I’ve not fully tested it though…

2 Likes

Thanks This Works

Thnaks @lolouk44 ll try this & keep u posted

I was wondering whether @pnbruckner might a better /more elegant way of doing this?
I could not find a function to shuffle an array and I just could not for the life of me have an entity_id to select a single char, i.e.something like

Pi/local/audio/Day{{states.input_select.random_list.attributes.options[0][states.counter.random_list_position.state]}}.mp3

which would not work…

1 Like

Looks like its going crazy :frowning:

What does that mean? What’s not working? do you get any errors? What’s in your logs?

1 Like

btw have not tried this yet as am outside.
I mean your script code elif lines is for 9 items but i was thinking to create one for 365 days.
Each day 1 quote. i was imagining how big code i need to write to make that happen
hope u got it :slight_smile: :slight_smile: :slight_smile:

lol hence my question to @pnbruckner who is a bit of a wizard when it comes to stuff like that :slight_smile:
One thing to note though, there’s every chance that you’ll reboot/upgrade your HA at some stage and you’d lose the trail over 365 days, and it would reinitialise.
You may then be better off using the random bit I initially provided… The odds of having the same quote within a few days when you’ve got 365 to choose from are fairly low…

1 Like

Yes @lolouk44 I too think the same line because of initialization when we restart HA & complicated in coding for less tech knowledge on coding like myself.
I have decided to stick with your first shortcode What you say?

Thanks a bunch for helping me

1 Like

Hi @lolouk44. Sorry, haven’t been ignoring you. I’ve been traveling so didn’t have enough time to read through the whole topic. :slight_smile:

First, I agree with the ultimate conclusion to just pick a random file to play and not worry about when it might repeat. It’s a lot easier, and it’s, well, more random. :wink: But, as long as you asked…

I think I’d use an input_text instead of an input_select. Really no need for a second dimension. So, say you have input_text.random_list, which is a list of numerals from 0 to 9 in a random order. And you have counter.random_list_position. Then you could do pretty much what you were thinking:

address_of_pi/local/Day{{ states('input_text.random_list')[states('counter.random_list_position')|int] }}.mp3

I think you just forgot the |int filter to convert the state of the counter to an int.

1 Like

Thanks. That makes sense.
Although I would personally leave it on a random selection over 365 days, I still like the idea of iteration through a list at random.
The reason I originally opted for an input_select was to try and set a number on each row, but for some reason it always ended up with all numbers in a dimension on the first row.
This is slightly off topic now so I’ll open a new one

@pnbruckner here is the new topic I’ve raised in case you’re interested

Anybody here who knows the content_id for the “I feel lucky” function in Google Play Music?

1 Like

So I have been trying to replicate the stuff in this thread but I’m failing. Playing the song that is hosted online above works but I would like to play a local song in “/config/www/audio/song.mp3” to my Nest Hub.

My problem is that I can’t access the file even in my browser on http://IPADDRESS:8123/local/audio/song.mp3.

Any ideas why that is?

EDIT: A simple restart solved it all :slight_smile: