# Creating flows based on node-red events?

**URL:** <https://discourse.nodered.org/t/creating-flows-based-on-node-red-events/40979>\
**Category:** General\
**Created:** [14 February 2021 15:46 UTC](https://discourse.nodered.org/t/creating-flows-based-on-node-red-events/40979 "2021-02-14T15:46:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![nzfarmer1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nzfarmer1/32/37190_2.png) [@nzfarmer1](https://discourse.nodered.org/u/nzfarmer1)\
**Post date:** [14 February 2021 15:46 UTC](https://discourse.nodered.org/t/creating-flows-based-on-node-red-events/40979/1 "2021-02-14T15:46:09Z")

</div>

Hi

Is there an input node event equivalent that can be configured to fire a flow on receipt of an internal node-red event?

The input node supports startup, but what about a tear down event (i.e. run a flow before a deploy is run to tear down services/flows nicely?)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [14 February 2021 21:00 UTC](https://discourse.nodered.org/t/creating-flows-based-on-node-red-events/40979/2 "2021-02-14T21:00:31Z")

</div>

Hi and welcome to the forum.

The `inject` node is there only to allow you to inject messages at your will.

What I think you are wanting to do is not that difficult, it is just you haven't quite got the picture of how things work. No big deal.

> [@nzfarmer1](#):
>
> i.e. run a flow before a deploy is run to tear down services/flows nicely?

Tear down services/flows nicely?

The idea is that `deploy` is just that. It starts all flows.

Let's say you have a couple of flows. One is this flow of which you speak and you want it to be run .... _to tear down services/flows_.

And the other is another flow doing _what ever_.

It (the second flow) does something of importance and then requires this other event to happen: _tear down services_.

The last node (or close to it) will be the one which _detects_ this thing/event/occurrence.

Connect it to the input of the first flow and that will then cause it to _run_.

---

<div class="post-metadata">

**Author:** ![nzfarmer1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nzfarmer1/32/37190_2.png) [@nzfarmer1](https://discourse.nodered.org/u/nzfarmer1)\
**Post date:** [14 February 2021 21:36 UTC](https://discourse.nodered.org/t/creating-flows-based-on-node-red-events/40979/3 "2021-02-14T21:36:01Z")

</div>

Am still somewhat confused by this.

The inject node has the option to trigger a flow say x seconds after the "start" event

But there is no complementary trigger a flow before a "stop" event.

When I click deploy, I am effectively restarting, so I want to give any nodes that require house keeping before ending a chance to perform this - for example flushing persistent data to a database or closing an open socket or stream.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [14 February 2021 21:46 UTC](https://discourse.nodered.org/t/creating-flows-based-on-node-red-events/40979/4 "2021-02-14T21:46:52Z")

</div>

While not a "stop event" you can _handle_ this by not doing full deploy or not editing flows during production.

Custom nodes (where they need to) have close events handlers for this reason.

There are other tactics for flushing data e.g. persistent storage (that flushes on node-red shutdown). And you could always have a maintenance routines that you could trigger to do your flushes etc.

Then there is the function node, the close script is executed on closing.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [14 February 2021 21:46 UTC](https://discourse.nodered.org/t/creating-flows-based-on-node-red-events/40979/5 "2021-02-14T21:46:52Z")

</div>

Could you elaborate on the `stop` event?

There isn't such a thing in Node-Red of which I know.

---

<div class="post-metadata">

**Author:** ![nzfarmer1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nzfarmer1/32/37190_2.png) [@nzfarmer1](https://discourse.nodered.org/u/nzfarmer1)\
**Post date:** [14 February 2021 21:54 UTC](https://discourse.nodered.org/t/creating-flows-based-on-node-red-events/40979/6 "2021-02-14T21:54:49Z")

</div>

Prior to a flow being restarted, I am hoping that there is an event - i.e. a "stop" or "close" event, fired from node-red that can be trapped and used in a sort of inject node such that if there is a house keeping/tear down flow, this can be run prior to the session state being lost?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [14 February 2021 21:57 UTC](https://discourse.nodered.org/t/creating-flows-based-on-node-red-events/40979/7 "2021-02-14T21:57:51Z")

</div>

Ok, (sorry @Steve-Mcl I am not trying to compete with you for who has the best answer)

_Restarting_..... Ok.

So you want to run a flow and each time it is run: previous data is wiped and only data from this session is valid/used.

Then all you would need to do is have a "New session" button and it wipes all changeable variables.

I say that only because there may be some you want to keep.

---

<div class="post-metadata">

**Author:** ![nzfarmer1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nzfarmer1/32/37190_2.png) [@nzfarmer1](https://discourse.nodered.org/u/nzfarmer1)\
**Post date:** [14 February 2021 21:57 UTC](https://discourse.nodered.org/t/creating-flows-based-on-node-red-events/40979/8 "2021-02-14T21:57:51Z")

</div>

This sounds what I want. Do you have an example of the "close" script of a function node?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [14 February 2021 21:59 UTC](https://discourse.nodered.org/t/creating-flows-based-on-node-red-events/40979/9 "2021-02-14T21:59:13Z")

</div>

Well, it is a `function` node and you edit this page/part of it.

 ![Screenshot from 2021-02-15 08-58-14](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/0/90e55578b8628884382f43525ff179869cdab6be.png)

---

<div class="post-metadata">

**Author:** ![nzfarmer1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nzfarmer1/32/37190_2.png) [@nzfarmer1](https://discourse.nodered.org/u/nzfarmer1)\
**Post date:** [14 February 2021 21:59 UTC](https://discourse.nodered.org/t/creating-flows-based-on-node-red-events/40979/10 "2021-02-14T21:59:30Z")

</div>

Close. I want to be able to run a house keeping flow that may do one of a number of things - and flushing volatile memory to a persistent store is an obvious use case.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [14 February 2021 22:01 UTC](https://discourse.nodered.org/t/creating-flows-based-on-node-red-events/40979/11 "2021-02-14T22:01:40Z")

</div>

But (to me) that isn't **close**. That is **wipe**.

But enough of the pedanticness.

Forget about the `setup` and `close`.

Just put it in the normal part of the node and attach a `button` to the input and when you want to start a new _session_, press the button.

---

<div class="post-metadata">

**Author:** ![nzfarmer1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nzfarmer1/32/37190_2.png) [@nzfarmer1](https://discourse.nodered.org/u/nzfarmer1)\
**Post date:** [14 February 2021 22:01 UTC](https://discourse.nodered.org/t/creating-flows-based-on-node-red-events/40979/12 "2021-02-14T22:01:52Z")

</div>

Funny. I've used dozens of function nodes and never seen this tab. sigh. Much thanks!

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [14 February 2021 22:02 UTC](https://discourse.nodered.org/t/creating-flows-based-on-node-red-events/40979/13 "2021-02-14T22:02:42Z")

</div>

Make sure you have the latest version. I'm not sure in which is was introduced.

(Would a kia ora bro! be in the realm of possibles?)

---

<div class="post-metadata">

**Author:** ![nzfarmer1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nzfarmer1/32/37190_2.png) [@nzfarmer1](https://discourse.nodered.org/u/nzfarmer1)\
**Post date:** [14 February 2021 22:11 UTC](https://discourse.nodered.org/t/creating-flows-based-on-node-red-events/40979/14 "2021-02-14T22:11:02Z")

</div>

Indeed! Greetings from Christchurch!!!

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [14 February 2021 22:11 UTC](https://discourse.nodered.org/t/creating-flows-based-on-node-red-events/40979/15 "2021-02-14T22:11:39Z")

</div>

I'm slightly to the west of you.

---

<div class="post-metadata">

**Author:** ![nzfarmer1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nzfarmer1/32/37190_2.png) [@nzfarmer1](https://discourse.nodered.org/u/nzfarmer1)\
**Post date:** [14 February 2021 22:12 UTC](https://discourse.nodered.org/t/creating-flows-based-on-node-red-events/40979/16 "2021-02-14T22:12:29Z")

</div>

Oxford? What are you using NR for?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [14 February 2021 22:15 UTC](https://discourse.nodered.org/t/creating-flows-based-on-node-red-events/40979/17 "2021-02-14T22:15:18Z")

</div>

(slightly further. _Over the ditch_.) 😉

What I am using NR for? Lots of stuff. For now just monitoring things.

I am slightly expanding it to automate plant watering - kind of.  
That is done by an Arduino but I use NR to control the power to it and weather/rain detection.

Then there is ..... well, not much else just now. I am in flux with a lot of other things just now.

Building a new computer is one and it is a lot of _bugs bunny_. ☹_But what else is it for - if not spending?_

---

<div class="post-metadata">

**Author:** ![nzfarmer1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nzfarmer1/32/37190_2.png) [@nzfarmer1](https://discourse.nodered.org/u/nzfarmer1)\
**Post date:** [14 February 2021 22:17 UTC](https://discourse.nodered.org/t/creating-flows-based-on-node-red-events/40979/18 "2021-02-14T22:17:10Z")

</div>

Haha. I used to live in oz, but am a kiwi now.

NR for me is also for watering plants - well misting them, but also handing fans, co2, resevoir levels, ph, ec, voltage, power prices, LED lights and well, pretty much the whole shebang.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [14 February 2021 22:22 UTC](https://discourse.nodered.org/t/creating-flows-based-on-node-red-events/40979/19 "2021-02-14T22:22:25Z")

</div>

That's where I am hoping to go - kind of.

Anyway, back to what you were asking.......

Honestly though:  
Set up your flow as needed. Set the variables. Check it is all working.

Note the variables that need to be wiped.

Put all those variable names in the `function` node and wipe them with the code in that node.

I'm still stuck/confused what the _trigger_ is for this to happen?  
Is it a specific flow or a whole lot of flows?  
Why?

Why I am asking is to see if I can better understand what is the _start_ of the cycle, and what is the _end_.

All you do then is have a bit of the flow to detect the start, send that to the `function` node to wipe the previous stuff, (maybe a small `delay` node after that), then send the signal to the node which starts the collection of data.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [14 February 2021 22:29 UTC](https://discourse.nodered.org/t/creating-flows-based-on-node-red-events/40979/20 "2021-02-14T22:29:28Z")

</div>

Whilst the close code in the function node will let you do _something_, you can't use that to trigger a flow. If the runtime is stopping, it's stopping. There's no waiting for a flow to complete first.

There is some work in progress around being able to more gracefully stop a flow, but again, that is not quite the same thing as triggering a new flow and waiting for it to complete.

[Next page](https://discourse.nodered.org/t/creating-flows-based-on-node-red-events/40979.md?page=2)
