Preview
Notice the Bulb icon being “clicked” turns on all lights in my bedroom, and changes the colour of the tile!
TL;DR
If you want to get going as quickly as possible, download this GIST on your phone and import it!
Source Code:
Simply click and hold the “TASKS” tab at the top, click “Import” and navigate to the place where you downloaded the GIST.
Click through and change the parts that say CHANGE_ME based on your own settings!
Why
I would like to continue on the good work by @broesie on his post Android: Control Home Automation with Tasker from Quick Toggles to explain how to make toggleable commands through Tasker!
The difference between this example and that of the previous example:
- Requires one task per toggle, instead of two
- Can read return data from Tasker for the true status
- Repeatable use of variables
- Use of Javascript to cut many, multiple commands to one!
Let's Begin
Create the Task
Click the +
icon to add a new task, and call it something meaningful. Note this name will be the word that appears under the tile!
Creating the Variables
For the sake of code re-usability, I would recommend making the following variables: 1. `url` (should be called `domain`) 2. `address` 3. `body`Create the variable by clicking the +
button down the bottom right, and searching for Variable Set
The %body
variable is what you would send to HA. You can work out what you need to send by going to the Services icon in the Developer tools, or looking through the forum for examples.
This is the example for the %body
variable.
For the %url
variable, this should be your dynamic DNS address, or however you access HA. For example https://xxxxxxxxx.duckdns.org:8123
(note, no trailing forward slash at the end! /
)
For the %address
you will need to work out what that is, but if you wish to toggle a light, it will be the same as mine:
/api/services/light/toggle?api_password=xxxx
note that if you have a password for web access, you place it at the end at the address, as the variable api_password
as is above, replacing the xxxx
.
Create the Javascript Request
After the variables are set, click the `+` and search for `JavaScriptlet` and add that.Add the following in the Code
section:
var xhttp = new XMLHttpRequest();
xhttp.open = ( "POST", url+address, false );
xhttp.send(body);
var result = JSON.parse(xhttp.responseText)[0].state;
The [0].state
at the end will look at the first array returned, and the value of state
.
If you have any issues, add flash(xhttp.responseText);
after the 3rd or 4th line to see if you are actually getting a response from HA!
If Statement
![11|690x388](upload://b668SYpig8KvSVIfM14FjfBGFFX.png)For the if
statements:
- Add an
if command
a. and in the first field type%result
and for the comparison, doon
. - Add the
Setup Quick Settings
action after that
a. For the name, it should be the same as the current task you have made, and are in! - add an
else
a. Tick the+
b. check%result
againstoff
c. important step toggle theStatus
toInactive
! This will turn the icon to a lighter shade
Choose the Icon
- Click the “grid icon” at the bottom of the screen
- Click on
Holo
- Choose any icon
Create the Quick Settings Tile
- Go to your quick tiles, and drag one of the Tasker tiles into your drawer as seen in the video.
- After exiting the edit, you should see your unset Tasker quick setting.
- Click and hold on the tile to edit it
- In the sceen that comes up, click the “magnifying glass” icon and click on your new task
Done!