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.