Trigger Node Payload

I have just started playing around with Node-Red and I do want to send a payload for my trigger node that is reading a flow variable.

I just noticed that it is always sending a fixed payload.

Here is my test flow.

[
    {
        "id": "9bbc6f849e93661d",
        "type": "inject",
        "z": "6a63e1416914870e",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 220,
        "y": 700,
        "wires": [
            [
                "593edc7ef8978fec"
            ]
        ]
    },
    {
        "id": "593edc7ef8978fec",
        "type": "trigger",
        "z": "6a63e1416914870e",
        "name": "",
        "op1": "counter",
        "op2": "0",
        "op1type": "flow",
        "op2type": "str",
        "duration": "-5",
        "extend": false,
        "overrideDelay": false,
        "units": "s",
        "reset": "",
        "bytopic": "all",
        "topic": "topic",
        "outputs": 1,
        "x": 460,
        "y": 700,
        "wires": [
            [
                "fc4b9165f8cad5a3"
            ]
        ]
    },
    {
        "id": "fc4b9165f8cad5a3",
        "type": "function",
        "z": "6a63e1416914870e",
        "name": "function 6",
        "func": "var counter = flow.get(\"counter\") || 1\nnode.log(`Before ${counter}`);\n\ncounter = counter + 1;\nif (counter > 25) {\n    counter = 1;\n}\nflow.set(\"counter\", counter);\nnode.log(`After ${flow.get(\"counter\")}`);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 700,
        "y": 700,
        "wires": [
            [
                "ce8b082edb2d1a26"
            ]
        ]
    },
    {
        "id": "ce8b082edb2d1a26",
        "type": "debug",
        "z": "6a63e1416914870e",
        "name": "debug 27",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 940,
        "y": 700,
        "wires": []
    }
]

I have configured it to send every five seconds "flow.counter"

In my function, I am editing this flow.counter with this code

var counter = flow.get("counter") || 1
node.log(`Before ${counter}`);

counter = counter + 1;
if (counter > 25) {
    counter = 1;
}
flow.set("counter", counter);
node.log(`After ${flow.get("counter")}`);
return msg;

Not sure but it is always a fixed number like 1,3, 8, bla bla bla...

Maybe I am missing something basic.

The flow context is only read once when the trigger starts. So you can use a trigger node and after that a change node which sets the payload to the flow variable.

Ohh okay, so that's why it is reading only once.

Sorry, I might have been missing the point. Do you mean that after the function node, I should set the change node and set the output back to my trigger node?

[
    {
        "id": "9bbc6f849e93661d",
        "type": "inject",
        "z": "6a63e1416914870e",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 220,
        "y": 720,
        "wires": [
            [
                "593edc7ef8978fec"
            ]
        ]
    },
    {
        "id": "593edc7ef8978fec",
        "type": "trigger",
        "z": "6a63e1416914870e",
        "name": "",
        "op1": "counter",
        "op2": "0",
        "op1type": "flow",
        "op2type": "str",
        "duration": "-5",
        "extend": false,
        "overrideDelay": false,
        "units": "s",
        "reset": "",
        "bytopic": "all",
        "topic": "topic",
        "outputs": 1,
        "x": 460,
        "y": 720,
        "wires": [
            [
                "fc4b9165f8cad5a3"
            ]
        ]
    },
    {
        "id": "fc4b9165f8cad5a3",
        "type": "function",
        "z": "6a63e1416914870e",
        "name": "function 6",
        "func": "var counter = flow.get(\"counter\") || 1\nnode.log(`Before ${counter}`);\n\ncounter = counter + 1;\nif (counter > 25) {\n    counter = 1;\n}\nflow.set(\"counter\", counter);\nnode.log(`After ${flow.get(\"counter\")}`);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 700,
        "y": 720,
        "wires": [
            [
                "ce8b082edb2d1a26",
                "f26e04cdb46da6b6"
            ]
        ]
    },
    {
        "id": "ce8b082edb2d1a26",
        "type": "debug",
        "z": "6a63e1416914870e",
        "name": "debug 27",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 940,
        "y": 720,
        "wires": []
    },
    {
        "id": "f26e04cdb46da6b6",
        "type": "change",
        "z": "6a63e1416914870e",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "counter",
                "tot": "flow"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 560,
        "y": 640,
        "wires": [
            [
                "593edc7ef8978fec"
            ]
        ]
    }
]



Tried it like this but my code seems to be going in a loop.

No this makes no sense. If your intention is only to set a flow variable, which you do in your function node, then payload from the trigger node could be ignored. Set your payload to the counter, if you need this as output of your function node. The payload of the trigger node can be totally ignored, the trigger node should only trigger.

Can't I configure the trigger node to re-trigger and read my "flow.counter" again?

[
    {
        "id": "9bbc6f849e93661d",
        "type": "inject",
        "z": "6a63e1416914870e",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 240,
        "y": 720,
        "wires": [
            [
                "593edc7ef8978fec"
            ]
        ]
    },
    {
        "id": "593edc7ef8978fec",
        "type": "trigger",
        "z": "6a63e1416914870e",
        "name": "",
        "op1": "counter",
        "op2": "0",
        "op1type": "flow",
        "op2type": "str",
        "duration": "-5",
        "extend": false,
        "overrideDelay": false,
        "units": "s",
        "reset": "",
        "bytopic": "all",
        "topic": "topic",
        "outputs": 1,
        "x": 480,
        "y": 720,
        "wires": [
            [
                "fc4b9165f8cad5a3",
                "5f53673fc47d8647"
            ]
        ]
    },
    {
        "id": "fc4b9165f8cad5a3",
        "type": "function",
        "z": "6a63e1416914870e",
        "name": "function 6",
        "func": "var counter = flow.get(\"counter\") || 1\nnode.log(`Before ${counter}`);\n\ncounter = counter + 1;\nif (counter > 25) {\n    counter = 1;\n}\nflow.set(\"counter\", counter);\nnode.log(`After ${flow.get(\"counter\")}`);\nmsg.payload = counter\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 700,
        "y": 720,
        "wires": [
            [
                "f26e04cdb46da6b6"
            ]
        ]
    },
    {
        "id": "ce8b082edb2d1a26",
        "type": "debug",
        "z": "6a63e1416914870e",
        "name": "debug 27",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1100,
        "y": 720,
        "wires": []
    },
    {
        "id": "f26e04cdb46da6b6",
        "type": "change",
        "z": "6a63e1416914870e",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "counter",
                "tot": "flow"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 905,
        "y": 721,
        "wires": [
            [
                "ce8b082edb2d1a26"
            ]
        ]
    },
    {
        "id": "5f53673fc47d8647",
        "type": "debug",
        "z": "6a63e1416914870e",
        "name": "debug 28",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 700,
        "y": 640,
        "wires": []
    }
]

I wanted the function node to update my flow.counter and when the trigger node is re-run it will re-read the flow.counter?

I have added the change node after my function.

I am seeing the same payload when my trigger node is activated

As @mickym2 says you can ignore the trigger payload, as you recall it in the function. Where you place msg.payload = counter matters in the function. No change node required.
e.g.

var counter = flow.get("counter") || 1
//node.log(`Before ${counter}`);
msg.payload = counter
counter = counter + 1;
if (counter > 25) {
    counter = 1;
}
flow.set("counter", counter);
//node.log(`After ${flow.get("counter")}`);
//msg.payload = counter
return msg;

example flow

[{"id":"b20786e061512231","type":"inject","z":"da8a6ef0b3c9a5c8","name":"reset trigger","props":[{"p":"reset","v":"true","vt":"bool"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":290,"y":4640,"wires":[["593edc7ef8978fec"]]},{"id":"593edc7ef8978fec","type":"trigger","z":"da8a6ef0b3c9a5c8","name":"","op1":"counter","op2":"0","op1type":"flow","op2type":"str","duration":"-5","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":540,"y":4700,"wires":[["fc4b9165f8cad5a3","5f53673fc47d8647"]]},{"id":"9bbc6f849e93661d","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":300,"y":4700,"wires":[["593edc7ef8978fec"]]},{"id":"fc4b9165f8cad5a3","type":"function","z":"da8a6ef0b3c9a5c8","name":"function 6","func":"var counter = flow.get(\"counter\") || 1\n//node.log(`Before ${counter}`);\nmsg.payload = counter\ncounter = counter + 1;\nif (counter > 25) {\n    counter = 1;\n}\nflow.set(\"counter\", counter);\n//node.log(`After ${flow.get(\"counter\")}`);\n//msg.payload = counter\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":760,"y":4700,"wires":[["ce8b082edb2d1a26"]]},{"id":"5f53673fc47d8647","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 28","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":760,"y":4620,"wires":[]},{"id":"ce8b082edb2d1a26","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 27","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":900,"y":4700,"wires":[]}]
1 Like

If I use you code without the change node.

The payload of your trigger node can be ignored (debug28) - the payload is replaced by your function node. As i said earlier. The trigger node reads the flow context only once during start and will not reread it every time it triggers.

1 Like

Ok, thanks to both of you. I think I got your point.

I need to read more on the documentation.

May be it would be a useful feature request for repeating messages to reread msg, flow or global context each time the node triggers, then a workaround isn't needed. I ran into this trap as well as I thought the flow context would be read each time the node triggers. :wink:

Yes, as a first time node red user, my first assumption would be is that the trigger node would read the flow context variable.

I am actually thinking of calling an API down the flow that reacts to the input coming from the trigger node.

But as you have mentioned it is what it is. This is actually the first flow that I have created. I have just started today.

Thanks again for being helpful to newbies.

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