Hi all, thought I'd quickly share this short how-to on using the new node.on close and node.send features in a function node.
I have a number of things that I want to set up when NR starts so I already had 3 nodes to do this:
inject (set to run once at startup) -> function (containing startup tasks) -> debug (telling me startup completed)
I now changed this to include the following in the function node:
msg.topic = "Starting";
msg.payload = "Startup completed";
node.on('close',function() {
msg.topic = "Closing";
msg.payload = "Closing down now";
node.send(msg);
});
return msg;
With a further slight amendment, you can do some additional work as the system closes. Set the function to have two outputs and:
node.on('close',function() {
msg.topic = "Closing";
msg.payload = "Closing down now";
node.send([null, msg]);
});
return [msg, null];
In each case, the return command triggers a message on startup and the on close triggers a message when shutting down. However, it should be noted that you are very limited as to what you can do during shutdown. For example, I tried to trigger an MQTT output node from the closing output and it doesn't execute it, a debug output node works however.
Hope this is helpful.
Julian.