# Expanding/Randomizing Output of Template Node

**URL:** <https://discourse.nodered.org/t/expanding-randomizing-output-of-template-node/53940>\
**Category:** General\
**Created:** [18 November 2021 13:45 UTC](https://discourse.nodered.org/t/expanding-randomizing-output-of-template-node/53940 "2021-11-18T13:45:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rlongfield](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rlongfield/32/14264_2.png) [@rlongfield](https://discourse.nodered.org/u/rlongfield)\
**Post date:** [18 November 2021 13:45 UTC](https://discourse.nodered.org/t/expanding-randomizing-output-of-template-node/53940/1 "2021-11-18T13:45:17Z")

</div>

I don't know enough to know if this is possible and I'm not even sure how to even start looking into this, so I'm hoping someone can send me in the right direction.

With some amazing help I've been able to create a few flows that take spoken input from Rhasspy, interfacing with Home Assistant, and/or my Chromecast Audio, turn some light on, change the thermostat temperature, or play some radio streams on the Chromecast (as well as controlling playback and volume).

What I am wonding is, is there a way to create some variation in the spoke response from Rhasspy indicating the request is being actioned.

For example when I ask Rhasspy to play a Radio Station, Rhasspy responds with 'Now Playing {{slots.station}}' (obviously slots.station is the actual station name.  
This response is a template node, set to mustache template, plain text output.

Is there a way for me to create other responses in this node and have one (sudo or actual) randomly selected?  
So instead of always getting 'Now Playing Station X' it might say 'Tuning to Station X' or 'Station X, Great choice'.

Possilbe, not possible?

Maybe some options that are station specific even?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [18 November 2021 22:49 UTC](https://discourse.nodered.org/t/expanding-randomizing-output-of-template-node/53940/2 "2021-11-18T22:49:24Z")

</div>

> [@rlongfield](#):
>
> Possilbe, not possible?

With some conditional mustache syntax yes, but its not pretty.

Far easier with a function node...

```auto
const station = msg.payload;
const options = [
    `Now playing station ${station}`,
    `Playing station ${station}, good choice`,
    `Tuning to Station ${station}`,
    //add more here
];
const index = Math.floor(Math.random() * options.length);
msg.payload = options[index];
return msg;

```

![dlYZT1j18Y](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/7/e7fec2c35ea1b26af1cc6d7f4141636c6b60d658.gif)

```auto
[{"id":"8fc733f4f1f3151a","type":"function","z":"9e7ee46441f8c187","name":"format station message","func":"const station = msg.payload;\nconst options = [\n `Now playing station ${station}`,\n `Playing station ${station}, good choice`,\n `Tuning to Station ${station}`,\n];\nconst index = Math.floor(Math.random() * options.length);\nmsg.payload = options[index];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2143.999984741211,"y":110.00002479553223,"wires":[["299967efbe8cd9a7"]]},{"id":"9ad7f8dc2fb7489a","type":"inject","z":"9e7ee46441f8c187","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Absolute Classic Rock","payloadType":"str","x":1843.999984741211,"y":110.00002479553223,"wires":[["8fc733f4f1f3151a"]]},{"id":"00a8b2fa6372eee9","type":"inject","z":"9e7ee46441f8c187","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Planet Rock","payloadType":"str","x":1813.999984741211,"y":150.00002479553223,"wires":[["8fc733f4f1f3151a"]]},{"id":"299967efbe8cd9a7","type":"debug","z":"9e7ee46441f8c187","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":2173.999984741211,"y":170.00002479553223,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![rlongfield](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rlongfield/32/14264_2.png) [@rlongfield](https://discourse.nodered.org/u/rlongfield)\
**Post date:** [19 November 2021 03:59 UTC](https://discourse.nodered.org/t/expanding-randomizing-output-of-template-node/53940/3 "2021-11-19T03:59:53Z")

</div>

> [@Steve-Mcl](#):
>
> Far easier with a function node...

That makes so much sense

---

<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:** [3 December 2021 04:00 UTC](https://discourse.nodered.org/t/expanding-randomizing-output-of-template-node/53940/4 "2021-12-03T04:00:50Z")

</div>

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