Restart flows using Node Red API

Hello

Please can somebody help with the working example of how to restart flows using API.

Link to Node Red documentation

I found a similar topic marked as solved, but I cannot really see the solution there.

Link to similar topic

I can use GET to retrieve the information about the flows and to get info about the specific flow.
I don't know how to structure and use this information to redeploy the flow.

My flows

[{"id":"38ebc8f0.806f88","type":"tab","label":"My Flow","disabled":false,"info":""},{"id":"10a7b362.152b9d","type":"inject","z":"38ebc8f0.806f88","name":"","topic":"Test","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":160,"wires":[["5dfc6842.05b878"]]},{"id":"5dfc6842.05b878","type":"change","z":"38ebc8f0.806f88","name":"","rules":[{"t":"set","p":"flowStatus","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":160,"wires":[["b34ba5fc.6d6518"]]},{"id":"b34ba5fc.6d6518","type":"http request","z":"38ebc8f0.806f88","name":"Get flows","method":"GET","ret":"obj","url":"http://localhost:1880/flows","tls":"","x":540,"y":160,"wires":[["95b8fef6.88cf7"]]},{"id":"95b8fef6.88cf7","type":"change","z":"38ebc8f0.806f88","name":"get flowId by name","rules":[{"t":"set","p":"flowId","pt":"msg","to":"*[type = 'tab' and label = $$.topic].id","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":750,"y":160,"wires":[["11379e70.7874d2"]]},{"id":"11379e70.7874d2","type":"http request","z":"38ebc8f0.806f88","name":"Get single flow","method":"GET","ret":"obj","url":"http://localhost:1880/flow/{{flowId}}","tls":"","x":960,"y":160,"wires":[["44b07581.39fe3c"]]},{"id":"44b07581.39fe3c","type":"debug","z":"38ebc8f0.806f88","name":"get single flow","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1140,"y":160,"wires":[]},{"id":"c16580ad.02c2d","type":"tab","label":"Test","disabled":false,"info":""},{"id":"7a75794c.af1c18","type":"inject","z":"c16580ad.02c2d","name":"","topic":"","payload":"","payloadType":"date","repeat":"5","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":240,"wires":[["591040.bf77cfc"]]},{"id":"591040.bf77cfc","type":"debug","z":"c16580ad.02c2d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":470,"y":240,"wires":[]}]

EDIT 1.

I still don't know how to structure the json object and how to use it.
This is what I have for my header:

{
    "Content-type": "application/json",
    "Node-RED-Deployment-Type": "full", 
    "Node-RED-API-Version": "v2"  //I'm not sure about this 
}

where I should put this:

{
    "rev": "abc-123", // I don't know what goes here 
    "flows": [
      {
        "type": "tab",
        "id": "38ebc8f0.806f88c",
        "label": "My Flow"
      }
    ]
}

Finally how to post this to restart flow?

http://localhost:1880/flows/{{headers}}

EDIT 2 - SOLUTION

Another day another attempt.

this is what's inside my function node

msg.url = 'http://localhost:1880/flows';
msg.method = 'POST';
msg.headers = {};
msg.headers['content-type'] = "application/json; charset=utf-8";
msg.headers['Node-RED-Deployment-Type'] = "reload";
msg.headers['Node-RED-API-Version'] =  "v2";
msg.payload = {
                           "flows": [ { } ]
};
return msg;

test flow:

[{"id":"9755354.1a5d8c8","type":"tab","label":"Test"},{"id":"45b295b1.0045cc","type":"function","z":"9755354.1a5d8c8","name":"request","func":"msg.url = 'http://localhost:1880/flows';\nmsg.method = 'POST';\nmsg.headers = {};\nmsg.headers['content-type'] = \"application/json; charset=utf-8\";\nmsg.headers['Node-RED-Deployment-Type'] = \"reload\";\nmsg.headers['Node-RED-API-Version'] =  \"v2\";\nmsg.payload = {\n    \"flows\": [\n        {\n            \"type\": \"tab\",\n            \"id\": \"9755354.1a5d8c8\",\n            \"label\": \"Test\"\n        }\n    ]\n};\nreturn msg;","outputs":1,"noerr":0,"x":260,"y":60,"wires":[["cd0b9552.5f3be8"]]},{"id":"2a590784.f7a868","type":"debug","z":"9755354.1a5d8c8","name":"results","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":610,"y":60,"wires":[]},{"id":"38dbceb9.0b5482","type":"inject","z":"9755354.1a5d8c8","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":60,"wires":[["45b295b1.0045cc"]]},{"id":"cd0b9552.5f3be8","type":"http request","z":"9755354.1a5d8c8","name":"","method":"use","ret":"txt","paytoqs":false,"url":"","tls":"","persist":false,"proxy":"","authType":"","x":450,"y":60,"wires":[["2a590784.f7a868"]]},{"id":"4fd571c0.a666f","type":"inject","z":"9755354.1a5d8c8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":"1","x":170,"y":140,"wires":[["f1187019.7b86b"]]},{"id":"f1187019.7b86b","type":"debug","z":"9755354.1a5d8c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":420,"y":140,"wires":[]}]

After triggering the request Test flow is reloaded and restarted and the timestamp will appear in the debug window 1 second later.

3 Likes

Thanks it worked for me...do you know if it is possible to just reload a single Flow?

Hi, I'm not sure if this is possible.
Accordingly to the documentation, the default reload behaviour is to reload all flows. I thought that maybe by adding flow detail inside the

{
    "flows": [
        {
            "type": "tab",
            "id": "ffab00e3.ca281",  //ID of the flow to restart
            "label": "Test2"  //Name of the flow to restart
        }
    ]
};

will restart only specified flow, but it doesn't work that way.

Maybe someone who knows more about Node-Red will shed some light on this subject.

Yes. This was my first thought too. Maybe its a workaround, but you can get every node with config, if you use the API. Also youre able to set Configurations for one flow...acording to the API, you can get the current config, edit one node just a little bit and save it. Then you trigger the API to redeploy only changed flows...there you are...but i did not test it :joy: Yet

I think that a few examples should be added to the documentation for people like myself that only start programming.
It looks like this is quite powerful API allowing for flows modification, but not very well explained (in my opinion).

I use pm2 to manage my NR processes. Let me know if anyone is interested to know more.

Can this be used to restart one flow only as @MrDrache333 suggested?

This question was also asked a few days ago in another thread, to which I gave this reply: Automatically restart one flow (not all flows) - #3 by knolleary

I'm not sure where pm2 comes into it - perhaps if you were to run each flow in its own instance of Node-RED, you'd be able to restart the relevant instance. However you then have the issue of managing multiple instances.

pm2 is used to restart the whole NR process, not one particular flow (the original ques asks to restart all flows, not a single one).

I use pm2 for one big reason: my NR process runs inside a docker container, so when my users upgrade a node, I dont want to restart the container itself (its time consuming). Instead, I restart the process using pm2.
There are other benefits like automatic restarts, monitoring etc.

The solution shown above works for me. But i still have the problem that the restart should only be started once. Only after the raspberry has been restarted. Since I am a beginner, I can't think of a solution at the moment.
Can I get a little help here?

Hi @easyE4 .
If you still looking for solution. Please can you quote the solution you are referring to?
Is it a use of the PM2 or the example I posted?
The flows will be restarted after the RPI reboot. I'm assuming you would like to restart them again, but only once after node red starts. You can use the inject node as a trigger and use "Inject once after" option

The restart may be also triggered by the event.

Please try to be more specific about your case.

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