# Multiple Instances

**URL:** <https://discourse.nodered.org/t/multiple-instances/16316>\
**Category:** General\
**Created:** [4 October 2019 12:42 UTC](https://discourse.nodered.org/t/multiple-instances/16316 "2019-10-04T12:42:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![SynFisa](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@SynFisa](https://discourse.nodered.org/u/SynFisa)\
**Post date:** [4 October 2019 12:42 UTC](https://discourse.nodered.org/t/multiple-instances/16316/1 "2019-10-04T12:42:38Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![clickworkorange](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/clickworkorange/32/12109_2.png) [@clickworkorange](https://discourse.nodered.org/u/clickworkorange)\
**Post date:** [4 October 2019 12:57 UTC](https://discourse.nodered.org/t/multiple-instances/16316/2 "2019-10-04T12:57:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SynFisa](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@SynFisa](https://discourse.nodered.org/u/SynFisa)\
**Post date:** [4 October 2019 13:03 UTC](https://discourse.nodered.org/t/multiple-instances/16316/3 "2019-10-04T13:03:48Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![clickworkorange](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/clickworkorange/32/12109_2.png) [@clickworkorange](https://discourse.nodered.org/u/clickworkorange)\
**Post date:** [4 October 2019 13:06 UTC](https://discourse.nodered.org/t/multiple-instances/16316/4 "2019-10-04T13:06:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SynFisa](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@SynFisa](https://discourse.nodered.org/u/SynFisa)\
**Post date:** [4 October 2019 13:18 UTC](https://discourse.nodered.org/t/multiple-instances/16316/5 "2019-10-04T13:18:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![clickworkorange](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/clickworkorange/32/12109_2.png) [@clickworkorange](https://discourse.nodered.org/u/clickworkorange)\
**Post date:** [4 October 2019 13:19 UTC](https://discourse.nodered.org/t/multiple-instances/16316/6 "2019-10-04T13:19:32Z")

</div>

Try something like this:

![Screenshot_2019-10-04_14-16-55](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4829f48afa814338ede1da1c353011f8502a6ae7.png)

```auto
[
    {
        "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"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![SynFisa](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@SynFisa](https://discourse.nodered.org/u/SynFisa)\
**Post date:** [4 October 2019 13:23 UTC](https://discourse.nodered.org/t/multiple-instances/16316/7 "2019-10-04T13:23:49Z")

</div>

Ahh I understand, awesome!

Thanks a ton for your help I appreciate it.

---

<div class="post-metadata">

**Author:** ![clickworkorange](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/clickworkorange/32/12109_2.png) [@clickworkorange](https://discourse.nodered.org/u/clickworkorange)\
**Post date:** [4 October 2019 13:28 UTC](https://discourse.nodered.org/t/multiple-instances/16316/8 "2019-10-04T13:28:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SynFisa](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@SynFisa](https://discourse.nodered.org/u/SynFisa)\
**Post date:** [4 October 2019 13:35 UTC](https://discourse.nodered.org/t/multiple-instances/16316/9 "2019-10-04T13:35:08Z")

</div>

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