# Function node, how to send text out 5 times to debug node?

**URL:** <https://discourse.nodered.org/t/function-node-how-to-send-text-out-5-times-to-debug-node/85527>\
**Category:** General\
**Created:** [14 February 2024 12:05 UTC](https://discourse.nodered.org/t/function-node-how-to-send-text-out-5-times-to-debug-node/85527 "2024-02-14T12:05:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [14 February 2024 12:05 UTC](https://discourse.nodered.org/t/function-node-how-to-send-text-out-5-times-to-debug-node/85527/1 "2024-02-14T12:05:21Z")

</div>

Hi,

If I receive a number greater than 80, output the same message 5 times, this goes to a queue and speech.

```auto
if (msg.payload >= 80) 
{

   for (let i = 0; i < 5; i++) 
   {
      var Text_to_speak = "Humidity exceeded ... in the ,,, LOFT ... Module ,, Twenty Five ..... ";

      msg.payload = Text_to_speak;

      return msg;
   }

}

```

Thanks

---

<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:** [14 February 2024 12:16 UTC](https://discourse.nodered.org/t/function-node-how-to-send-text-out-5-times-to-debug-node/85527/2 "2024-02-14T12:16:48Z")

</div>

use `node.send(msg)`

also, you should clone the `msg` before updating to avoid "by reference" _weirdness_ that you can fall foul of in async systems.

```auto
if (msg.payload >= 80) {
   for (let i = 0; i < 5; i++) {
      const m = RED.util.cloneMessage(msg)
      m.payload = "Humidity exceeded ... in the ,,, LOFT ... Module ,, Twenty Five ..... "
      node.send(m)
   }
}

```

It is all in [the docs](https://nodered.org/docs/user-guide/writing-functions#sending-messages-asynchronously).

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [14 February 2024 14:54 UTC](https://discourse.nodered.org/t/function-node-how-to-send-text-out-5-times-to-debug-node/85527/3 "2024-02-14T14:54:45Z")

</div>

HI Steve,

Thank you for that and as I would expect from you it does work 🙂

One thing I have noticed lately, code that I wrote a few years ago and then I come back to it today, I can no longer understand the more technical words, so I try and keep all my code as logical and simple as possible 👍

So i have modified your code a little and it does seem to work, is there anything wrong with doing it this way ?

Thanks for your help

```auto
if (msg.payload >= 80) 
{
   for (let i = 0; i < 5; i++) 
   {
      msg.text = "Humidity exceeded ... in the ,,, LOFT ... Module ,, Twenty Five ..... "
      msg.payload = msg.text
      node.send(msg)
   }
}

```

---

<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:** [14 February 2024 15:03 UTC](https://discourse.nodered.org/t/function-node-how-to-send-text-out-5-times-to-debug-node/85527/4 "2024-02-14T15:03:48Z")

</div>

> [@questuk](#):
>
> is there anything wrong with doing it this way ?
> 
> Thanks for your help
> 
> ```auto
> if (msg.payload >= 80) 
> {
> for (let i = 0; i < 5; i++) 
> {
> msg.text = "Humidity exceeded ... in the ,,, LOFT ... Module ,, Twenty Five ..... "
> msg.payload = msg.text
> node.send(msg)
> }
> }
> 
> ```

At first glance no. but, since `msg` is an object, and NodeJS/Node-RED is highly asynchronous, there is a risk that when you alter `msg.payload` that the other 4 are altered (if they are still in-flight). That is what as known as "by reference"

essentially an object (like `msg`) is sent by reference (think `C/C++` pointer to an object) so changing anything in `msg` leaves you vulnerable to side effects - that's why i added the `RED.util.cloneMessage`

* * *

If you want clarity later on, add comments...

```auto
if (msg.payload >= 80) {
   // loop 5 times, to generate 5 messages
   for (let i = 0; i < 5; i++) {
      // clone msg into a NEW separate variable `m` so that we dont alter any in-flight messages
      const m = RED.util.cloneMessage(msg)
      // update the payload of the clone
      m.payload = "Humidity exceeded ... in the ,,, LOFT ... Module ,, Twenty Five ..... "
      // send the clone msg
      node.send(m)
   }
}

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [14 February 2024 15:15 UTC](https://discourse.nodered.org/t/function-node-how-to-send-text-out-5-times-to-debug-node/85527/5 "2024-02-14T15:15:12Z")

</div>

Since @questuk is _sending the same message_ 5 times, wouldn't this be simpler?

```auto
if (msg.payload >= 80) 
{
   msg.payload = "Humidity exceeded ... in the ,,, LOFT ... Module ,, Twenty Five ..... "
   for (let i = 0; i < 5; i++) 
   {
      node.send(msg)
   }
}

```

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [14 February 2024 15:15 UTC](https://discourse.nodered.org/t/function-node-how-to-send-text-out-5-times-to-debug-node/85527/6 "2024-02-14T15:15:45Z")

</div>

HI Steve,

Thanks for that, I mainly write Arduino code and nearly every line has comments 👍

When I saw your code, I instantly looked online to understand and add my comments, but I couldn't find much understandable info.

Thanks for updating me on that and the reasons why, I now understand it.

Thanks

---

<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:** [14 February 2024 15:24 UTC](https://discourse.nodered.org/t/function-node-how-to-send-text-out-5-times-to-debug-node/85527/7 "2024-02-14T15:24:23Z")

</div>

> [@jbudd](#):
>
> Since @questuk is _sending the same message_ 5 times, wouldn't this be simpler?

Yes, but I assume _something_ will be changed (better to be safe than have a hard to track bug)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 February 2024 15:26 UTC](https://discourse.nodered.org/t/function-node-how-to-send-text-out-5-times-to-debug-node/85527/8 "2024-02-14T15:26:10Z")

</div>

> [@jbudd](#):
>
> wouldn't this be simpler?

Since `node.send()` does not clone the message and since objects are stored by reference, all the messages would point to the same object in memory, therefore if a later node were, for example, to do  
`msg.payload = 7`  
then that would affect the payload of all 5 messages (since they all point to the same object in memory).

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [14 February 2024 15:35 UTC](https://discourse.nodered.org/t/function-node-how-to-send-text-out-5-times-to-debug-node/85527/9 "2024-02-14T15:35:08Z")

</div>

> [@Colin](#):
>
> if a later node were, for example, to do  
> `msg.payload = 7`

Yes, I see that if these messages are passed on beyond whatever speaks the message, for example to send an email, changes made to message 1 after speaking will also affect messages 2 - 4 still in the speech queue.

I assume that these five (ie 5 instances of this single) message are entirely seperate from any other msg object flying around Node-red.

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [14 February 2024 15:52 UTC](https://discourse.nodered.org/t/function-node-how-to-send-text-out-5-times-to-debug-node/85527/10 "2024-02-14T15:52:53Z")

</div>

Hi,

Yes it is meant as a warning that there is a possible leak in the Loft and is announced on a speaker.

I also have sensors that monitor temperature in most rooms.  
As an example 2 rooms could both get excessive temperature do to a fire in one of them.  
So I would have 2 warnings that would both announce over the speaker which room has the excessive heat.

So in this example, I guess the 2 rooms messages would get mixed up as both Colin and Steve mentioned?

But with the code that they have given me, one warning would complete then the other warning after that and then if still excessive heat they would continue to announce without getting mixed up.

I think that is how it would now operate ?

Thanks 👍

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 February 2024 15:58 UTC](https://discourse.nodered.org/t/function-node-how-to-send-text-out-5-times-to-debug-node/85527/11 "2024-02-14T15:58:41Z")

</div>

> [@questuk](#):
>
> I guess the 2 rooms messages would get mixed up as both Colin and Steve mentioned?

Not if those were as a result of a new message passed into your function.

I strongly recommend using clone() in cases like this, even if you think it is not necessary. The saving in not doing so is insignificant (unless you have messages containing large objects such as video, images or files) and it can be a nightmare trying to track down bugs caused in this way as they can be timing dependent and the symptom may not be seen till much further down the flow.

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [14 February 2024 16:21 UTC](https://discourse.nodered.org/t/function-node-how-to-send-text-out-5-times-to-debug-node/85527/12 "2024-02-14T16:21:52Z")

</div>

Hi Colin,

This is my code now...

```auto
if (msg.payload >= 80)
{

   for (let i = 0; i < 5; i++) 
   {
                                                                                            
      const text = RED.util.cloneMessage(msg) // Clone msg into a NEW separate variable `text` so that we don't alter any in-flight messages.                                                                            
      text.payload = "Humidity exceeded ... in the ,,, LOFT ... Module ,, Twenty Five ... " // Update the payload of the clone.
      node.send(text) // Send the clone msg.

   }
}

```

I think this has everything you mentioned.

Thanks

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 February 2024 16:48 UTC](https://discourse.nodered.org/t/function-node-how-to-send-text-out-5-times-to-debug-node/85527/13 "2024-02-14T16:48:46Z")

</div>

> [@questuk](#):
>
> ` const text = RED.util.cloneMessage(msg)`

One of the most important ways of documenting code so that you will understand it when you come to look at it later is giving variables meaningful names. After the above line you have an object called text, but it isn't the text, the text in is text.payload. It is a message so I would call it something like msg2.

Also you are executing `text.payload = ...` every time round the loop, which is pointless, move that outside the loop.

```auto
if (msg.payload >= 80)
{
   msg.payload = "Humidity exceeded ... in the ,,, LOFT ... Module ,, Twenty Five ... "
   for (let i = 0; i < 5; i++) 
   {
      const msg2 = RED.util.cloneMessage(msg) // Clone msg into a NEW separate variable `msg2` so that we don't alter any in-flight messages.                                                                            
      node.send(msg2) // Send the clone msg.

   }
}

```

In fact there is no need for msg2 at all, inside the loop just use  
`node.send(RED.util.cloneMessage(msg)) // clone the message and send the clone`

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [14 February 2024 17:32 UTC](https://discourse.nodered.org/t/function-node-how-to-send-text-out-5-times-to-debug-node/85527/14 "2024-02-14T17:32:38Z")

</div>

> [@Colin](#):
>
> node.send(RED.util.cloneMessage(msg)) // clone the message and send the clone

Hi Colin,

I have adjusted the function as you stated all works ok.

```auto
if (msg.payload >= 80)
{
   msg.payload = "Humidity exceeded ... in the ,,, LOFT ... Module ,, Twenty Five ... "
   
for (let i = 0; i < 5; i++) 
   {
      node.send(RED.util.cloneMessage(msg)) // clone the message and send the clone

   }
}

```

---

<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:** [14 February 2024 17:42 UTC](https://discourse.nodered.org/t/function-node-how-to-send-text-out-5-times-to-debug-node/85527/15 "2024-02-14T17:42:57Z")

</div>

> [@Colin](#):
>
> Since `node.send()` does not clone the message a

That is not what the docs say

> The Function node will clone every message object you pass to `node.send` to ensure there is no unintended modification of message objects that get reused in the function. Before Node-RED 1.0, the Function node would not clone the _first_ message passed to `node.send`, but would clone the rest.

> The Function can request the runtime to _not clone_ the first message passed to `node.send` by passing in `false` as a second argument to the function. It would do this if the message contains something that is not otherwise cloneable, or for performance reasons to minimise the overhead of sending messages:

```auto
node.send(msg,false);

```

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [14 February 2024 17:45 UTC](https://discourse.nodered.org/t/function-node-how-to-send-text-out-5-times-to-debug-node/85527/16 "2024-02-14T17:45:33Z")

</div>

@E1cid

Hi, Yes I read that earlier today, so I am a bit confused 🤔

---

<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:** [14 February 2024 17:47 UTC](https://discourse.nodered.org/t/function-node-how-to-send-text-out-5-times-to-debug-node/85527/17 "2024-02-14T17:47:12Z")

</div>

~~You should be able, according to the docs just use `node.send(msg,false)`~~

Scrap that just use `node.send(message)`. Tired, had a long day.  
no need for RED.util.cloneMessage()`.

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [14 February 2024 17:49 UTC](https://discourse.nodered.org/t/function-node-how-to-send-text-out-5-times-to-debug-node/85527/18 "2024-02-14T17:49:39Z")

</div>

Actually I do want it to clone the first message only, so it would be ...

```auto
node.send(msg);

```

?

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [14 February 2024 17:51 UTC](https://discourse.nodered.org/t/function-node-how-to-send-text-out-5-times-to-debug-node/85527/19 "2024-02-14T17:51:52Z")

</div>

No problem have a restful evening 🙂

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [14 February 2024 18:01 UTC](https://discourse.nodered.org/t/function-node-how-to-send-text-out-5-times-to-debug-node/85527/20 "2024-02-14T18:01:41Z")

</div>

Hi all,

So it looks like this is the final code  
Tested from 2 different rooms each sending a different warning, almost sent at the same time.  
All announced correctly and each room fully completed speech, before announcing the other room warning.

```auto

if (msg.payload >= 80)
{
   msg.payload = "Humidity exceeded ... in the ,,, LOFT ... Module ,, Twenty Five ... "
   
   for (let i = 0; i < 5; i++) 
   {
      node.send(msg); // clone the message and send the clone

   }
}

```

[Next page](https://discourse.nodered.org/t/function-node-how-to-send-text-out-5-times-to-debug-node/85527.md?page=2)
