Help building variable name from nodes

All, I'm trying to send a random sound to Alexa from a list of 10 of her sounds. I've used a change node to assign the sound names to flow.1 through flow.10. Then a random node picks a number from 1 through 10. Now I'm trying to combine that number with "flow." and pass the resulting variable to a home assistant call service node which requires data in json format that looks like this:
{"media_content_id":"NAME OF SOUND","media_content_type":"sound"}.
At one point I had it spitting out a JSON in this format, but the flow.X was coming out instead of the value of flow.X. Can anyone recommend a better way to do this?

In the call service node select JSONata in the data field, you can then use a JSONata expression to pick a random sound.
e.g.

( 
$sounds := ["sound1","sound2","sound3","soundnth"];
$array_length := $count($sounds);
$position := $floor($random() * $array_length);
{
    "media_content_id": $sounds[$position],
    "media_content_type": "sound"
}
)

which should output

{
  "media_content_id":"sound1",
  "media_content_type":"sound"
}

Thank you for your help. Your code seems to be working just as you say, but it's outputting a random array member, such as "sound2". I was expecting it to output the sound name that I had assigned to sound2. I took it down to two nodes with your suggestion:
Var Declaration Function node:

flow.set("sound1","amzn_sfx_large_crowd_cheer_01");
flow.set("sound2", "buzzers_pistols_01");
flow.set("sound3", "amzn_sfx_church_bell_1x_02");
flow.set("sound4", "amzn_sfx_doorbell_chime_02");
flow.set("sound5", "air_horn_03");
flow.set("sound6", "amzn_sfx_trumpet_bugle_04");
flow.set("sound7", "amzn_sfx_rooster_crow_01");
flow.set("sound8", "amzn_sfx_scifi_engines_on_02");
flow.set("sound9", "amzn_sfx_scifi_alarm_01");
flow.set("sound10", "amzn_sfx_large_crowd_cheer_01");
return msg;

and the call service node:

( 
$sounds := ["sound1","sound2","sound3","sound4","sound5","sound6","sound7","sound8","sound9","sound10"];
$array_length := $count($sounds);
$position := $floor($random() * $array_length);
{
    "media_content_id": $sounds[$position],
    "media_content_type": "sound"
}
)

How do I get it to plug in the variable value instead of the variable name?

Replace

$sounds := ["sound1","sound2","sound3","sound4","sound5","sound6","sound7","sound8","sound9","sound10"];

with

$sounds := ["amzn_sfx_large_crowd_cheer_01", 
 "buzzers_pistols_01",  "etc......"];

Or
If you want/need to use flow context

"media_content_id": $flowContext($sounds[$position]),

Or
Set context to

flow.set("sounds",["amzn_sfx_large_crowd_cheer_01", 
 "buzzers_pistols_01",  "etc......"]);
return msg;

and

$sounds :=  $flowContext("sounds");

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.