Creating flows based on node-red events?


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?)

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.

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.

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.

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.

1 Like

Could you elaborate on the stop event?

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

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?

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.

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

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

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.

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.

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

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?)

1 Like

Indeed! Greetings from Christchurch!!!

I'm slightly to the west of you.

Oxford? What are you using NR for?

(slightly further. Over the ditch.) :wink:

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. :frowning: But what else is it for - if not spending?

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.

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 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.

1 Like

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.

1 Like