Node-Red Flow Samples/Sharing

Restart Homebridge on HE reboot/power cycle

I was having an issue with Homebridge log showing errors if HE was restarted and Homebridge was not

The errors appear to be harmless but I was looking for a way to automatically restart Homebridge process when HE was restarted. It turns out that the Homebridge UI plug-in has an API that can be accessed from Node-RED.

The following flow automatically restarts Homebridge if HE is restarted.

It uses standard nodes (Change, Delay, Function, HTTP Request) and the node-red-contrib-credentials (node-red-contrib-credentials (node) - Node-RED) for username, password and the IP address/port of the Homebridge server.

The flow is triggered by the link-in from the Hub systemStart location event on another flow. The flow execution is delayed for 2 minutes to ensure that HE has finished rebooting. It then logs into Homebridge and calls the restart API (similar to the user selecting the "restart" button in the Homebridge UI)

Below is the flow if anyone wants it.

NOTE: The log errors seem to be only with the MakerAPI Homebridge plug-in

[{"id":"b4ab3d8b.fdbf5","type":"group","z":"934f1005.6e02","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["f4c6b858.26b3e8","166fcda7.a1102a","695cbfba.6f73a","acf9ac7f.7b6e7","122304c7.f7beeb","e7ca8e9a.de2618","4012e6a2.c3064","e603b57e.6c9968","1122835a.5ff315","bd279032.bec948","8576170d.ec4a38","d98d244.6275758"],"x":43.33332824707031,"y":2490.0000381469727,"w":1339.6667022705078,"h":323.99996185302734},{"id":"f4c6b858.26b3e8","type":"link in","z":"934f1005.6e02","g":"b4ab3d8b.fdbf5","name":"","links":["d22ba984.19a1e"],"x":97.33332824707031,"y":2583.0000381469727,"wires":[["166fcda7.a1102a"]]},{"id":"166fcda7.a1102a","type":"change","z":"934f1005.6e02","g":"b4ab3d8b.fdbf5","name":"Delete Payload","rules":[{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":236.33334350585938,"y":2582,"wires":[["695cbfba.6f73a"]]},{"id":"695cbfba.6f73a","type":"delay","z":"934f1005.6e02","g":"b4ab3d8b.fdbf5","name":"Delay 2 Mins","pauseType":"delay","timeout":"2","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":431.3333435058594,"y":2583,"wires":[["acf9ac7f.7b6e7"]]},{"id":"acf9ac7f.7b6e7","type":"credentials","z":"934f1005.6e02","g":"b4ab3d8b.fdbf5","name":"","props":[{"value":"payload.username","type":"msg"},{"value":"payload.password","type":"msg"},{"value":"HB_IP","type":"msg"}],"x":177,"y":2652.33349609375,"wires":[["e7ca8e9a.de2618"]]},{"id":"122304c7.f7beeb","type":"inject","z":"934f1005.6e02","g":"b4ab3d8b.fdbf5","name":"Manual Restart","props":[{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":186,"y":2709.33349609375,"wires":[["acf9ac7f.7b6e7"]]},{"id":"e7ca8e9a.de2618","type":"function","z":"934f1005.6e02","g":"b4ab3d8b.fdbf5","name":"Set Login URL and Headers","func":"//Set URL for Homebrdige login and header for Content Type\n\nmsg.url = msg.HB_IP+'/api/auth/login'\nmsg.headers = 'Content-Type: application/json'\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":401.333251953125,"y":2653.0002365112305,"wires":[["4012e6a2.c3064"]]},{"id":"4012e6a2.c3064","type":"http request","z":"934f1005.6e02","g":"b4ab3d8b.fdbf5","name":"Login and get Token","method":"POST","ret":"obj","paytoqs":"body","url":"","tls":"","persist":false,"proxy":"","authType":"","x":654.9999542236328,"y":2652.0002059936523,"wires":[["e603b57e.6c9968"]]},{"id":"e603b57e.6c9968","type":"function","z":"934f1005.6e02","g":"b4ab3d8b.fdbf5","name":"Set Restart URL and Headers","func":"var token = msg.payload.access_token;\nmsg.headers = {\n     Authorization: \"Bearer \"+token\n}\nmsg.url = msg.HB_IP+'/api/server/restart'\n\n//Delete unneeded messager parts\ndelete msg.HB_IP\ndelete msg.payload.access_token\ndelete msg.payload.token_type\ndelete msg.payload.expires_in\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":912.3333129882812,"y":2651.0002059936523,"wires":[["1122835a.5ff315"]]},{"id":"1122835a.5ff315","type":"http request","z":"934f1005.6e02","g":"b4ab3d8b.fdbf5","name":"Restart Homebridge","method":"PUT","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":1172.0000381469727,"y":2650.6669921875,"wires":[["bd279032.bec948"]]},{"id":"bd279032.bec948","type":"debug","z":"934f1005.6e02","g":"b4ab3d8b.fdbf5","name":"RESTART RETURN","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1237.0000305175781,"y":2704.33349609375,"wires":[]},{"id":"8576170d.ec4a38","type":"comment","z":"934f1005.6e02","g":"b4ab3d8b.fdbf5","name":"Restart Homebridge on HE reboot","info":"","x":213.3333282470703,"y":2531.0000381469727,"wires":[]},{"id":"d98d244.6275758","type":"comment","z":"934f1005.6e02","g":"b4ab3d8b.fdbf5","name":"Change Log","info":"2021-05-23: Created","x":139.3333282470703,"y":2773,"wires":[]}]

Hope this helps someone!

1 Like