ok, that will require some coding, but I like a challenge
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'
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'
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…
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
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
lol hence my question to @pnbruckner who is a bit of a wizard when it comes to stuff like that
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…
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?
Hi @lolouk44. Sorry, haven’t been ignoring you. I’ve been traveling so didn’t have enough time to read through the whole topic.
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. 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:
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
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.