Auto Redeploy When OPC Connection Lost

Occasionally our node-red-contrib-opcua client nodes will disconnect (usually when downloading a new program to the PLC) and then not be able to reconnect without deploying the flow again. Is there a way to, based on that connection status, automatically re-deploy our node red flow if the connection is not established for a certain period of time?

Either re-deploy OR just simply restart the node-red service as a whole...

you can run a system execute "exec" node to run a batch file which can then shut down your service and also restart it. net stop <servicename> and then net start <servicename>. You can look up how to run Node-RED as a service Running Node-RED as a service on Windows using NSSM · GitHub.

disclaimer: This assumes Windows OS.

To solve that bit use a trigger node set to send nothing initially, and send a message to run the restart after a time. Feed that from a status node attached to the opc node. In addition configure the Trigger node to reset if msg.payload is "Connected" or whatever the status value is when it is connected. Then the trigger will start timing when the status goes offline, but if it comes online it will reset the trigger and stop the restart message from being sent.

1 Like

I am running on a Raspberry Pi, I should have mentioned that in my original post. Can I use the exec node and just use "sudo systemctl restart nodered" or something like that as the command?

Yes, except that you need to tell the OS to allow the node red user to use that command without having to enter a password. If you run that command in a terminal when logged on as the node-red user (which will be pi unless you have changed it) does it ask for a password? If it doesn't ask then it should run ok in an exec node, except you might need to use /bin/systemctl as the PATH may not be setup when run in node-red.
If it does ask for a password then you can usesudo visudo which edits the sudoers file and add on the end
pi ALL=(ALL) NOPASSWD: /bin/systemctl
which tells it to allow the pi user to use the command without a password. visudo will use vi by default as the editor. If you don't use vi then you might need to google how to save and exit after you add the line. You can tell it to use nano by default instead of vi but I don't bother, I just have a quick google to remind myself how to do it.

Ok awesome, thank you. The command works when I inject it manually. The only thing is when I use the status node to read the status of one of the OPCUA Client nodes, it just spits undefined even though there is a "Connected" status below it with the green dot. Is there some further configuration I need to do with the status command?

Do you mean you have configured a status node linked to the opc node and connected it directly to a debug node and the debug node shows undefined?

Yes that is what I was currently trying in terms of the status node... Would it be sufficient to use the trigger node and connect it to a variable in the OPC that I have acting as a heartbeat, sending true/false alternating and then when I don't see anything for ~60 sec (or whatever the time be) to then send the command to the exec node?

I don't think what you describe is possible, as the mechanism the node uses to show the status should automatically be picked up by the status node. Can you export just the opc node, the status node and the debug node so I can have a look?

Yes, you could use a variable value as the heartbeat instead. I would like to understand the status node problem so you send the flow anyway please?

Below is those three nodes. Also attached is a screenshot of what I am seeing...


I'm an idiot...realizing that im debugging msg.payload

Me too. I had forgotten that the Status node passes on msg.status :slight_smile:

1 Like

download the node source code, modify accordingly and re-publish i guess. With the recent improvements in node-red you may not need a re-publish at all. Function node can do the same.

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