# Help building variable name from nodes

**URL:** <https://discourse.nodered.org/t/help-building-variable-name-from-nodes/73841>\
**Category:** General\
**Created:** [18 January 2023 03:31 UTC](https://discourse.nodered.org/t/help-building-variable-name-from-nodes/73841 "2023-01-18T03:31:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmooty73](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cmooty73/32/74541_2.png) [@cmooty73](https://discourse.nodered.org/u/cmooty73)\
**Post date:** [18 January 2023 03:31 UTC](https://discourse.nodered.org/t/help-building-variable-name-from-nodes/73841/1 "2023-01-18T03:31:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [18 January 2023 07:49 UTC](https://discourse.nodered.org/t/help-building-variable-name-from-nodes/73841/2 "2023-01-18T07:49:16Z")

</div>

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.

```auto
( 
$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

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

```

---

<div class="post-metadata">

**Author:** ![cmooty73](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cmooty73/32/74541_2.png) [@cmooty73](https://discourse.nodered.org/u/cmooty73)\
**Post date:** [19 January 2023 04:08 UTC](https://discourse.nodered.org/t/help-building-variable-name-from-nodes/73841/3 "2023-01-19T04:08:17Z")

</div>

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:

```auto
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:

```auto
( 
$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?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [19 January 2023 07:52 UTC](https://discourse.nodered.org/t/help-building-variable-name-from-nodes/73841/4 "2023-01-19T07:52:47Z")

</div>

Replace

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

```

with

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

```

Or  
If you want/need to use flow context

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

```

Or  
Set context to

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

```

and

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

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [2 February 2023 07:52 UTC](https://discourse.nodered.org/t/help-building-variable-name-from-nodes/73841/5 "2023-02-02T07:52:51Z")

</div>

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