To implement that in Tasker I have used RESTask for Tasker (because I had troubles having the native HTTP Post action work for me):
RESTask for Tasker (Free, Google Play) →
Next create 3 global variables in Tasker (self-explanatory I guess):
Then create a new Task with the following 4 actions:
The first action will be a Variable Set %payload:
Name:%payload To: { "method" :"login", "params" : { "appType" :"Kasa_Android", "cloudPassword" :"%TPLPASS", "cloudUserName" :"%TPLUSER", "terminalUUID" :"%TPLTERM" } }
The second action will be the RESTask call:
Further down we need to set the custom body to %payload:
The third action will be a Javascriptlet with the following code, to extract the token (from the rtres, passed from RESTask) to the %mtoken variable:
var mtoken = JSON.parse(rtres).result.token;
Finally, here the fourth action will just be a Flash action, that will show the token:
Flash [ Text:%mtoken Long:On ]
The whole task should end up looking like this:
You can run the Task and it should flash the token on the screen!! Yay
]]>The first thing we need is the ability to control the HS100 smart plug from Tasker on our Android phone. If you haven’t already, be sure to check my preview post where I explain how to create two Tasker Tasks, that will turn our plug On and Off.
For the next step I’ll use the WearTasker: with this app you can choose some of your Tasker tasks and publish them on your watch. They will show as buttons when you open WearTasker on your wrist, and the corresponding task will run when pressed. Quite simple. TBH WearTasker feels like a natural extension of Tasker to Android Wear.
WearTasker - Tasker for Wear (Free+, Google Play) →
You can buy a Pro in-app purchase if you’d like, which unlocks some cool features, but what I use here works with the free version.
So let’s publish both our smart plug tasks. Open Wear Tasker on your phone, and create a new shortcut. Choose your “Power On Plug” task, and name it however you like. You can also select an icon and a color. Then repeat the same operations with the other task:
Back to the app, it should now look like this:
And that’s how it will look on your watch:
Now try to press one of the Task and see how your HS100 smart plug responds!
I love WearTasker. I find it extremely usefull to be able to call Tasker tasks from my watch. Some other tasks I have there are one to power off my home server, one to refresh my Plex library, and one to send a Whatsapp to my wife with my current location and current route ETA.
]]>To quickly recap, this is the command that can change the plug state (depending on the %state variable, 0: switch off, 1: switch on):
curl --request POST "https://eu-wap.tplinkcloud.com/?token=%token HTTP/1.1" \ --data '{"method":"passthrough", "params": {"deviceId": "%deviceId", "requestData": "{\"system\":{\"set_relay_state\":{\"state\":%state}}}" }}' \ --header "Content-Type: application/json"
In Tasker let’s create a new Task called “Switch Plug On ”, then add 3 “Variable Set” actions, to set the value of %deviceId, %token and %state to the right values for you.
Then we’ll use an HTTP Post action, that we’ll configure like this:
Note: Remember to change the URL above to the one that works for you (check the Common Issues section at the end of first post and its comments).
The resulting Tasker Task will look like this:
In text form, it looks like this:
Switch Plug On 💡 (143) A1: Variable Set [ Name:%deviceId To:YOUR_DEVICE_ID_HERE Recurse Variables:Off Do Maths:Off Append:Off ] A2: Variable Set [ Name:%token To:YOUR_TOKEN_ID_HERE Recurse Variables:Off Do Maths:Off Append:Off ] A3: Variable Set [ Name:%state To:1 Recurse Variables:Off Do Maths:Off Append:Off ] A4: HTTP Post [ Server:Port:https://eu-wap.tplinkcloud.com Path:?token=%token Data / File:{"method":"passthrough", "params": {"deviceId": "%deviceId", "requestData": "{\"system\":{\"set_relay_state\":{\"state\":%state}}}" }} Cookies: User Agent: Timeout:30 Content Type:application/json Output File: Trust Any Certificate:Off ]
If you have curl available from the command line on your Android phone, you can alternatively use a Run Shell action, instead of the HTTP Post action.
In this case, the Tasker Task would look like this:
And in text form it would read like this:
Switch Plug On 💡 (143) A1: Variable Set [ Name:%deviceId To:YOUR_DEVICE_ID_HERE Recurse Variables:Off Do Maths:Off Append:Off ] A2: Variable Set [ Name:%token To:YOUR_TOKEN_ID_HERE Recurse Variables:Off Do Maths:Off Append:Off ] A3: Variable Set [ Name:%state To:1 Recurse Variables:Off Do Maths:Off Append:Off ] A4: Run Shell [ Command:curl --request POST "https://eu-wap.tplinkcloud.com/?token=%token HTTP/1.1" --data '{"method":"passthrough", "params": {"deviceId": "%deviceId", "requestData": "{\"system\":{\"set_relay_state\":{\"state\":%state}}}" }}' --header "Content-Type: application/json" Timeout (Seconds):0 Use Root:Off Store Output In: Store Errors In: Store Result In: ]
Once you have the Switch On task working, you can simply clone it in Tasker and change the %state variable to 0, and that will give you the “Switch Off Plug” task!
Do not hesitate to contribute in the comments with any Profile idea you have to play with your TP-Link HS100 smartplug.
You can acquire Tasker from the Play Store, personally I believe it’s really worth the money. You can also download a 7 days free trial from Tasker official website.
In a next post I’ll show how you can control your smart plug from your Android smartwatch, which is super cool!
]]>One of the little know features of Tasker is the ability to run Android Java code, sort of.
That requires some learning, as everything in Tasker, but can prove very useful to further extend Tasker possibilities.
Let see a simple example, to illustrate the concept. Let say we want to enable Bluetooth. The corresponding Android Java code would be something like :
In Tasker we would use a couple of Java Function actions that we put in a Tasker task :
First:
BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
Then:
bta.enable();
This is the resulting Tasker task:
Bluetooth Enable (71) A1: Java Function [ Return:bta Class Or Object:BluetoothAdapter Function:getDefaultAdapter {BluetoothAdapter} () Param: Param: Param: Param: Param: Param: Param: ] A2: Java Function [ Return: Class Or Object:bta Function:enable {boolean} () Param: Param: Param: Param: Param: Param: Param: ]
This is very powerful and can unleash some new possibilities.
Say you want to enable LinkedIn synchronization on WiFi only. Tasker has no native actions to handle Android accounts synchronization. You need to access the ContentResolver Java objects and methods. This is something you can do with Tasker! I’ll detail this example in a future post.
]]>