Tasker Batteries aa

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.

We do like IF as a service, and it does a few nifty things right on your Android device, but there is just no match when you put it up against Tasker.

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.

Tasker Advanced Battery Log charts

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.

Tasker Battery Log Variable pluggedin

Select Variable.

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.

Tasker Battery Log Write File

Choose File.

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.

Tasker Battery Log Plugged In Task

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.”

Tasker Battery Log Variable unplugged

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”.

Tasker Battery Log UnPlugged Task

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“.

Tasker Battery Log Profile Power

Select State.

Select Power.

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.”

Tasker Battery Log Profile Exit Task

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.

Tasker Battery Log Profile

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.

Tasker Excel Battery Log Charts

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.

What’s next

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.

Next week

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?

  • TDN

    Is there a way to have Tasker tell when an app triggers a wake lock?

    • This is a double edged sword. Watching for wakelock could be a permanent wakelock itself, forcing your device to never deep sleep because Tasker is always running to watch for your device running. If that makes sense? Anyway, Tasker is pretty much designed to come to life, create its own wakelocks, whenever other apps wake the device. As this can happen many times per second as is, it may be too much drain on the battery to take action each time.

      I must ask what you are looking to accomplish? I mean, sure, you can use some serious workarounds to watch your device for wakelocks, or, at least track the awake time and activity, but really, if you are looking for a log, just use the logcat (ADB etc.) it already watches and reports logcats in a text file for off-device analysis.

      Aside from that, there is no readily available way to track wakelocks with Tasker, sorry. If you are interested in learning more, here is one of the best write-ups on the topic (warning, requires root): http://forum.xda-developers.com/showthread.php?t=1139927 or http://tasker.wikidot.com/deep-sleep-detective

      Good luck!

      • TDN

        Thanks Jonathan! I have read both of those write ups, excellent information there.

        I’m trying to find out if my Nexus 5’s battery is dead or if there is a rogue app keeping my device awake longer than it is supposed to. I have massive battery drain (from ~80% to 0% in minutes) while playing a game. I’m also seeing bad drain while the device is not in use, 100% to ~60% in 8 hours while in airplane mode.

        I am starting to believe it is the battery now, as I’ve eliminated most of my downloaded apps. My next step is to do a device wipe, but if it is the battery, that isn’t going to do anything. Do you have any other suggestions that I might try?

        • I see. Make sure you check out my ADB logcat tutorial (http://www.androidauthority.com/android-customization-battery-life-adb-logcat-598073/) before doing anything else.

          I was having bad battery life on my Nexus 9, running the ADB Logcat totally helped me out. What I found was, contrary to what I would have thought, turning off WiFi, or putting the device into airplane mode, was causing the problem.

          For me, I had apps like Pushbullet and Zooper Widget installed, which frequently wake the device, hit the internet, and go back to sleep – they ate battery, but not bad enough to uninstall. BUT when they couldn’t connect to the internet, they go into a stupid retry loop that is almost a constant wakelock, killing the battery in no time.

          I imagine there are other apps like this as well, like, an email or social media app that normally checks for messages every 15 minutes when connected, but retries connection every 30 seconds when it can’t connect.

          Anyway, hit that ADB logcat article, it’s actually pretty easy once your PC has the Android drivers, and it’s totally enlightening as to what and how your apps keep your device awake.

          • TDN

            Thanks again for all that info!

            I think the original culprit was Yahoo mail client, and after I uninstalled that I got a little better life, but I was still having issues. I do know about Zooper, IF, Tasker, Pushbullet and other apps that do the loop checks, but I don’t have any of them on my device. I think I will try the battery cycling and ADB next to see if I can find out what is going on.

          • TDN

            Hey Jonathan, I don’t know if you even care, but I identified the culprit. It appears that the memory leak that has supposedly been fixed in 5.1.1 treated its ugly head again in the update to fix the second stagefright threat. How I know this is because as soon as I upgraded to Marshmallow, my battery life went back to normal. I still have to put my phone through its paces to see if this is just a temporary thing or if it is actually fixed, but so far so good.

            Also I just wanted you to know that if you ever need help with Tasker ideas I have built several projects such as reading my texts to me when I’m driving, turning WiFi and data on and off as I connect to known networks and other fun stuff that make my phone work for me, instead of the other way around.

            Again, thanks for your help, I learned some more useful information that I really hope I don’t ever need again, lol!

          • Awesome to hear that you are back up and running. It’s really a shame about Lollipop, specifically a shame about ART, they have so much good going on, but these little memory issues and subsequent battery drain concerns are a real pain in the butt.

            Thank you for the offer to help with ideas, I am all ears to see what you guys would like to see and learn. Funnily enough, my project for this week touches on using location and WiFi connections to trigger other things. Tasker is really a great tool, I’ve been playing with it for years and still enjoy finding new stuff and ways to use it as well.

        • Almost forgot, it may also be a software issue, check out this forum thread where my Moto G was dying kind of like that. Eventually I solved it with some full discharge, full recharge cycles, but it was a total pain.


  • jason_pe

    Hi Jonathan, you can skip the entire csv step and write directly to a google spreadsheet(there’s a tasker plugin for that).

  • Cliff Tam

    Thanks for sharing this. I want to see if i can create a way to track average amp input when i am charging my phone ;)