# Function node sends multiple messages instead of one

**URL:** https://discourse.nodered.org/t/function-node-sends-multiple-messages-instead-of-one/83843
**Category:** General
**Tags:** function-node
**Created:** [20 December 2023 00:24 UTC](https://discourse.nodered.org/t/function-node-sends-multiple-messages-instead-of-one/83843 "2023-12-20T00:24:19Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![ChrisG](https://avatars.discourse-cdn.com/v4/letter/c/5f8ce5/32.png) [@ChrisG](https://discourse.nodered.org/u/ChrisG)
#### Post date: [20 December 2023 00:24 UTC](https://discourse.nodered.org/t/function-node-sends-multiple-messages-instead-of-one/83843/1 "2023-12-20T00:24:19Z")

</div>

Hi,

Node-Red v3.1.0

I have a simple 'edit function node' which collects values from global variables and formats a message with those values. I want this function to run once a minute so have put a 'inject node' in front of it.

To monitor whats happening, I've put a 'edit debug node' on the output.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/a/7a43fdb2085f438cffec11fc8542099bb3dba220.png)

What I want to happen is for the inject node to start the activity, the function node to create the message and send it onwards, and then wait for the next trigger.

What I get is the inject node starts the activity, and the debug node 'sees' the message, then sees it again, and again, and so on. At the rate of one every second.

Any ideas would be much appreciated.

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [20 December 2023 00:54 UTC](https://discourse.nodered.org/t/function-node-sends-multiple-messages-instead-of-one/83843/2 "2023-12-20T00:54:22Z")

</div>

> [@ChrisG](#):
>
> What I get is the inject node starts the activity, and the debug node 'sees' the message, then sees it again, and again, and so on. At the rate of one every second.

Hello Chris,

Ofcourse the Function node will see every message you send it .. now i gather that you want to do different things on the 2nd 3rd 4th etc msg .. so you have to somehow differentiate between the trigger msgs ?

One way to accomplish this is with Context  
there are three types

> ```
> context - the node’s local context
> flow - the flow scope context
> global - the global scope context
> 
> ```

You could use `context` which is available only to the Function node's scope to keep track of the msgs

Example of a counter is available in the Node-red Documentation [here](https://nodered.org/docs/user-guide/writing-functions#storing-data)

---

<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: [20 December 2023 01:01 UTC](https://discourse.nodered.org/t/function-node-sends-multiple-messages-instead-of-one/83843/3 "2023-12-20T01:01:20Z")

</div>

I suspect your problem may be here  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/7/a750cd21239559832b4635df936c589e8aa1bd07.png)

"_Every 1m_" is in italics, indicating that's the node's name, not how frequently it is set to trigger.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/8/68a29ed8313515ac50db924436d85c86dce92cd7.png)

Mind you, I'm not familiar with this node  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/a/4ae72620f93721a9219413dc73edf2da8414d3b8.png)

And there may well be a problem here too  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/d/1d5f7b8fde29e3c7567bdffc57876e64048e66b1.png)

---

<div class="post-metadata">

### Author: ![ChrisG](https://avatars.discourse-cdn.com/v4/letter/c/5f8ce5/32.png) [@ChrisG](https://discourse.nodered.org/u/ChrisG)
#### Post date: [20 December 2023 07:17 UTC](https://discourse.nodered.org/t/function-node-sends-multiple-messages-instead-of-one/83843/4 "2023-12-20T07:17:01Z")

</div>

Thanks Guys,

UnborN, you are right re the function node reacting to every message, but I was hoping the trigger sending one message per minute would mean the function node would do one message per minute.

I'll look more at the context you mentioned.

jbudd, you are right, with the inject node name being what shows, but the node was also configured to 1 minute.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/3/e3d3ef9ed9849898b33e90a53ef615e4fd98a395.png)  
the msg-speed is a node that just counts the rate of messages being received. This is what it looks like after running all night:-  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/9/a96efdaad62e78af439900f41a1973f0f2c2a623.png)  
60/hour indicates it's receiving 1 per minute.

I'll re-jig the flow and post shortly.

thanks

---

<div class="post-metadata">

### Author: ![ChrisG](https://avatars.discourse-cdn.com/v4/letter/c/5f8ce5/32.png) [@ChrisG](https://discourse.nodered.org/u/ChrisG)
#### Post date: [20 December 2023 07:36 UTC](https://discourse.nodered.org/t/function-node-sends-multiple-messages-instead-of-one/83843/5 "2023-12-20T07:36:54Z")

</div>

Here is the json for the issue:-

[{"id":"4b211f23c08b43f4","type":"tab","label":"Flow 5","disabled":false,"info":"","env":},{"id":"af5483388c10dfb5","type":"function","z":"4b211f23c08b43f4","name":"Combine 6 global variables into one payload","func":"msg.payload = \n\n{\n\n "solarPvId" : flow.get("solarPvId"),\n "solarPvTimestamp" : flow.get("solarPvTimestamp"),\n "currentGeneratingUnitsW" : flow.get("currentGeneratingUnitsW"),\n "exportingUnintsW" : flow.get("exportingUnintsW"),\n "currentDayGeneratedW" : flow.get("currentDayGeneratedW"),\n "currentDayExportedW" : flow.get("currentDayExportedW")\n\n}\nnode.send(msg);\n\n//return msg;\n\n","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":,"x":850,"y":140,"wires":[["8284852709ca22a8"]]},{"id":"8284852709ca22a8","type":"debug","z":"4b211f23c08b43f4","name":"dB payload","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1190,"y":140,"wires":},{"id":"638a99cd0d575a4e","type":"msg-speed","z":"4b211f23c08b43f4","name":"","frequency":"hour","interval":1,"estimation":false,"ignore":false,"pauseAtStartup":false,"topicDependent":false,"x":510,"y":140,"wires":[["af5483388c10dfb5"],]},{"id":"ea1bc11cc99b2091","type":"inject","z":"4b211f23c08b43f4","name":"Every 1m","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":true,"onceDelay":"10","topic":"","payload":"","payloadType":"date","x":250,"y":140,"wires":[["638a99cd0d575a4e"]]}]

obviously the function isn't getting any data now as the flow variables aren't being created, but the output is still showing messages at 1/second (timestamp on when something comes from a node within the flow):-

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/f/2f12d81b37a53f099aec638fb56ee0b8ef179940.png)

---

<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: [20 December 2023 07:45 UTC](https://discourse.nodered.org/t/function-node-sends-multiple-messages-instead-of-one/83843/6 "2023-12-20T07:45:55Z")

</div>

Add debug nodes to the outputs of the inject and the speed nodes. Do they also show the same frequency of messages?

Also, your flow is corrupt as you have not wrapped it in a code fence.

**Canned Text:**  
In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

You can edit and correct your post by clicking the pencil ✏ icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

### Author: ![ChrisG](https://avatars.discourse-cdn.com/v4/letter/c/5f8ce5/32.png) [@ChrisG](https://discourse.nodered.org/u/ChrisG)
#### Post date: [20 December 2023 07:51 UTC](https://discourse.nodered.org/t/function-node-sends-multiple-messages-instead-of-one/83843/7 "2023-12-20T07:51:48Z")

</div>

Ah Steve,

Here is the flow with a 2nd speed node and ticks:-

```auto
[{"id":"4b211f23c08b43f4","type":"tab","label":"Flow 5","disabled":false,"info":"","env":[]},{"id":"af5483388c10dfb5","type":"function","z":"4b211f23c08b43f4","name":"Combine 6 global variables into one payload","func":"msg.payload = \n\n{\n\n \"solarPvId\" : flow.get(\"solarPvId\"),\n \"solarPvTimestamp\" : flow.get(\"solarPvTimestamp\"),\n \"currentGeneratingUnitsW\" : flow.get(\"currentGeneratingUnitsW\"),\n \"exportingUnintsW\" : flow.get(\"exportingUnintsW\"),\n \"currentDayGeneratedW\" : flow.get(\"currentDayGeneratedW\"),\n \"currentDayExportedW\" : flow.get(\"currentDayExportedW\")\n\n}\nnode.send(msg);\n\n//return msg;\n\n","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":850,"y":140,"wires":[["cb470dd8032cfddf"]]},{"id":"8284852709ca22a8","type":"debug","z":"4b211f23c08b43f4","name":"dB payload","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1330,"y":140,"wires":[]},{"id":"638a99cd0d575a4e","type":"msg-speed","z":"4b211f23c08b43f4","name":"","frequency":"hour","interval":1,"estimation":false,"ignore":false,"pauseAtStartup":false,"topicDependent":false,"x":510,"y":140,"wires":[["af5483388c10dfb5"],[]]},{"id":"ea1bc11cc99b2091","type":"inject","z":"4b211f23c08b43f4","name":"Every 1m","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":true,"onceDelay":"10","topic":"","payload":"","payloadType":"date","x":250,"y":140,"wires":[["638a99cd0d575a4e"]]},{"id":"cb470dd8032cfddf","type":"msg-speed","z":"4b211f23c08b43f4","name":"","frequency":"hour","interval":1,"estimation":false,"ignore":false,"pauseAtStartup":false,"topicDependent":false,"x":1130,"y":140,"wires":[["8284852709ca22a8"],[]]}]

```

I've added a 2nd speed count which clearly shows messages out of the function isn't 1-2-1 with messages in.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/d/cdfa17af522a2d8c7b7cc4e2e871c782240a43df.png)

---

<div class="post-metadata">

### Author: ![ChrisG](https://avatars.discourse-cdn.com/v4/letter/c/5f8ce5/32.png) [@ChrisG](https://discourse.nodered.org/u/ChrisG)
#### Post date: [20 December 2023 08:00 UTC](https://discourse.nodered.org/t/function-node-sends-multiple-messages-instead-of-one/83843/8 "2023-12-20T08:00:38Z")

</div>

Thanks Steve,

the extra debug nodes identified the issue. Taking the speed counters out of being in-line has resolved the issue.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/b/6b84673929ea3be0763c09ae53953d810d82558e.png)

---

<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: [20 December 2023 09:20 UTC](https://discourse.nodered.org/t/function-node-sends-multiple-messages-instead-of-one/83843/9 "2023-12-20T09:20:00Z")

</div>

> [@ChrisG](#):
>
> the extra debug nodes identified the issue.

For the future, the moral is that any time you have unexpected results, then use debug nodes to work backwards through the flow to find out which node is actually malfunctioning.

---

<div class="post-metadata">

### Author: ![ChrisG](https://avatars.discourse-cdn.com/v4/letter/c/5f8ce5/32.png) [@ChrisG](https://discourse.nodered.org/u/ChrisG)
#### Post date: [20 December 2023 10:09 UTC](https://discourse.nodered.org/t/function-node-sends-multiple-messages-instead-of-one/83843/10 "2023-12-20T10:09:40Z")

</div>

> [@Colin](#):
>
> For the future, the moral is that any time you have unexpected results, then use debug nodes to work backwards through the flow to find out which node is actually malfunctioning.

very true,  
malfunctioning? the documentation doesn't state the seen action either way, so perhaps 'not operating in the expected way'. 🙂

---

<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: [20 December 2023 10:26 UTC](https://discourse.nodered.org/t/function-node-sends-multiple-messages-instead-of-one/83843/11 "2023-12-20T10:26:10Z")

</div>

Malfunctioning is in the eye of the beholder.  
For you it was functioning badly. But yes, you are right, 'not as expected' is better.

---

<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 January 2024 10:27 UTC](https://discourse.nodered.org/t/function-node-sends-multiple-messages-instead-of-one/83843/12 "2024-01-03T10:27:02Z")

</div>

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