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"