AndroidAppsTablets

Pass practically any data to a widget using Tasker

tasker widget - for some reason we don't have an alt tag here

I’ve talked about how Tasker can pass data to certain widgets before, but it’s a topic that deserves its own piece. Several custom widgets apps allow you to use custom data input that can be pulled from either Locale or Tasker, and since those apps can gather data from practically anywhere, the sky’s the limit.

There are several custom widget apps that support this type of data input, like Text Widget, Minimalistic Text, and my favorite – Make Your Clock. What these have in common is that they’re based around the concept of creating your own widget (or editing existing ones) to add various elements like time, data, or weather wherever you like in the widget. One of the custom elements you can add is one that gets its content – text content, that is – from a link to Tasker or Locale. You then position that text field wherever you want in your widget, as part of something else, or as a standalone widget.

Static text

To set up a link between the two apps, you have to transfer data from the automation app to the widget app. In Tasker, these apps add themselves as plug-ins. The idea is that you write data to a variable, which is essentially like a text file that isn’t saved anywhere, but acts more like the clipboard. Both Tasker and the widget apps use their own variables, so the idea is to transfer data to the widget app variable. This might sound complicated, but the configuration screen for Make Your Clock shows how simple it is.

tasker to widget - for some reason we don't have an alt tag here

The second screenshot above is the configuration screen for sending the data to the widget in Tasker. The Variable Name field is where you input any name for the variable – like a file name. It doesn’t really matter what it is as long as you remember it, because it’s what you will then input in the Variable Name field when you add the element to your widget in your widget creation app, like in screenshot 3 above. Variable Value is what the variable should say; basically the text in the “text file.” As an example, if you type in TESTVAR as Variable name, Hello World as Variable value, and then add an element in your widget with the TESTVAR variable name then that element will display Hello World when added to the widget.

You might wonder what makes this different from simply inserting custom static text to a widget. Unlike static text, these variables can change in the background. For instance, if you have a Tasker profile that activates while at work, you could add a new action to that profile that also sets the value of a variable to e.g. At Work. Then, when you come to work, the widget would change automatically.

Obviously though that’s not the most useful of examples since you normally know when you’re at work. To use an example from my own phone, I use this to give me a visual indicator of active profiles. If Sleep Mode (a Tasker profile I have) is on, the widget shows “SM is on.” If you have Tasker profiles for work, home, and so on you can use this to add a small indication to your widget so you know that the right profile is active.

Built in variables

Where this system really becomes useful though is when you also start using variables on the Tasker side. In my examples above, Tasker wrote static text to a variable that belongs to the widget creator app. The real potential comes when Tasker uses variables on its side as well. Tasker variables start with a %, and there are both user created and built-in variables. Built-in variables automatically gain the value of something that Tasker can read, and you can find a full list of those here. For instance, there is a %BATT variable that will display the current battery level as a number from 1-100. What this means in practice is that wherever you type in %BATT in Tasker, that word will automatically be replaced by the battery level. If the battery is at 50%, %BATT will be replaced by 50. If you were to use Tasker’s Notify action to create an Android notification and type in “Battery is at %BATT percent” as the text to display, then that text would change depending on the battery level.

As you can see from the list, there are quite a few built in variables. Everything from what day it is, to who sent you the last SMS, to whether or not silent mode is on. All you need to do to access the information these represent is to use their variable name somewhere there’s text, like when you write text to your widget. Back at the screenshot of the Make Your Clock configuration above, you’ll notice a check box that says “Replace Tasker Variables.” If this is checked, any Tasker variables that you input into the Variable Value field will work like it will anywhere else in Tasker. If you were to enter %BATT in Variable Value and check the checkbox, the widget element using that Variable Name would display the current battery level. Using this with Tasker’s extensive range of built-in variables means that you can add info to your widget that you can’t do with the limited number of elements available in the widget creation apps themselves, like who called you last or something like that.

User-created variables

The final step up in terms of difficulty and potential usefulness is when you dwell into user created variables. There is practically no limit to what sort of data you can write to a variable, but at the same time, there’s also practically no limit to how complicated the setup can be. As an example, this is currently the most advanced setup I have in Tasker that involves gathering data:

Tasker queries (using HTTP Get) a custom PHP file that is stored on my server. This PHP file reads the Pocketables RSS feed for my articles, meaning an RSS feed that only displays my articles. It filters out the links to the articles and throws away everything else, then returns the URLs as a single string with each URL separated by commas. Tasker then splits this into separate URLs and checks each of these against a text file on my phone. If a match is found, nothing happens. If a match isn’t found, it adds the URL to the file. It then counts the number of URLs in the file and sends that number to a widget that displays the number of articles I’ve written in the current month. At the end of the month, Tasker writes a separator into the file and writes the current article count to a variable which is then subtracted from the count from there on out, making sure that the number visible in the widget is for the current month only even when the file contains multiple months worth of data. 

Sounds complicated? That one really is, using close to 100 Tasker actions and custom PHP code to eventually cause a 1-2 digit number to display in a widget. Once set up it’s fully automated, though, and it’s a great example of exactly how far you can take this system.

To learn how to do this, you really have to sit down and play with Tasker’s variable actions. It can seem confusing at first, but high school math comes in handy here. If you think of Tasker variables as variables in math, like X, that might help. You can even use math to modify variables. As an example, the built in variable %TIMES is the current time in seconds (since minutes, hours, days aren’t compatible with base 10 math). For example. if you have a work profile that activates when you’re at work and add an action to that which uses Set Variable %Enterwork to %TIMES when you go to work and Set Variable %Worktime to (%Enterwork – %TIMES)/3600 when you leave, then you would have a variable called %Worktime that would show you how long you were at work in hours, which could then be used in a widget. I use a similar variable to tell me how long I slept.

Message count variables

While the sky’s the limit for user created variables, there’s one very common use for this whole system: message counts. Have you ever seen people with widgets that display how many messages they’ve received, either on the home screen or the lock screen? Some of those are widgets from Google Play, but a lot of them come from Tasker combined with a custom widget app. These work by using notifications for new messages to add to a counter that shows how many messages you have. A tutorial for this is available on the XDA forums.

It’s also possible to further modify these counters, like for instance separate messages from important contacts. Instead of having “5 new SMS” you could have “2 new personal SMS. 3 new work SMS” or something like that. The sky truly is the limit.

Cross-device variables

In a previous article I showed how you can use Tasker to write information to a text file which is then synced to Dropbox using Dropsync. This file can then be accessed from other devices, essentially creating cross-device variables. I mentioned before that the example of showing you’re at work in a widget isn’t overly useful since you obviously know you are, but what if it’s not your own device that shows you at work, but the device of a family member? Imagine a family of Android users where a widget shows the status of everyone else. This is entirely possible to do, and while I’m sure teenagers would protest it as an invasion of privacy, I’m sure everyone will see the potential usefulness. There are services that show the location of your friends and family already, however the benefits of this system is that it’s much more adaptable. Whereas e.g. Apple’s Find My Friends is an on-demand GPS based tracking service, a Tasker based system can update based on e.g. calendar events, WiFi state, or manual status updates. Everything happens automatically, and can easily be configured to not use a lot of battery power or mobile data. Unfortunately it’s the kind of thing that is complicated to set up, but works automatically once it is.

IFTTT

If This Then That is a great web service that does a lot of the same things that Tasker does, some things it doesn’t, and perhaps more importantly: with less tech know-how needed. You specify an If trigger, e.g. a new status on Facebook, and a That event, e.g. “send me an email.” I use it for notifying me about new entries to certain RSS feeds via email, and it’s even the reason why Bryan got a second tablet. It has a tons of Ifs and Thats though, so the possibilities are endless here too.

So what does this has to do with parsing data to a widget? Well, IFTTT have a lot of Thats that can be used by Tasker, and in turn, be passed to a widget. SMS is one example, Email another, Dropbox a third. The Dropbox ability is especially useful because it can create and append files in your Dropbox folder. If you then were to Dropsync that folder to your phone and have Tasker read the contents of a specific file in it, you could link the two together.

As a practical example, let’s say you’re only reading this site for news about the HTC One X. You could then feed the Pocketables RSS feed into IFTTT, select “new feed matches,” type in “HTC One X,” and have it write the title of the new RSS entry to a text file in your Dropbox folder. The file would then be Dropsynced to your device, read by Tasker, and the title of the feed could be sent to a widget. As I said though, IFTTT has a lot of triggers, so this is just one of a near infinite amount of possibilities.

Updating the widgets

One final piece of advice if attempting this is regarding updating the widgets. The process of parsing info from the automation app to the widget app isn’t automatic, so if you just set up a widget variable to show %BATT it won’t show the battery status in real time unless you update the widget in real time – and that uses battery power. You have to run the plugin that updates the widget variable every time you want the widget to update, and choosing when that is can be tricky. If the data you’re parsing is something that changes often, you can run the widget plugin action on a timer or e.g. when the screen turns on. If it’s something that updates regularly, like my article counter, a timer is definitely a good way to go. If it’s something that changes value as part of another task, like my sleep mode indicator, you can simple add it to that task, like how my sleep mode activation task runs the widget plugin as the final action when activated.

A nice tip if you use this for a lot of data is to have one task that updates all the variables. Basically you would then just add a profile that runs a task on a timer (or something else) and then run multiply instances of the widget plugin action in order to update them all in one go. As an example, you could have a 2 hour timer which would run the widget plugin four times: one for SMS message count, one for the location of your wife, one for the amount of hours you’re been out running this month, and one for recent IFTTT-filtered Pocketables articles containing the word “Tasker.”

 In conclusion

Writing about features like this is hard because there’s quite a leap from what’s easy to what’s possible. There’s a lot of potential if you get into it though, and it’s possible to turn more or less anything into an information widget this way. If someone is struggling with a particular setup, post it here and I’ll see if I can help make it work.

Pocketables does not accept targeted advertising, phony guest posts, paid reviews, etc. Help us keep this way with support on Patreon!
Become a patron at Patreon!

Andreas Ødegård

Andreas Ødegård is more interested in aftermarket (and user created) software and hardware than chasing the latest gadgets. His day job as a teacher keeps him interested in education tech and takes up most of his time.

Avatar of Andreas Ødegård