Shelly pro 2 internal watchdog script

I think getting the in_mode is not available via rpc. But you can use getComponentConfig e.g.

print ("in_mode: " + Shelly.getComponentConfig("switch:0").in_mode);

I figuerd it out, this script seems to work, I have note yet simulated a real network failure, but if I set a bad ip adress it does what it’s supposed to. My openHAB server is setting mode to detached when connection is re established just to make sure connection is ok. For some reason my devices can be stuck as unreachable for openHAB binding if device is offline for more than xtime, lets say that I cut power to a switch or to the outdoor baths that this unit controls openHAB sometimes fail to get this things back online.

I also implemented a successCounter just in case, thanks for all help and I hope this can help someone else.

let CONFIG = {
  endpoints: [ "http://openHABserver:8080", ],
  numberOfFails: 4, httpTimeout: 10, pingTime: 15,
};
let endpointIdx = 0;
let failCounter = 0;
let successCounter = 0;
let pingTimer = null;
let op_mode = "";
let old_op_mode ="";

function pingEndpoints() {
  Shelly.call("http.get", { url: CONFIG.endpoints[endpointIdx], timeout: CONFIG.httpTimeout },
     
      function (response, error_code, error_message) {
      old_op_mode = op_mode;
      op_mode = Shelly.getComponentConfig("switch:0").in_mode;
      if (error_code === -104) {
        print("Failed to fetch ", CONFIG.endpoints[endpointIdx]);
        failCounter++;
        successCounter=0;
        print("Rotating through endpoints");
        endpointIdx++;
        endpointIdx = endpointIdx % CONFIG.endpoints.length;
        print("Error openHAB server not reachable number of errors:", failCounter);
        print ("operating_mode: " + op_mode);
                
        } else {
        failCounter = 0;
        successCounter++;        
        print("Success openHAB server reachable setting failcounter to:", failCounter);
        print("Gamla operating mode: " +old_op_mode+ " Ny operating mode: " +op_mode);
        print("successCounter: "+successCounter)
        }
      
      if (failCounter >= CONFIG.numberOfFails && op_mode ==="detached"){
        //if (failCounter >= CONFIG.numberOfFails) {
        print("Too many fails, change mode to follow!");
        successCounter = 0;
        Shelly.call( "http.get", { url: "http://127.0.0.1/rpc/Switch.SetConfig?id=0&config={%22in_mode%22:%22follow%22}", timeout: CONFIG.httpTimeout })
      }
      
    }
  );
}

print("Start watchdog timer");
pingTimer = Timer.set(CONFIG.pingTime * 1000, true, pingEndpoints);

That’s not going to work like you wanted: e.g. you never set back to “follow”. Frankly with all the code commented out that doesn’t look like a solution to recommend

I removed the out commented code in solution. In my implementation standard in_mode is detached. When connection to openHAB server is lost for 4 retrys in my case one minute, watchdog change in_mode to follow. In my case the watchdog shouldn’t set mode to detached again that should come from the rule in openHAB which it does. If in_mode is follow and connections exist from openHAB to shelly device it should set it in detached mode again.

I haven’t tested a real outage yet but I simulated with bad ip adress setting in the script and upon failure it does what I needed it to do and if openHAB has connection it set in_mode to detached again.

1 Like

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