First I create a “cloud heartbeat” item in my items file:
DateTime Cloud_Heartbeat_TimeStamp "Cloud access heartbeat date-time [%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS]" (gAllDates)
Then I have a function that I run every ten minutes that updates the item via the cloud, using the REST API. That way, if cloud access is interrupted, the “heartbeat” stops. I don’t know how much detail you need. I’ll post a couple of the relevant functions.
// Update this date-time item via the cloud. If the cloud is down, it will not get updated
ZombificationCheck.prototype.SendCloudHeartbeat =
function SendCloudHeartbeat()
{
// logInfo("SendCloudHeartbeat");
try
{
var headers = GetOpenHabCloudCallAuthorisationHeaders();
// logInfo("Cloud heartbeat headers: " + headers);
var dateNow = new Date();
DoHttpPutRequest("https://myopenhab.org:443/rest/items/Cloud_Heartbeat_TimeStamp/state", "Set cloud heartbeat", "*/*", dateNow.toISOString(), headers);
}
catch(exception)
{
logInfo("Exception caught in SendCloudHeartbeat. Message: " + exception);
}
}
export//
function GetOpenHabCloudCallAuthorisationHeaders()
{
var headers = [];
headers['Content-Type'] = '*/*';
headers['accept'] = '*/*';
headers['Authorization'] = 'Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Cloud user and pw encoded
headers['WWW-Authenticate'] = 'Basic';
return headers;
}
What I’m trying to do, and what may be working - but I’m not sure because lately the cloud disconnections have stopped, is restart the relevant bundle when the cloud heartbeat stops for too long.
ZombificationCheck.prototype.CheckIfAutomateAndCloudOffline =
function CheckIfAutomateAndCloudOffline()
{
// Try restarting cloud client if down for a short amount of time
if (dgReboot.GetMinutesSinceReboot() > 10 && nCloudHeartbeatAge > this.CLOUD_MAX_HEARTBEAT_AGE && (nCloudHeartbeatAge < this.CLOUD_MIN_AGE_ALERT_THRESHOLD || nCloudHeartbeatAge > dgReboot.GetMinutesSinceReboot()))
{
SendPushNotification("Attempting to restart cloud binding because cloud down");
RestartBundle("org.openhab.io.openhabcloud");
}
}
export//
function RestartBundle(sBundleID)
{
var Exec = Java.type("org.openhab.core.model.script.actions.Exec");
var Duration = Java.type("java.time.Duration");
// @ts-ignore
Exec.executeCommandLine(Duration.ofSeconds(6), "sudo", "/usr/bin/ssh", "-p", "8101", "-i", "/var/lib/openhab/.ssh/openhab.id_rsa", "openhab@localhost", "bundle:restart", sBindingID);
}
Getting the bundle restart to work was a bit involved, I recall. I can dig into the details if you want - I’d have to refresh my memory.