TP-LINK HS110 Get daily power consumption via PowerShell


#1

Hello.

I am trying to create Powershell script to read TP-LINK HS 110 daily / monthly power consumption using Invoke-WebRequest command.
I am able to get token from https://wap.tplinkcloud.com and also to get my device ID. Then when i try to use {‘system’:{‘get_sysinfo’:{}}} or {‘emeter’:{‘get_daystat’:{‘month’:2,‘year’:2018}}} it ends after few seconds with request timeout message. What i am doing wrong?

Here is command list:

Here is a PowerShell example:
$authToken = “TOKEN FROM LOGIN REQUEST”
$euUri = “https://eu-wap.tplinkcloud.com” (FROM deviceList.appServerUrl)
$addr = $euUri + “?token=” + $authToken
$devID = “DEVICE ID FROM {‘method’:‘getDeviceList’}”
$command = “{‘system’:{‘get_sysinfo’:null}}”
$getStat = “{‘method’:‘passthrough’,‘params’:{‘deviceId’:’”+$devID+"’,‘requestData’:"+$command+"}}"
$result = Invoke-WebRequest -Uri $addr -Method POST -Body $getStat -ContentType “application/json” -TimeoutSec 60

And the response is {“error_code”:-20002,“msg”:“Request timeout”}

Thank you for help.


(Darren) #2

Is the purpose of this so you can track usage over time?


#3

Exactly. The mobile Kasa app only showing averages. I would like to have detailed stats.


(Darren) #4

@Gutheinz maybe able to assist as he wrote the dth and smartapp for the tp interface


(Dave Gutheinz) #5

Do you have a SmartThings and SmartThings account?

I have groovy code on gitHub in a service manager and device handler that may be of assistance. It includes cloud login. Additionally, it includes the commands to get daily data for for the last 31 days (I then average them, like the Kasa App). There is also another command to the plug that gets monthly totals for a year or so.

Device Handler file: (Cloud) TP-Link Plug-Switch.groovy
Location: https://github.com/DaveGut/Cloud-Based_TP-Link-to-SmartThings-Integration/tree/master/Device%20Handlers

Service Manager file: TPLink Cloud Connect V2.groovy
Loocation: https://github.com/DaveGut/Cloud-Based_TP-Link-to-SmartThings-Integration/tree/master/Service%20Manager

The energy meter commands are (need to change month and year.

Get Daily Statistic for given Month: {“emeter”:{“get_daystat”:{“month”:1,“year”:2016}}}

Get Montly Statistic for given Year: {“emeter”:{"“get_monthstat”:{“year”:2016}}

Finally, if you are local to the device (on same network), you can control it without going through the cloud. I used node.js to test the bulbs and plugs originally (flush out the commands). Also, I created a logger you can use as an example. It gets the current usage periodically, but changing the frequency and command would allow daily logging. Location: https://github.com/DaveGut/TP-Link-Power-Logger


#6

@Gutheinz Thank you. But there is a requirement that this must be done in PowerShell…

Here is complete source code:
https://pastebin.com/EynYBYRC

I am using exactly the same syntax to get daily statistic for given month
“{‘emeter’:{‘get_daystat’:{‘month’:3,‘year’:2018}}}”

Still getting {“error_code”:-20002,“msg”:“Request timeout”} as response.


(Dave Gutheinz) #7

One potential error comparing my groovy code with yours:

$addr = $euUri + “?token=” + $authToken

should be

$addr = $euUri + “/?token=” + $authToken

There should be a “/” after the $euUri.

Dave

PS - This was done by comparing your code to my Service Manager code mentioned in initial response.