Check, if openHAB cloud Connector is online?

Doesn’t the Binding auto reconnect?

The main issue is here: Connection error recovery failed · Issue #134 · openhab/openhab-cloud · GitHub

1 Like

Thank you @abal . I’ve implemented your Script. Works like a charm.

  • OpenHAB also runs in a container (Docker). Since there is no portainer in my installation, I just stop OpenHAB and docker then automatically restarts the container (restart_policy: unless-stopped);
  • I trigger the script every minute.

Question: To trigger the stopping of OpenHAB I used “executeCommandLine” in a DSL rule (see bellow). Would it be possible to trigger this directly in JS 2021? Sorry for this amateurish question.

JS 2021:

var rnd = (Math.random() + 1).toString(36);
var heartbeathItem = items.getItem("String_heartbeat");
heartbeathItem.postUpdate(rnd);

var response = actions.HTTP.sendHttpGetRequest('https://myopenhab.org/rest/items/String_heartbeat', {"Authorization": "Basic __Acc_PW___"}, 5000);

var isOk = false;
try{
  var responseJson = JSON.parse(response);
  isOk = responseJson.state === rnd;
  // console.log("REST RESULT: " + responseJson.state + "; EXPECTED: " + rnd + "; IsOk: " + isOk);
} catch (err){
  isOk = false;
}

var lastHeartbeatItem = items.getItem("Num_heartbeat_last_live");

if(isOk){
  // Update the last heartbeat date, we will use it for downtime calculation
  lastHeartbeatItem.postUpdate((new Date()).getTime());
} else {
  console.log("Cloud connector is down or something bad happened. (API response: " + response + ")");

  // If the connection is broken for more than 10 minutes, but internet connection is live, then restart OH container
  var now = new Date();
  var lastHeartbeat = now;
  if(lastHeartbeatItem.state !== 'NULL'){
    lastHeartbeat = new Date(parseInt(lastHeartbeatItem.state));
  }
  var delta = now - lastHeartbeat;
  var downForSec = Math.abs(delta)/1000;
  console.log("Last heartbeat: " + downForSec + " sec ago");
  // if down for more than 10 min, then check internet connection and if alive (means, that the OH cloud connector is only broken) then restart container
  if(downForSec > (10 * 60)){
    console.log("Cloud connector is down for more than 10 minutes, restarting OH container ...");
    // Update the last beat, to avoid cyclic restarts right after the startup (first script execution)
    lastHeartbeatItem.postUpdate((new Date()).getTime());
        
    osgi.getService("org.openhab.core.automation.RuleManager").runNow("reboot",true, {"trigger_type": "event"});
    
    //console.log("Restart container: " + (res !== null ? "OK" : "FAILED"));
    console.log("Restart container");
  }
}

DSL rule:

executeCommandLine(Duration.ofSeconds(10),"bash","/openhab/runtime/bin/stop");
1 Like

In ECMA2021 it should be something like this:

var Duration = Java.type("java.time.Duration");
var stopOH = actions.Exec.executeCommandLine(Duration.ofSeconds(10), 'bash', '/openhab/runtime/bin/stop');

Theoretically it should work with time.Duration so you don’t have to import the Java Duration. If that doesn’t work an issue should be filed to do the same magic for Duration that is done for ZonedDateTime.

1 Like

Thanks Rich. Yes, it works :slight_smile:

1 Like