TP-LINK HS110 Get daily power consumption via PowerShell

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.

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

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

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

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

@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.

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.

this code is working
find and replace XXXXX with your config

cls

$uri = “https://wap.tplinkcloud.com
$euUri = “https://eu-wap.tplinkcloud.com
#Some random GUID
$uid = “84ddc92e-c0be-4132-91cb-13305db401a9”

#Login to get token
$login = “{‘method’: ‘login’,‘params’: {‘appType’: ‘Kasa_Android’,‘cloudUserName’: ‘XXXX’,‘cloudPassword’: ‘XXXX’,‘terminalUUID’: $uid }}”
Write-Host “Getting token” -ForegroundColor Yellow
$loginResult = Invoke-WebRequest -Uri $uri -Method POST -Body $login -ContentType “application/json”
$resultJson = $loginResult.Content | ConvertFrom-Json
$authToken = $resultJson.result.token
Write-Host “Token is” $authToken -ForegroundColor Green

#Use device list to get device ID
$getDevice = “{‘method’:‘getDeviceList’}”
$addr = $euUri + “?token=” + $authToken
Write-Host “Getting device ID” -ForegroundColor Yellow
$deviceResult = Invoke-WebRequest -Uri $addr -Method POST -Body $getDevice -ContentType “application/json”
$resultJson = $deviceResult.Content | ConvertFrom-Json
$devID = $resultJson.result.deviceList[0].deviceId
Write-Host “Device ID is” $devID -ForegroundColor Green

#Call emeter to get some power usage data
Write-Host “Getting power usage data” -ForegroundColor Yellow

$command = “{‘system’:{‘get_sysinfo’:null}}”

$getStat="{"“method”":"“passthrough”","“params”":{"“deviceId”":"“XXXXXXXX”","“requestData”":""{"“system”":{"“get_sysinfo”":null},"“emeter”":{"“get_daystat”":{"“month”":10,"“year”":2019}}}}""}}"

#Show the command
Write-Host $getStat

#Send request
$deviceResult = Invoke-WebRequest -Uri $addr -Method POST -Body $getStat -ContentType “application/json” -TimeoutSec 60
($deviceResult).Content

requestData value is a string " {json}"

Hi all,

I’m trying this:

$getStat="{"“method”":"“passthrough”","“params”":{"“deviceId”":"“8006EDB0EA58D9E6ECD161586179818D1B149A0D”","“requestData”":{"“emeter”":{"“get_daystat”":{"“month”":12,"“year”":2019}}}}"

But get always wrong JSON Format:

StatusCode : 200
StatusDescription : OK
Content : {“error_code”:-10100,“msg”:“JSON format error”}
RawContent : HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 47
Content-Type: application/json;charset=UTF-8
Date: Sun, 29 Dec 2019 19:33:06 GMT
Server: Apache-Coyote/1.1

                {"error_code":-10100,"msg":...

Forms : {}
Headers : {[Connection, keep-alive], [Content-Length, 47], [Content-Type, application/json;charset=UTF-8], [Date, Sun, 29 Dec 2019 19:33:06 GMT]…}
Images : {}
InputFields : {}
Links : {}
ParsedHtml : mshtml.HTMLDocumentClass
RawContentLength : 47

what is the issue?

I too am interested in this as, AFAIK, this is the only smart plug that can be controlled via Powershell