# Inject node trigger behavior

**URL:** <https://discourse.nodered.org/t/inject-node-trigger-behavior/76902>\
**Category:** General\
**Created:** [24 March 2023 11:08 UTC](https://discourse.nodered.org/t/inject-node-trigger-behavior/76902 "2023-03-24T11:08:35Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![WhiteLion](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/whitelion/32/12266_2.png) [@WhiteLion](https://discourse.nodered.org/u/WhiteLion)\
**Post date:** [24 March 2023 11:08 UTC](https://discourse.nodered.org/t/inject-node-trigger-behavior/76902/1 "2023-03-24T11:08:35Z")

</div>

Hiho 🙂  
I ve a problem question with the inject node:  
When my system reboots I use a bunch of these nodes to trigger a initial activities of the flows or load variables. Therefor I use "one time injection after xx seconds". I set the seconds to time the sequence like lower should start earlier and higher values start later. This is because some flows are based on values which are set by a flow that shout start earlier. Now I ran into trouble with this because:  
The more the project grows the more time it needs to start on a weak system like a raspberry pi its very notable. Somehow then the injection node does not start at all or at least is not set to a valid sequence.  
example:  
How it should be:  
Flow 1: Injection node starts 10 seconds after node red boot - \> sets value for Flow 2  
Flow 2: Injection node starts 11 seconds after node red boot - \> sets value for Flow 3  
Flow 3: Injection node starts 12 seconds after node red boot - \> sets value for Flow 4  
startup finished.

How it seems to be:  
Flow 1: Injection node starts 10 seconds after node red boot - \> Fails to sets value for Flow 2  
(because project has grown and startup time increases / is skipped)  
Flow 2: Injection node starts 11 seconds after node red boot - \> failed because missing variable of flow 1 or using useless default value  
Flow 3: Injection node starts 12 seconds after node red boot - \> failed because missing variable of flow 2 or using useless default value

How I expected it be:  
startup takes longer:  
put all to a queue and trigger them in the sequence like I set the seconds value and start them with delay. Or maybe at least add an option for that to the injection node if this behavior is for reasons not wanted as default.

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [24 March 2023 11:19 UTC](https://discourse.nodered.org/t/inject-node-trigger-behavior/76902/2 "2023-03-24T11:19:26Z")

</div>

May I propose another option?

You could use just one (1) `inject` node ... and connect the other flows with a `link out` - `link in` sequence. This ensures that the initialization of the dependent flow runs when the predecessor has finished its doing...

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [24 March 2023 11:27 UTC](https://discourse.nodered.org/t/inject-node-trigger-behavior/76902/3 "2023-03-24T11:27:32Z")

</div>

> [@WhiteLion](#):
>
> Flow 1: Injection node starts 10 seconds after node red boot - \> sets value for Flow 2

Additional remark: The timer starts ticking when Node-RED instantiates the `inject` node. There's no reference to the moment in time when Node-RED booted. 10 seconds sound like an enormous span - even on Pi systems. How big is you project?

The issue here could rather be that the sequence of initializing the flows might be to dense; thus it could help perhaps to increase the span between injecting into flow#1 & flow#2 to more than one second.

---

<div class="post-metadata">

**Author:** ![WhiteLion](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/whitelion/32/12266_2.png) [@WhiteLion](https://discourse.nodered.org/u/WhiteLion)\
**Post date:** [24 March 2023 11:41 UTC](https://discourse.nodered.org/t/inject-node-trigger-behavior/76902/4 "2023-03-24T11:41:52Z")

</div>

danke für deine schnelle antwort 🙂

my current project is not huge (I think) but that s much about the definition of huge. I would say it has about 1000 nodes (400 inactive debug maybe 400 function rest exec, some less gui stuff). I am running this on a Raspberry Pi 3A+ with only 512MB of ram. htop shows this:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/8/a866e87f622e8f13d6b704be5681196790886e36.png)

I have a much bigger project running on the Raspberry Pi 4 with 4 GB and have to set the injection node time to about 60 seconds there.

btw node red should have a stats view to show amount of flows / nodes ...aso.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [24 March 2023 11:45 UTC](https://discourse.nodered.org/t/inject-node-trigger-behavior/76902/5 "2023-03-24T11:45:41Z")

</div>

If flows must be triggered in sequence, you can use the `complete` node

```auto
[{"id":"c97088bddf5f31b8","type":"inject","z":"09deee63c1b960f7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":300,"y":2520,"wires":[["4026c2c46cbf2480"]]},{"id":"4026c2c46cbf2480","type":"delay","z":"09deee63c1b960f7","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":460,"y":2520,"wires":[["955cca5606224839"]]},{"id":"955cca5606224839","type":"change","z":"09deee63c1b960f7","name":"delaytrigger","rules":[{"t":"set","p":"triggerDelay","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":2520,"wires":[["b13e8b0ab9436e8f"]]},{"id":"e802a8c131c36fed","type":"complete","z":"09deee63c1b960f7","name":"","scope":["a09166e79e3d80e2"],"uncaught":false,"x":290,"y":2580,"wires":[["164bf11e64b44ddf"]]},{"id":"164bf11e64b44ddf","type":"debug","z":"09deee63c1b960f7","name":"debug complete","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":480,"y":2580,"wires":[]},{"id":"b13e8b0ab9436e8f","type":"function","z":"09deee63c1b960f7","name":"function 1","func":"node.warn('function 1')\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":780,"y":2520,"wires":[["28b16a50c2c61783"]]},{"id":"28b16a50c2c61783","type":"delay","z":"09deee63c1b960f7","name":"short delay","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":930,"y":2520,"wires":[["a09166e79e3d80e2"]]},{"id":"a09166e79e3d80e2","type":"function","z":"09deee63c1b960f7","name":"function 2","func":"node.warn('function 2')\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1080,"y":2520,"wires":[[]]}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/0/004554275b6be08c5ec2a8dd013a5b2cd7d0cb67.png)

NB! not all nodes fire the complete event so that should be checked out but most of them do.

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [24 March 2023 11:57 UTC](https://discourse.nodered.org/t/inject-node-trigger-behavior/76902/6 "2023-03-24T11:57:32Z")

</div>

> [@WhiteLion](#):
>
> about 1000 nodes

> [@WhiteLion](#):
>
> Raspberry Pi 3A+ with only 512MB of ram

That sounds like a nasty torture for this tiny system. 😉  
Anyway: Whenever there's a dependency - as in the case you described - I follow the practice to model it as a (solid & reliable) dependency... especially in such scenarios with impacted predictability.

---

<div class="post-metadata">

**Author:** ![WhiteLion](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/whitelion/32/12266_2.png) [@WhiteLion](https://discourse.nodered.org/u/WhiteLion)\
**Post date:** [24 March 2023 13:10 UTC](https://discourse.nodered.org/t/inject-node-trigger-behavior/76902/7 "2023-03-24T13:10:33Z")

</div>

> [@hotNipi](#):
>
> NB! not all nodes fire the complete event so that should be checked out but most of them do.

Thx. I will test that out. 👍

> [@ralphwetzel](#):
>
> That sounds like a nasty torture for this tiny system. 😉  
> Anyway: Whenever there's a dependency - as in the case you described - I follow the practice to model it as a (solid & reliable) dependency... especially in such scenarios with impacted predictability.

thx 4 your hints. I still have to spend a lot of work to fetch that goal I think...

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [24 March 2023 15:39 UTC](https://discourse.nodered.org/t/inject-node-trigger-behavior/76902/8 "2023-03-24T15:39:30Z")

</div>

> [@WhiteLion](#):
>
> btw node red should have a stats view to show amount of flows / nodes ...aso.

Take a look at this flow. You have to change the name of the file to read in.  
On my little test machine it returns this

 ![Untitled 5](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/7/c77fb5d865a09eff59007036a28bc68d7adc9ffe.jpeg)

 ![Untitled 6](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/0/6095bfcadc4da7da91502f8a1e8d58fa6a3df94a.jpeg)

```auto
[{"id":"bcd4bc8e5fa549df","type":"file in","z":"72e3ac42d134d33c","name":"","filename":"/home/pi/.node-red/flows.json","filenameType":"str","format":"utf8","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":300,"y":100,"wires":[["5989a3158b6f6489"]]},{"id":"5989a3158b6f6489","type":"json","z":"72e3ac42d134d33c","name":"","property":"payload","action":"","pretty":false,"x":490,"y":100,"wires":[["d3a7a822b092f32d"]]},{"id":"f492b248145e9d02","type":"inject","z":"72e3ac42d134d33c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":100,"wires":[["bcd4bc8e5fa549df"]]},{"id":"d3a7a822b092f32d","type":"function","z":"72e3ac42d134d33c","name":"Count element types","func":"// Count different node types from the flows file.\n// Change the definition of counts{} below to modify which element types are counted.\n// Could usefully be tweeked to count subflows, I don't use them.\nlet counts = { // names ending with 's' are handled differently below. The rest are valid values of element.type \n \"nodes\": 0, \"tab\": 0, \"inject\": 0, \"exec\": 0, \"function\": 0, \"group\": 0,\n \"links_in_out\": 0, \"debug\": 0, \"ui_nodes\": 0, \"ui_tab\": 0, \"ui_group\": 0, \n}\n\nfor (let i in msg.payload) {\n let e = msg.payload[i]\n if (e.hasOwnProperty(\"x\") && e.type !== \"group\" && e.type != \"junction\") { //* consider a node = an element visible on editor ('x') except group & junction \n counts.nodes++\n }\n if (e.type.includes(\"ui_\") && e.type != \"ui_tab\" && e.type != \"ui_group\" && e.type != \"ui_base\") { // ui nodes \n counts.ui_nodes++\n }\n if (e.type == \"link in\" || e.type == \"link out\" || e.type == \"link call\") { // Links: in, out and call \n counts.links_in_out ++\n }\n if (counts.hasOwnProperty(e.type)) { // anything else has to be in the declaration of counts{} above.\n counts[e.type] ++\n }\n}\n\nmsg.payload = counts\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":100,"wires":[["f2eb9d7a2c8aa5dd"]]},{"id":"f2eb9d7a2c8aa5dd","type":"debug","z":"72e3ac42d134d33c","name":"debug 293","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":850,"y":100,"wires":[]}]

```

Edit: tweeked and commented the script.  
If anyone who uses subflows or alternative dashboards is interested in this, you might want to consider what elements should be counted for those features and adjust it.  
It won't work for anyone using Projects.

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [25 March 2023 01:09 UTC](https://discourse.nodered.org/t/inject-node-trigger-behavior/76902/9 "2023-03-25T01:09:39Z")

</div>

Humm,

I can see this leading to _ **"my flows are bigger than yours"** _ 🙂

---

<div class="post-metadata">

**Author:** ![WhiteLion](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/whitelion/32/12266_2.png) [@WhiteLion](https://discourse.nodered.org/u/WhiteLion)\
**Post date:** [25 March 2023 09:19 UTC](https://discourse.nodered.org/t/inject-node-trigger-behavior/76902/10 "2023-03-25T09:19:18Z")

</div>

> [@jbudd](#):
>
> Take a look at this flow. You have to change the name of the file to read in.  
> On my little test machine it returns this

Thx nice !!

> [@Sean-McG](#):
>
> I can see this leading to _ **"my flows are bigger than yours"** _

Challenge accepted:  
The project I am talking about (Raspi 3A+):  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/6/666056df395f4e0da08cc5b30672b1247967a59a.png)

My home automation project (Raspi 4 4GB):  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/d/2d139cf757917766aaeb00fb32be8300fd9bd799.png)

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [25 March 2023 15:40 UTC](https://discourse.nodered.org/t/inject-node-trigger-behavior/76902/11 "2023-03-25T15:40:40Z")

</div>

Obviously you need to write more efficient code in your function nodes 😉 🤣 🤣 🤣

---

<div class="post-metadata">

**Author:** ![WhiteLion](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/whitelion/32/12266_2.png) [@WhiteLion](https://discourse.nodered.org/u/WhiteLion)\
**Post date:** [25 March 2023 18:01 UTC](https://discourse.nodered.org/t/inject-node-trigger-behavior/76902/12 "2023-03-25T18:01:27Z")

</div>

All function nodes are highly optimized 🤣 🤣 🤣

you can easy compare it to native assembly code

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [24 May 2023 18:01 UTC](https://discourse.nodered.org/t/inject-node-trigger-behavior/76902/13 "2023-05-24T18:01:52Z")

</div>

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