QNimbus / node-red-contrib-openhab-v2

MIT License
9 stars 3 forks source link

Reconnect after losing connection #13

Closed radokristof closed 4 years ago

radokristof commented 5 years ago

Hi everyone!

These nodes have been working correctly for me, no big problems with it. The only problem is that, it won't reconnect to the host when it comes back online. For example Node-RED starts faster than openHAB (until the REST API available) and the openHAB nodes doesn't really want to show/get states from openHAB until I restart Node-RED.

TDenisM commented 4 years ago

I use something like that to restart NodeRed flow automatically in case of disconnecting of OpenHab:

[{"id":"f56768cf.1ea0b8","type":"status","z":"ff430707.e57cd8","name":"","scope":["82cc936e.64783"],"x":800,"y":80,"wires":[["f8043319.ad5a5"]]},{"id":"f8043319.ad5a5","type":"function","z":"ff430707.e57cd8","name":"","func":"if (msg.status.fill && msg.status.fill != 'green') {\n return msg;\n} else {\n msg.reset = true;\n return msg;\n}\n\n","outputs":1,"noerr":0,"x":960,"y":80,"wires":[["81b1cec7.37f17"]]},{"id":"81b1cec7.37f17","type":"delay","z":"ff430707.e57cd8","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1100,"y":140,"wires":[["af3ac592.ef6708"]]},{"id":"af3ac592.ef6708","type":"function","z":"ff430707.e57cd8","name":"","func":"msg.headers = {};\nmsg.headers['Node-RED-Deployment-Type'] = 'reload';\nreturn msg;","outputs":1,"noerr":0,"x":1130,"y":200,"wires":[["480bfc07.18e2c4"]]},{"id":"480bfc07.18e2c4","type":"http request","z":"ff430707.e57cd8","name":"","method":"POST","ret":"txt","url":"http://127.0.0.1:1880/flows","tls":"","x":1210,"y":260,"wires":[["2e7e5a49.98e816"]]},{"id":"e50030c8.a4df","type":"inject","z":"ff430707.e57cd8","name":"MANUAL RESTART FLOW","topic":"","payload":"","payloadType":"date","repeat":"3600","crontab":"","once":false,"onceDelay":0.1,"x":1060,"y":340,"wires":[["af3ac592.ef6708"]]},{"id":"2e7e5a49.98e816","type":"debug","z":"ff430707.e57cd8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1290,"y":320,"wires":[]}]

In Status node you should select one of your OpenHab in or out nodes. In my case there is no authorisation in NodeRed. If you have, you should read documentation and add some headers with your credentials. The status node checks if status of OpenHab node is ok (green), if it becomes red, the flow will restart in 5 seconds. If OpenHab node reconnects in 5 seconds the delay node resets and nothing happens.