Node-red question for random text output

I’m very new to node-red and would like to add random payload to be TTS by google home. Did anyone have any example on how can I add multiple payload and return it randomly?

This is a weather changes function in node-red and would like to add more variety to the payload

switch (msg.payload)
{
    case "clear-night":
        payload = "Weather changes to clear-night";
        break;
    case "cloudy":
        payload = "Weather changes to cloudy";
        break;
    default:
        payload = "";
        return [null, msg];
}
msg.payload = payload;
return [msg,null]

image

[{"id":"63557d198b49e9d9","type":"inject","z":"2be14480c912336c","name":"clear-night","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"clear-night","payloadType":"str","x":202,"y":192,"wires":[["b33be2e906f14ab8"]]},{"id":"88aafb6d7f142ad8","type":"inject","z":"2be14480c912336c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"cloudy","payloadType":"str","x":194,"y":224,"wires":[["b33be2e906f14ab8"]]},{"id":"3374419f1ae2cca6","type":"inject","z":"2be14480c912336c","name":"empty","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"str","x":194,"y":256,"wires":[["b33be2e906f14ab8"]]},{"id":"b33be2e906f14ab8","type":"function","z":"2be14480c912336c","name":"","func":"const text = {\n    \"clear-night\": [\n        \"Weather changes to clear-night\",\n        \"abc\",\n        \"def\",\n        \"ghi\",\n    ],\n    cloudy: [\n        \"Weather changes to cloudy\",\n        \"123\",\n        \"345\",\n        \"456\",\n    ]\n};\n\nif (!text[msg.payload]) return [null, msg];\n\nconst items = text[msg.payload];\nmsg.payload = items[Math.floor(Math.random()*items.length)];\n\nreturn msg;","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":192,"wires":[["08880f116e91d95e"],["52132dc9fe318940"]]},{"id":"08880f116e91d95e","type":"debug","z":"2be14480c912336c","name":"from list","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":556,"y":192,"wires":[]},{"id":"52132dc9fe318940","type":"debug","z":"2be14480c912336c","name":"not in list","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":556,"y":240,"wires":[]}]

Hi,

Is there any chances you can paste the content ot functions? I mean I can’t read the info.

You can import the sequence from the menu at the top right.

But this seems to be the function, just replace \n with new line.

Got it… thanks

Thanks for the import functions… I forget that this exist for nodered.