Function node sends multiple messages instead of one


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.

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.

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

I suspect your problem may be here

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

Mind you, I'm not familiar with this node

And there may well be a problem here too

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.

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:-
60/hour indicates it's receiving 1 per minute.

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


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):-

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 ```)

   code goes here 

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

See this post for more details - How to share code or flow json

Ah Steve,

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

[{"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.

Thanks Steve,

the extra debug nodes identified the issue. Taking the speed counters out of being in-line has resolved 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.

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

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

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