Use a random number to be used as an index lookup in an array?

I’m not sure if I’m on the right track.

When we start our coffee maker, a timer starts. When that timer ends, the coffee maker is turned off. At that moment, a random MP3 file is played on a media player.

I’m attempting to adapt this automation to Node-RED, but I’m not sure how to do it.

I first want to:

a) Look at an array to determine how many key/pairs there are so I know what the range of random numbers should be.

b) generate a random number in that range.

c) Use that number as the index position for the value of the pair.

d) pass that value as the payload to the action, which would ultimately be TTS, or perhaps calling a pre-recorded MP3 file.

The current script I have uses a data template to calculate the number of files in a folder, then uses that number to generate a random number in that range. It then combines that random number with the name of the folder to identify the audio file (which I’ve pre-recorded and numbered accordingly) in the www folder of HASS.io.

The automation I’d like would look at an array that would be hosted on a public URL in a simple JSON format, count the entries, create a random number based upon the count, pull the value in the indexed position ( a simple quote or something of that nature ) and then turn that value into the payload.

Ideas?

[{"id":"786d6acb.2509f4","type":"inject","z":"5eb3594f.d294b8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":176,"y":2000,"wires":[["ee2b997a.c94d98"]]},{"id":"ee2b997a.c94d98","type":"http request","z":"5eb3594f.d294b8","name":"","method":"GET","ret":"obj","paytoqs":false,"url":"https://breaking-bad-quotes.herokuapp.com/v1/quotes/5","tls":"","proxy":"","authType":"basic","x":326,"y":2000,"wires":[["95a8b12.394465"]]},{"id":"95a8b12.394465","type":"function","z":"5eb3594f.d294b8","name":"","func":"const randomIndex = Math.floor(Math.random() * msg.payload.length);\nmsg.payload = msg.payload[randomIndex];\n\nreturn msg;","outputs":1,"noerr":0,"x":466,"y":2000,"wires":[["7ca340bc.83276"]]},{"id":"7ca340bc.83276","type":"debug","z":"5eb3594f.d294b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":614,"y":2000,"wires":[]}]

Oh…sweet. What happens if the JSON object is something like a book that contains chapters and pages and sections?

Woo hoo! Sweet. I got it! It looks up a quote then randomly says the author, pauses, then says the quote.

I wonder if there’s a better way to insert a pause inside of a template in one of the TTS calls instead of needing to time them apart from each other. If I add both attributes of the payload to the same TTS call, it sounds like a run-on sentence.

[{"id":"1ee2036.558b3fd","type":"server-state-changed","z":"80c5bbcf.f30568","name":"Reading Lamp Switch","server":"453178ab.f3be18","entityidfilter":"switch.reading_lamp_1","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":140,"y":60,"wires":[["14a053ca.e902bc"]]},{"id":"2384bd79.6adbe2","type":"api-call-service","z":"80c5bbcf.f30568","name":"Google Say Quote","server":"453178ab.f3be18","service_domain":"tts","service":"google_say","data":"{\"entity_id\":\"media_player.kitchen_speaker, media_player.bathroom_speaker, media_player.master_bedroom_speaker\",\"message\":\"{{payload.body}}\"}","mergecontext":"","output_location":"","output_location_type":"none","x":894,"y":126,"wires":[[]]},{"id":"1fec3c24.76f7e4","type":"function","z":"80c5bbcf.f30568","name":"Random Generator","func":"const randomIndex = Math.floor(Math.random() * msg.payload.length);\nmsg.payload = msg.payload[randomIndex];\n\nreturn msg;","outputs":1,"noerr":0,"x":495,"y":56,"wires":[["5da61539.10ab3c"]]},{"id":"14a053ca.e902bc","type":"http request","z":"80c5bbcf.f30568","name":"Get Quotes","method":"GET","ret":"obj","paytoqs":false,"url":"https://raw.githubusercontent.com/psyked/quotes/master/quotes.json","tls":"","proxy":"","authType":"basic","x":380,"y":126,"wires":[["1fec3c24.76f7e4"]]},{"id":"c99a7b11.1e1d98","type":"inject","z":"80c5bbcf.f30568","name":"Manual Trigger","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":159,"y":145,"wires":[["14a053ca.e902bc"]]},{"id":"5da61539.10ab3c","type":"api-call-service","z":"80c5bbcf.f30568","name":"Google Say Author","server":"453178ab.f3be18","service_domain":"tts","service":"google_say","data":"{\"entity_id\":\"media_player.bathroom_speaker, media_player.kitchen_speaker, media_player.master_bedroom_speaker\",\"message\":\"{{payload.attribution}} says\"}","mergecontext":"","output_location":"","output_location_type":"none","x":618,"y":127,"wires":[["aaaaac24.1e299"]]},{"id":"aaaaac24.1e299","type":"ha-wait-until","z":"80c5bbcf.f30568","name":"Wait 2.5 Seconds","server":"453178ab.f3be18","outputs":2,"entityId":"","property":"","comparator":"is","value":"","valueType":"str","timeout":"2500","timeoutUnits":"milliseconds","entityLocation":"","entityLocationType":"none","checkCurrentState":true,"x":746.5,"y":55,"wires":[[],["2384bd79.6adbe2"]]},{"id":"453178ab.f3be18","type":"server","z":"","name":"Home Assistant"}]