moddedBear

Control TP-Link Kasa Smart Devices With Tasker

I was recently gifted a few Kasa smart plugs, and after a little bit of research I found out it's possible to use their API to control the plugs using Tasker, a popular Android automation app. The resources I was able to find were just a little out of date, so I'm deciding to write my own notes on the process here.

At the bottom of this post I'll link to the XML files for the tasks I created in case anybody wants to import them.

Create a login task

Before anything else, you need to create a task that logs in to the Kasa API and gets a token. This token should be saved as a global variable so other tasks can make use of it.

You'll need to create a few global variables:

With that done, you can start creating the login task.

Start with a Variable Set action. Name the variable "%payload" and set it to the following:

{ "method" :"login",
"params" : {
  "appType" :"Kasa_Android",
  "cloudPassword" :"%TPLPASS",
  "cloudUserName" :"%TPLUSER",
  "terminalUUID" :"%TPLTERM" } }

Next, create an HTTP Request action. Set the method to POST, the URL to "https://wap.tplinkcloud.com", and the body to "%payload".

Create a JavaScriptlet action next and give it the following code. This will extract the login token from the response and save it as a local variable.

var mtoken = JSON.parse(http_data).result.token;

The last thing to do is to use another Variable Set action to set the global %TPLTOKEN variable to "%mtoken".

I don't know how often this token expires, but you may want to create a profile that runs the login task on some sort of regular interval just in case.

Find your device IDs

To control a device you first need to know its ID. You can get a list of all of your devices by sending a POST request to "https://wap.tplinkcloud.com?token={your-token-here}" with the following data:

{ "method": "getDeviceList" }

Here is an example curl command that'll do the trick. You may want to pipe it to jq to make the response more readable.

curl -X POST -H "Content-type: application/json" -d '{ "method": "getDeviceList" }' 'https://wap.tplinkcloud.com?token={your-token-here}'

Find whichever devices you want to control in Tasker in the response and make note of their device IDs. Save each device ID as a Tasker variable and call it something descriptive like %TPLLAMPID.

Create a device control task

Create a new task and add a Variable Set action. Call the variable %payload and set it to the following:

{"method":"passthrough", "params": {"deviceId": "%TPLDEVICEID", "requestData": "{\"system\":{\"set_relay_state\":{\"state\":0}}}" }}

Be sure to change %TPLDEVICEID to whatever you called the variable that contains your device ID. Also be sure to set the state to either 0 for off or 1 for on.

Next create an HTTP Request action and set its method to POST and its URL to "https://wap.tplinkcloud.com?token=%TPLTOKEN". Set its body to "%payload".

That should be it! If you run the task, you should see your device turn off or on depending on what you set the state in the payload to. If you need to debug, you can create a popup action with the text "%http_data" to see the response from the API.

Resources used

https://www.reddit.com/r/tasker/comments/czt93u/integrate_kasa_app_with_tasker/
https://itnerd.space/2017/06/19/how-to-authenticate-to-tp-link-cloud-api-with-tasker/
https://itnerd.space/2017/01/22/how-to-control-your-tp-link-hs100-smartplug-from-internet/

- moddedBear / 2021-12-06

Home