Last week on our Android customization series, we took to IF and Box to create and save a simple text file battery log for your device. The idea is pretty effective, but we found a few kinks in the software. This week, we’ll be taking it to a whole new level, using Tasker to create a highly effective and customizeable battery log, saved right on your device.
It feels like it is becoming somewhat less necessary these days to clock and optimize your Android device’s battery consumption, some devices have much larger battery capacities, some have quick charge capability, and others yet have both. If you still need to see what’s going on, or just want to track it for fun, here is our basic take on using Tasker to create an advanced battery log.
Before we begin
Tasker - Google Play StoreAs you can imagine, you will require a copy of Tasker to follow along today. Tasker is $2.99 in the Google Play Store and can do so much more than create just a simple battery log. Check out some of our other Tasker projects for an idea what this app can do for you.
Optional: you may want to have a PC or other device with functioning spreadsheet application for today as well. This will be for reporting purposes only, I’ll show you in a bit. Microsoft Office Excel is an old favorite for many while Google Sheets is a new favorite for others.
Advanced battery log using Tasker
As is usually the case with Tasker, the hardest part is going to be deciding exactly what you want to do, and how you want to do it. I have to say, this is why I love Android, I never ask what the device can do, I only ask how to make it do what I want, and there is almost always an answer.
For today, we will create a pretty basic rendition of this advanced tracker, we will save the data in a DB friendly format, look at it on some graphs and maybe make some usage alarms after that.
Let’s get started with the battery tracker.
As simple as I am about to try to make this, I want to say that we will be skipping some of the better coding techniques and pushing a few more buttons than might otherwise be required, all in an effort to make things easier to follow. That said, this is still going to get a little confusing at times. If you find you don’t exactly know what is happening, just follow through and hopefully it will all make sense in the end.
Create your data file
Before we can start in on Tasker, we need to create our file that we will be saving data to. I will leave you to figure out if your favorite file explorer will allow you to create files, or if you need to transfer one over from your PC.
Create a blank text file with the .csv file extension and save it somewhere on your Android device. I will call mine simply “Battery.csv“.
Take note of where you save this file on your device, you will need to track it down later.
Create your Tasker Tasks
For the more advanced Tasker users in the crowd, ones that can appreciate object oriented programming, think about creating a ‘File-Writer’ Task that accepts variable(s) to build your string to be saved. If that made no sense to you, here’s what we’re going to do today:
We will create a separate Task for each time we wish to save data to the log. Remember that you will have to choose these times and code appropriately for each, but don’t worry it’s actually pretty easy.
Let’s start with a Task that fires when you plug in your phone.
Open Tasker, create a new Task and name it appropriately, I’ll call mine “BatteryPluggedCSV.”
As the name implies, we won’t be saving to a normal .txt file today, instead we’ll use the .csv alternative, which allows the file to import into your favorite spreadsheet application, more on that later.
Press the “+” button to add your first action.
Select Variable Set.
Provide a variable Name, use all caps to denote it as a global variable, just in case. I’ll call mine “BATCSV“.
In the To field, we will enter the data that we wish to save. Now, it is important to note that you can create any text string here, like, you could put “I plugged in my phone at %TIME, with power level %BATT.” As easy as this is to read, it is not effective for use in a .csv file when you try to import to a spreadsheet. Instead, we will save the data without embellishment with a comma “,” in between each piece of data, you recall that csv stands for Comma Separated Values, correct? So, your To: field will look something like this:
True, AC Plugged In, %DATE, %TIME, %BATT, %UPS, %MEMF
Where “True” is a Boolean that we will use to denote power state, i.e. true = plugged in, false = unplugged. Our second entry will be our reason for action, in this case we are saving the data because we plugged in our device, there are many other reasons you might enter here – we’ll explain more later.Then, as you probably guessed, the remaining elements starting with the percent symbol are built-in Tasker variables for the various system stats. I’ll leave %DATE, %TIME and %BATT to your imagination, %UPS is system up time in seconds and %MEMF is your amount of free RAM.
Now hit that system Back button to save and exit out of this action.
Hit the “+” button to add your next action.
Choose Write File.
Select the magnifying glass to search for your Battery.csv file you created earlier.
In the Text field, you can simply enter your variable, mine was called “%BATCSV“. (Please note that my image has a different variable name, it’s from my other project, sorry.)
Turn on the option for Append.
Turn on the option for Add Newline.
Hit that system Back button to save and exit.
Do you see what is happening? First we created a variable that contained all of the info we wished to save, then we simply wrote it on a new line in the csv file.
So far, this is nothing overly new from last week’s project, except that you have more control over what info is available to save, you have more control over when the data is saved, you can use the saved data in new ways and more.
Now, let’s make a new Task for when you unplug your device.
This is pretty easy, actually. Use all of the steps from the previous Task, except, when you are setting the value of our already existing %BATCSV variable, enter “False” at the beginning of the string, then enter something like “AC Unplugged” in place of “AC Plugged In.”
Remember that Boolean? You got it, false = unplugged, otherwise, our reason for action is different, but from there, for our purpose today, we want to save the exact same data, which is done in the exact same way as the plugged in Task. For reference below, I called mine “BatteryUnpluggedCSV”.
Other Tasks for other triggers
We will not walk you through any other triggers or Tasks today, but you are welcome to jump ahead and build as many unique battery info Tasks as you can imagine. There are plenty more times than just plugging in or unplugging that I would want to save data points, I’d want to see low battery warnings, overheating battery warnings, maybe even an entry once per hour, just to see how things are coming along.
As I eluded earlier, you can utilize variables into a dedicated “file writer” Task, to prevent the code duplication required here, but if you ignore that and create brand new Tasks for each trigger, just copy the above steps, keep the Boolean to indicate your plugged-in state regardless the trigger, then update your reason for action (trigger) entry, including things like “Low Battery”, “AC Plugged In”, “USB Plugged in”, “Device Unplugged”, “Hourly Update”, “WiFi/Bluetooth radio activated”, “WiFi/Bluetooth radio deactivated” and so much more.
Let’s explore this a little as we create our Tasker Profiles.
Battery state Tasker Profile
As it turns out, our ‘plugged in’ and ‘unplugged’ Tasks can be controlled by one Profile, as follows:
Create a new Profile, if asked, give it an appropriate name, I called mine “LogPluginCSV“.
Select Power (again).
Leave the Source on Any for now, but know that you can recreate this project for each power input type, to really track your charging speeds and quality per source.
Tap the system Back button to save and exit.
Choose your battery plugged in Task, mine was called “BatteryPluggedCSV.”
Now, long press on the name of your Task in the Profile overview to get the extra menu options.
Select Add Exit Task.
Select your battery unplugged Task, I didn’t give you the name of mine above, but it was “BatteryUnpluggedCSV.”
That is it for this Profile, folks. Now, any time you plug in or then unplug your device, Tasker will write a line of data to your Battery.csv file.
You may now consider what other times of the day you want to collect battery data. Perhaps you would like to create a Profile that saves the data every hour, every time you turn on the display or maybe before and after each phone call or when you open and close a specific app. This is where Tasker seriously outperforms IF for collecting battery data.
Think through your day, decide at which points you would like to take a battery reading and design the Tasker Profile to collect it. Whenever possible, utilize one of the previously created Tasks to go with your new Profiles, things can get out of hand very easy here, but if you want, go ahead and create as many new Tasks for each entry as you’d like, we won’t stop you.
Viewing your data
As we’ve discussed already, a .csv file is capable of importing into a spreadsheet for better viewing. From there, you can go ahead and make graphs and otherwise order, arrange, manipulate and perform calculations on your data.
I will admit that you can do most of the above calculations, aside from graphing, using the Variable tools in Tasker, but that’s not what we’re after today. Let’s look at what I’ve done with some of my data.
I won’t bother you with how to make charts in Excel or Google Sheets, but the data is all there, go have fun. If you want to really take things up a notch, your data can easily import into a full database program for some really powerful usage tools and long term analysis.
I realized too late that the Free Memory data provided by Tasker includes the available RAM and the unit of measure, example, 308MB. This was a little disappointing when I got into Excel, as the “MB” would need to be stripped out of each entry to be able to do calculations. Go ahead and find a workaround if this bothers you – Tasker’s variable manipulation actions will allow you to identify the letters, strip them from the string, then ensure that the remaining numbers are saved as int for calculations.
From there, the same variable management can be used to perform calculations on your collected data before saving to your .csv file. It breaks all the rules of good database management to save calculations, but I won’t blame you if your data is headed to a spreadsheet only, that’s not a real database anyway.
Once you are comfortable with this project, do keep in mind that there are many other aspects of your device that you can collect data for. Perhaps you want to log an entry with CPU utilization whenever your processor reaches 100% usage. Maybe you want a notification when your free memory drops below 100MB. What about creating a separate log file to track all the time you spend on certain apps.
My point is, this basic project can be used as a template to track and quantify a lot of things in your world. Stay tuned, we’ll be running through a few of these in the coming weeks.
I hope the seeming lack of focus didn’t throw you off course this week, our Android customization is really pretty handy once you get used to it, and Tasker is just a ton of fun for us. Next week we will take a slightly different approach to this project, using the same concepts, we will take a more proactive stance on your battery consumption.
What do you say, is collecting battery data and using it to further understand your device and device usage worth your time?