Amplifier automation with Mini

supersix

New member
Joined
May 17, 2024
Messages
3
I have a WiiM Mini and an old amplifier connected to a Shelly Plus Plug S smartplug.
I'm using a script running on the smartplug to automatically poweron and poweroff the aplifier according to Wiim Mini player status API.
It seems to work well enough.
If anyone is interested, this is the script code:
JavaScript:
let CONFIG = {
  interval: 5 * 1000,             // milliseconds
  poweroff_delay: 3 * 60 * 1000,  // milliseconds
  wiim_status_url: "https://192.168.10.201/httpapi.asp?command=getPlayerStatus"
};

function powerOn() {
  print("powerOn()");
  Shelly.call("Switch.Set","{ id:0, on:true }",null,null);
};
function powerOff() {
  print("powerOff()");
  Shelly.call("Switch.Set","{ id:0, on:false }",null,null);
};

function ChangeLEDColor(red, green, blue, brightness) {
  let config = {
      "config": {
          "leds": {
              "mode": "switch",
              "colors": {
                  "switch:0": {
                      "on": {
                          "rgb": [red, green, blue],
                          "brightness": brightness
                      },
                      "off": {
                          "rgb": [red, green, blue],
                          "brightness": 0
                      }
                  }
              }
          }
      }
  }; 
  Shelly.call("PLUGS_UI.SetConfig", config);
}
function LEDPower(brightness) {
  let config = {
      "config": {
          "leds": {
              "mode": "power",
              "colors": {
                  "power": {
                      "brightness": brightness
                  }
              }
          }
      }
  }; 
  Shelly.call("PLUGS_UI.SetConfig", config);
}

function check_wiim_status()  {
  print("check_wiim_status()");
  Shelly.call("HTTP.GET", { url: CONFIG.wiim_status_url, ssl_ca: "*", timeout: 3 },
    function (res, error_code, error_msg, ud) {
      if (error_code !== 0) {
        print("error_code: ", error_code);
        print("error_msg: ", error_msg); 
        return;
      }
      if (res.code === 200) {
        let resobj = JSON.parse(res.body);
        print("__Status: ", resobj.status);
        if (resobj.status === "play" || resobj.status === "loading") {
          if (typeof (timer_off_handle) !== 'undefined') {
            print("____Delete timer for powerOff()");
            Timer.clear(timer_off_handle);
            delete(timer_off_handle);
            LEDPower(30);
          }
          powerOn();
        } else { // status "stop", "pause", "none"
          if (typeof (timer_off_handle) === 'undefined') {
            print("____Start timer for powerOff()");
            timer_off_handle = Timer.set(
              /* number of milliseconds */ CONFIG.poweroff_delay,
              /* repeat? */ false,
              /* callback */ powerOff
            );
            ChangeLEDColor(0, 100, 100, 100); // cyan
          }
        }
      } else {
        print("res.code != 200: ", res.code)
      };
    },
    null
  );
}

let timer_handle = Timer.set(CONFIG.interval,true,check_wiim_status,null);
 
Back
Top