plasticrake / tplink-smarthome-api

TP-Link Smarthome WiFi API
MIT License
1.02k stars 141 forks source link

Plug#getInUse() question #51

Closed kurac338 closed 6 years ago

kurac338 commented 6 years ago

Hi, I am having a little trouble getting device.getInUse() to work. I have an HS110 (v3 EU) on the latest firmware (1.5.2).

I instantiate the client like so: const plug = client.getPlug({host: DEVICE_IP, inUseThreshold: POWER_TRESHOLD}); Where my POWER_TRESHOLD = 20 Watts.

Later on when I try to get the in use status, it always returns false. It seems as though the emeter power property is not set properly. If I try to print it, it is undefined.

Talking about this source code chunk: async getInUse (sendOptions) { if (this.supportsEmeter) { await this.emeter.getRealtime(sendOptions); } else { await this.getSysInfo(sendOptions); } return this.inUse; }

this.emeter.realtime.power = undefined this.emeter.realtime.power_mw = 25322 (OK)

Either I am doing something wrong or the reponse object has changed. Any help would be appreciated, thanks.

kurac338 commented 6 years ago

As a quick fix I ended up writing my own method in my JS script. Querying the emeter for a realtime value and then comparing the power_mw value with my threshold value, and used that method in place of #getInUse().

plasticrake commented 6 years ago

@kurac338 I don't have a newer version of the plugs that has the different emeter response. Can you help me understand the new values by running this command and pasting the output here? Remove any sensitive information such as location.

tplink-smarthome-api getInfo <ip of device>

You can run this to find the IP:

tplink-smarthome-api search

Thanks!

kurac338 commented 6 years ago
response:
{ sysInfo: 
   { sw_ver: '1.5.2 Build 180130 Rel.085820',
     hw_ver: '2.0',
     type: 'IOT.SMARTPLUGSWITCH',
     model: 'HS110(EU)',
     mac: 'bla-bla',
     dev_name: 'Smart Wi-Fi Plug With Energy Monitoring',
     alias: 'Plug',
     relay_state: 0,
     on_time: 0,
     active_mode: 'none',
     feature: 'TIM:ENE',
     updating: 0,
     icon_hash: '',
     rssi: -38,
     led_off: 0,
     longitude_i: bla,
     latitude_i: bla,
     hwId: 'bla-bla',
     fwId: 'bla-bla',
     deviceId: 'bla-bla',
     oemId: 'bla-bla',
     next_action: { type: -1 },
     err_code: 0 },
  cloud: 
   { info: 
      { username: '',
        server: 'devs.tplinkcloud.com',
        binded: 0,
        cld_connection: 1,
        illegalType: 0,
        tcspStatus: 1,
        fwDlPage: '',
        tcspInfo: '',
        stopConnect: 0,
        fwNotifyType: 0,
        err_code: 0 } },
  emeter: 
   { realtime: 
      { voltage_mv: 236960,
        current_ma: 37,
        power_mw: 0,
        total_wh: 645,
        err_code: 0 } },
  schedule: { nextAction: { type: -1, err_code: 0 } } }
plasticrake commented 6 years ago

Thanks @kurac338! I have changed the API to normalize both the new and old style emeter.realtime values. If you could upgrade to the newest version and test for me (as I don't have a new style plug to test with) that would be very helpful! Both old and new will use the older value so you'd have to divide your threshold value you were using in your custom script by 1000.

kurac338 commented 6 years ago

The new version seems to have fixed the issue. I've done a couple of quick tests. I will update this thread if I run into any more related issues. Thanks!

BTW for anyone setting up the device, the inUseThreshold parameter is specified in Watts.

plasticrake commented 6 years ago

@kurac338 Thanks! I'm updating the documentation to be clear that it is in Watts