Multiple Instances

I'm not sure how the node's work in terms of instancing.

If i have an event that triggers a chain, and that path has some loops and time interval stuff, if a new event comes in, how do i make it not affect the state of the ongoing process?

More specifically, I have an event that triggers on camera motion, then when that happens, i want to do some work over the next 10 seconds. However when another event comes in, it seems to just follow the same state as the previous one one. I'd like it to be able to have its own instance of the node path, so it has its own data and its own 10 seconds operating logic. Is that possible? Or does it already work this way and I misunderstand?

It's not entirely clear from your post what your flow does and what the problem is, but you could give your messages some known property that you can use to differentiate them - for example a msg.timestamp. You can check against this in the code which runs the cycle. If you need to attach some data that a message will carry along with it just add it as another msg.property.

So my flow is like this:

Camera Detected Motion > Loop for once per second for 15 seconds -> Take a picture once a second and analyse it -> take action based on analysis.

I want to be able to have multiple loops in flight. Right now it seems if i reach the "loop for 15 seconds" node, it ignores the input, rather than it having its own 15 seconds loop with its own incoming data.

Specifically, i'm using looptimer node type. I don't know if its a node-specific behavior or if this is how node-red. Does a flow exist in multiple instances when or is there only one flow and its aware of its previous state? Can i make it early on fork its on virtual instance?

Ok, one way to achieve that could be to have something like a msg.iteration property on each message, and increase that by 1 each time the message passes through your function node - once the value hits 15 stop passing the message along.

How does that result in the next detection (that could happen seconds late) have its own 15 loops through?

Try something like this:

Screenshot_2019-10-04_14-16-55

[
    {
        "id": "7f5668f1.8a144",
        "type": "inject",
        "z": "88c9e740.7493a",
        "name": "",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 200,
        "y": 180,
        "wires": [
            [
                "a223e8d9.45d0f"
            ]
        ]
    },
    {
        "id": "b0d57be3.75a6e",
        "type": "debug",
        "z": "88c9e740.7493a",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "iteration",
        "targetType": "msg",
        "x": 610,
        "y": 180,
        "wires": []
    },
    {
        "id": "9a0d74ba.c08a78",
        "type": "delay",
        "z": "88c9e740.7493a",
        "name": "",
        "pauseType": "delay",
        "timeout": "1",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "x": 400,
        "y": 160,
        "wires": [
            [
                "a223e8d9.45d0f"
            ]
        ]
    },
    {
        "id": "a223e8d9.45d0f",
        "type": "function",
        "z": "88c9e740.7493a",
        "name": "Iterate",
        "func": "if(typeof msg.iteration === \"undefined\") {\n    msg.iteration = 1;\n} else {\n    msg.iteration++;\n}\nif(msg.iteration <= 15) {\n    return msg;\n}\n",
        "outputs": 1,
        "noerr": 0,
        "x": 400,
        "y": 220,
        "wires": [
            [
                "9a0d74ba.c08a78",
                "b0d57be3.75a6e"
            ]
        ]
    }
]
1 Like

Ahh I understand, awesome!

Thanks a ton for your help I appreciate it.

1 Like

My pleasure! I was going to say, if you change the debug node to show the entire message, you can see that for each time you click on the inject node, a separate cycle will be triggered: compare the timestamps in the msg.payload and you'll see that each message does 15 rounds.

Yeah it works, and integrated it smoothly. I can see the feeds independently processing now. Thanks again :slight_smile:

1 Like