How To Create A List Of Random Messages For Notifications

The basic process is this:

  1. Create a list of messages.
  2. Store those message in an array.
  3. Count those messages.
  4. Generate a random number based on that count.
  5. Get the message at the random number's index position in the array.
  6. Make that message be the payload.
  7. Use that payload in a notification as the message data.

Here is a flow that you can import to see some of the functions that can help you achieve this goal. Copy this text and paste it into the import input in NodeRED. Each line in each function is explained.

[{"id":"e8b16d9b.c9233","type":"tab","label":"Simple NodeRed","disabled":false,"info":""},{"id":"1ed7f8d7.65f0f7","type":"function","z":"e8b16d9b.c9233","name":"Create list of messages and store to Global Variable","func":"// Create your list of messages and store them temporarily in myMessages\nvar myMessages = [\"message1\",\"message2\",\"message3\"];\n\n// Set a NodeRED Global Context Variable called \"myMessages\" and insert the contents of myMessages as defined in the previous line.\nglobal.set(\"myMessages\", myMessages);\n\n\nreturn msg;\n\n// You can setup a periodic inject node to trigger this to ensure that the messages\n// are always available to NodeRED.","outputs":1,"noerr":0,"initialize":"","finalize":"","x":540,"y":180,"wires":[["76f3500.0bd23b"]]},{"id":"739a83ee.86becc","type":"inject","z":"e8b16d9b.c9233","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":180,"wires":[["1ed7f8d7.65f0f7"]]},{"id":"76f3500.0bd23b","type":"debug","z":"e8b16d9b.c9233","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":930,"y":320,"wires":[]},{"id":"a6b82578.e61388","type":"inject","z":"e8b16d9b.c9233","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":280,"wires":[["ebb6b07d.bf389"]]},{"id":"ebb6b07d.bf389","type":"function","z":"e8b16d9b.c9233","name":"Example of how to access each message in the array","func":"// Get the value of the first value in the global variable (array) we created in the previous inject.\nvar currentMessage = global.get('myMessages[0]');\n\n// Set the payload to the currentMessage as defined in the last line.\nmsg.payload = currentMessage;\n\n// Send the message through.\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":540,"y":280,"wires":[["76f3500.0bd23b"]]},{"id":"e2a370d2.d10ec","type":"function","z":"e8b16d9b.c9233","name":"Example of how to count items in an array and assign to a variable.","func":"// Set a temporary value for currentMessage to the number of messages (length) in the array.\nvar currentMessage = global.get('myMessages').length;\n\n// Set the payload to that value.\nmsg.payload = currentMessage;\n\n// Send the message through.\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":580,"y":380,"wires":[["76f3500.0bd23b"]]},{"id":"981193d6.b31b","type":"inject","z":"e8b16d9b.c9233","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":380,"wires":[["e2a370d2.d10ec"]]},{"id":"46387613.508b68","type":"inject","z":"e8b16d9b.c9233","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":480,"wires":[["faebeaeb.953dc8"]]},{"id":"faebeaeb.953dc8","type":"function","z":"e8b16d9b.c9233","name":"Example of how to send the random message to the payload.","func":"// Get the length of the list of messages.\nvar messagesAvailable = global.get('myMessages').length;\n\n// Generate a random number based upon that number\nconst randomIndex = Math.floor(Math.random() * messagesAvailable);\n\n// Get the messages from the global variable and store them here in 'message'\nvar message = global.get('myMessages');\n\n// Set the message payload to the message in the position defined by the random index.\nmsg.payload = message[randomIndex];\n\n// Send the message through to the next node.\nreturn msg;\n\n\n// When you call the notify service, your data is going to be {\"message\":\"{{payload}}\"}\n// The notification node will be the last in the chain.","outputs":1,"noerr":0,"initialize":"","finalize":"","x":570,"y":480,"wires":[["76f3500.0bd23b"]]}]