IT Nerd Space

How to remotely get the state of your HS100 smart plug

How to remotely get the state of your HS100 smart plug

How to remotely get the state of your HS100 smart plug

In previous posts we’ve seen how to remotely switch On/Off our TP-Link HS100 smart plug, and how to control it with IFTTT. In this post we’ll see how we can remotely obtain the state of the HS100 relay: On or Off, from command line. I even do it with Tasker for Android!

Please be sure to refer to this post if you are unsure how to obtain the Token and DeviceID that are necessary to control the plug via TP-Link cloud service.

TL;DR: The one-liner below, run in a Linux/Android/Mac shell, will return ON or OFF, indicating what is the state of an HS100 relay:

curl -s --request POST "https://wap.tplinkcloud.com/?token=TOKEN_HERE HTTP/1.1"\
 --data '{"method":"passthrough", "params": {"deviceId": "DEVICEID_HERE", "requestData": "{\"system\":{\"get_sysinfo\":null},\"emeter\":{\"get_realtime\":null}}" }}'\
 --header "Content-Type: application/json" | grep -q '..relay_state..:1' && echo "ON" || echo "OFF"

I’ve put this line above in a Tasker shell action, and I can perfectly obtain my HS100 state from Tasker now! So cool.

For the one wondering what we can obtain with the get_sysinfo command: run the following line. Here I extract from the Json output the responseData field, which contains the interesting payload, I unescape it with sed, and pretty-format it using json_pp:

curl -s --request POST "https://wap.tplinkcloud.com/?token=TOKEN_HERE HTTP/1.1"\
  --data '{"method":"passthrough", "params": {"deviceId": "DEVICEID_HERE", "requestData": "{\"system\":{\"get_sysinfo\":null},\"emeter\":{\"get_realtime\":null}}" }}'\
  --header "Content-Type: application/json" | jq ".result.responseData" | sed -e 's#\\\"#"#g; s#^\"##; s#\"$##' | json_pp

The output would be something like this. I’ve masked most of the data for obvious privacy reasons, but you can still get the idea. We see the position, time the device was On, MAC Address, Model, Alias (custom name)…

{
 "system" : {
 "get_sysinfo" : {
 "fwId" : "FWID_HERE",
 "relay_state" : 1,
 "dev_name" : "Wi-Fi Smart Plug",
 "oemId" : "OEMID_HERE",
 "on_time" : 451,
 "model" : "HS100(EU)",
 "icon_hash" : "",
 "updating" : 0,
 "led_off" : 0,
 "err_code" : 0,
 "longitude" : LONGITUDE_HERE,
 "mac" : "MAC_ADD_HERE",
 "hwId" : "HWID_HERE",
 "rssi" : -53,
 "deviceId" : "DEVICEID_HERE",
 "latitude" : LATITUDE_HERE,
 "sw_ver" : "1.0.8 Build 151101 Rel.24452",
 "alias" : "My Smart Plug",
 "type" : "smartplug",
 "hw_ver" : "1.0",
 "feature" : "TIM",
 "active_mode" : "schedule"
 }
 },
 "emeter" : {
 "err_msg" : "module not support",
 "err_code" : -1
 }
}

I guess an HS110 which has some energy consumption colection capability would give more data in the emeter section. Mine is an HS100, and I get “module not support(ed)”. Don’t hesitate to send me some example of an HS110 output energy data, I’ll put them here as well.

We can also extract one particular field’s value using a one-liner like the one below. Here I would extract the value of the relay_state field:

curl -s --request POST "https://wap.tplinkcloud.com/?token=TOKEN_HERE HTTP/1.1"\
 --data '{"method":"passthrough", "params": {"deviceId": "DEVICEID_HERE", "requestData": "{\"system\":{\"get_sysinfo\":null},\"emeter\":{\"get_realtime\":null}}" }}'\
 --header "Content-Type: application/json" | jq ".result.responseData" | sed -e 's#\\\"#"#g; s#^\"##; s#\"$##' | jq ".system.get_sysinfo.relay_state"
  • Jacob Tan

    Hey Alex, here’s the HS110 output I got:

    {
    “error_code”:0,
    “result”:{
    “responseData”:”{“system”:{“get_sysinfo”:{“err_code”:0,”sw_ver”:”1.1.2 Build 161014 Rel.170948″,”hw_ver”:”1.0″,”type”:”IOT.SMARTPLUGSWITCH”,”model”:”HS110(UK)”,”mac”:”MAC_ADDR_HERE”,”deviceId”:”DEVICE_ID_HERE”,”hwId”:”HW_ID_HERE”,”fwId”:”FW_ID_HERE”,”oemId”:”OEM_ID_HERE”,”alias”:”ALIAS_HERE”,”dev_name”:”Wi-Fi Smart Plug With Energy Monitoring”,”icon_hash”:””,”relay_state”:1,”on_time”:1905,”active_mode”:”schedule”,”feature”:”TIM:ENE”,”updating”:0,”rssi”:-43,”led_off”:0,”latitude”:LAT_HERE,”longitude”:LONG_HERE}},”emeter”:{“get_realtime”:{“current”:0.153893,”voltage”:235.863940,”power”:35.487832,”total”:0.067000,”err_code”:0}}}”
    }
    }

    • James Scholes

      I’m seeing a similar error_code: 0 response, but if you parse that output, you can see relay_state: 1, so I expect your switch is on, yet?

      • Jacob Tan

        Yup it’s switched on, just did another POST and I get error_code: 0 response again

  • Timo

    Is it possible to integrate this into ifttt?
    For example if my HS100 is switched on, turn on my Hue lights?

    • Oh man, you just gave me an idea for another blog post Timo! Keep watching this space 😄

      • Timo

        Great! 😅

      • Timo

        Do you have any news/updates about my request? 😊