Using Linux/shell & Tasker to create a technical battery widget

What you will learn

Some basic Linux/shell commands via a Terminal Emulator

Profile Aim

Extract battery data using Linux/shell commands via the terminal and pass to Tasker to create an impressive battery widget.

Simple Preparation

Download and install Android Terminal Emulator [by Jack Palevich] free from the Market

Make yourself a cup of tea.

Overview

For noobs, or even some seasoned Android/Tasker users, venturing into the world of Linux/shell/ADB is a leap that just seems a little too daunting and probably remains on the todo list.

If reading the above has already made you reach for a spare set of underwear and you’ve suddenly got an urge to watch the TV instead, under the assumption that this will cause you major brain-ache, then think again! What I will show you is actually very simple and I hope will leave you feeling suitably impressed with yourself.

As always, I will explain everything in as much detail as I can, bordering on patronising (apologies) in the hope that I leave no one confused…

Let’s get started!

Profile and Task Explanation

By using the Terminal we are able to access some hidden gems that many applications (some you possibly have already paid for) use to inform us of our system information. We are going to focus on a simple battery widget for now, although I’m sure what you learn will open a door to accessing much more information from your device, that will see you creating all sorts of informative widgets for your personal use – please share them!

Open the terminal application and type:

su

This simple command has set your ‘user id’ to superuser. This is no more complex that logging into your pc at home under the name of someone who has administrator privileges.

We are going to run commands under superuser because much of the data has restricted access unless you have certain permissions.

Next type:

dumpsys battery

Much to your surprise (perhaps) you’ll see a similar output to this:

Current Battery Service state:
  AC powered: false
  USB powered: true
  status: 2
  health: 2
  present: true
  level: 23
  scale: 100
  voltage:3864
  temperature: 204
  technology: Li-ion

Unfortunately, not all outputs are quite as user-friendly as this, but this one is great to work with and as you can see, supplies us with plenty of information for our battery widget.

Now, we could immediately export all of this information to a text file and then get Tasker to read that text file using the ‘read line’ action to populate the data into variables. But let’s have a look at a very simple command that can make life easier before we send to Tasker.

Looking at the ‘level: 23’ line, it maybe that you only want to make a widget showing the battery data – so let’s just get that.

Using the ‘grep’ command, we can easily narrow down the output with little effort. Try this command in the terminal:

dumpsys battery | grep level

The ‘|’ is known as a ‘pipe’ and in simple terms says ‘do this before you give me the output’. In the example above, grep has only supplied us with the lines that contain the word ‘level’.

Try the same for ‘temperature’:

dumpsys battery | grep temperature

Easy hey

So, let’s say that you’ve completed your battery level widget, but you hate the text ‘level:’ that appears before your battery percentage. To get rid of this, think of every output you get as appearing in an excel spreadsheet – each
‘word’ or ‘data section’ appears in its own column and you can easily select the individual columns you want to view.

Try this:

dumpsys battery | grep level | awk '{print $0}'

So, that didn’t change anything did it… You still got the full output – that’s because $0 means ‘everything’. Now try this:

dumpsys battery | grep level | awk '{print $1}'

Now try this:

dumpsys battery | grep level | awk '{print $2}'

Now try this:

dumpsys battery | grep level | awk '{print $1,$2}'

You’ve just learnt how to output all or some of the data you require.

TIP: If you’re having to retype the commands every time in the terminal, install a keyboard that has directional arrows on it (I use smart keyboard). Pushing ‘up’ on the arrows displays the last command you typed and then you can scroll across with the left arrow to edit just the parts you want, before pressing enter to execute. Simple.

Now you know how to extract just the data you want with the commands grep and awk, let’s assume that you’ve decided what text you want in front of the battery level and would like this to display in the output.

Try this:

dumpsys battery | grep level | awk '{print “CURRENT BATTERY LEVEL”$2}'

You’ll see that any text you put between the “ ” will output the exact text you’ve written. However, did you notice that there was no gap between the text and the battery level? Simply insert a comma to segment the output:

dumpsys battery | grep level | awk '{print “CURRENT BATTERY LEVEL”,$2}'

Right, so now you know how to extract just the data you want for your widget, let’s transfer it to Tasker. Using a command above, we’ll send just the battery percentage over and nothing else. Type this:

dumpsys battery | grep level | awk '{print $2}'

Now type this:

dumpsys battery | grep level | awk '{print $2}' > /mnt/sdcard/batterywidget.txt

Navigate to the root of your storage card and confirm that a text file containing just the current percentage of your battery has been created.

Next, let’s get just the temperature:

dumpsys battery | grep temperature | awk '{print $2}' >> /mnt/sdcard/batterywidget.txt

Navigate back to the same file and you’ll see this information has been appended underneath the percentage. The reason that this data was entered below the previous data (appended) was because of the double ‘»’ used above. A single ‘>’ means ‘send the output to this file’. It will overwrite anything the file currently contains. A double ‘»’ means ‘append the data to this file’.

Before we head over to Tasker to deal with the data, let’s do one more command. The health of my battery is currently showing as 2. That number means pretty much nothing to anyone – it could be 2/1,000,000. If we know that 1 = super, 2 is very good; 3 is good, 4 is poor and 5 is sh*t, then let’s translate that data first by using a very simple IF command.

Starting with:

dumpsys battery | grep health

Followed by:

dumpsys battery | grep health | awk '{print $2}'

Followed by:

dumpsys battery | grep health | awk '{if ( $2 == 1 ) print “Super”}'

If you got no output, try one of these:

dumpsys battery | grep health | awk '{if ( $2 == 2 ) print “Very Good”}'
dumpsys battery | grep health | awk '{if ( $2 == 3 ) print “Good”}'
dumpsys battery | grep health | awk '{if ( $2 == 4 ) print “Poor”}'
dumpsys battery | grep health | awk '{if ( $2 == 5 ) print “Sh*t”}'

I’m hoping you got an output and can see how the IF condition is selecting the correct output. We don’t want to use five different commands to get our answer though, so let’s join them together:

dumpsys battery | grep health | awk '{if ( $2 == 1 ) print "Super"; if ( $2 == 2 ) print "Very Good"; if ( $2 == 3 ) print "Good"; if ( $2 == 4 ) print "Poor"; if ( $2 == 5 ) print "Sh*te" }'

I very much hope that you are still with me at this point and suitably impressed with yourself!? The above code could be simplified using just ‘awk’ and no ‘grep’ or ‘cut’ or ‘sed’, but the format should enable you to see exactly what is going on and how you are getting the output you are! So we’ll stick with that…

Ok then, let’s take this puppy over to Tasker. Click on the task icon and select ‘New Task’. Name it ‘ShellBat’ and then select the + to add the first action. Select Misc and then Run Shell and in the command field enter:

dumpsys battery | grep level | awk '{print $2}' > /mnt/sdcard/batterywidget.txt

Tick the ‘use root’ box, choose a suitable icon (by pressing the icon button) and then press the green tick to save. Then press it again to save the task and again to exit Tasker. This makes sure the task is correctly saved in case your device crashes – if you don’t save all of the way out of Tasker, you progress could be lost.

Go back into Tasker and select the task ShellBat and press the play icon. Approve the superuser request (if prompted) and then open up a file explorer and check that the file sdcard/batterywidget.txt has populated with your current battery level correctly.

It has? Result! Next we need to get the battery health text in there too and to do that we need to add another run shell action in position 2 in the shellbat task. So, press + in the task, select Misc, run shell, tick use root and enter the following in the command field:

Note – If you are not viewing this tutorial on your phone and copying and pasting the commands over, at the end of this post I’ve included a text file with the commands in that you can drag to your sdcard and then copy and paste the contents from there – pressed the thanks button yet?? ;)

dumpsys battery | grep health | awk '{if ( $2 == 1 ) print "Super"; if ( $2 == 2 ) print "Very Good"; if ( $2 == 3 ) print "Good"; if ( $2 == 4 ) print "Poor"; if ( $2 == 5 ) print "Sh*te" }’ >> /mnt/sdcard/batterywidget.text

In the above command you’ll remember that we’ve used ‘»’ which will append the battery health underneath the current battery level.

Press the play button in the task and then navigate back to the text file to make sure it has populated correctly! It did? Whoop! Having that data safely in the text file, we now need to get it into Tasker. We do this using the read line action. In the task, select the + button, followed by file, followed by read line. Hit the magnifying glass and select batterywidget.txt. In the line field put the number 1 and in the var field type %BWLEVEL and press the tick to save. Press + again selecting another read line action, this time put 2 in the line field and name the var %BWHEALTH. Finally, we want to be able to check that everything is populating correctly without viewing variables and text files – so let’s flash the variable results on the screen. Press the + again and select, Alert, Flash and in the text field type %BWLEVEL %BWHEALTH. Tick the long button and then the tick to save. Press play in the task and hopefully see the contents of the two variables flash up on the screen!

I won’t talk you through making the widget itself – there’s a Minimalist Text tutorial above that talks you through how to get the variable information into a widget. You could also use the Zoom Widgets application that’s made by the dev of Tasker to knock something up too.

As far as how to update the information goes, you could run the task from a ‘display on’ context, so the data is refreshed every time you turn your phone on. Other options are to set a 5 minute interval between updates (when the display is on) or you could have the widget perform the task when clicked to manually refresh the data.

Other ideas would be to variable set %BWLEVEL to %BWLEVELR if it is less than 20. If that variable exists, then the widget would use a red text to display the battery level or %BWLEVELG (for green) if it is > 80.

Anyway, the point of this tutorial was to show you how to extract and pass the data using terminal commands and I very much hope I’ve shown you some handy basics.

Please post your screenshots if you make anything good!

As always, the thanks meter is the only way I’ll know to keep writing this stuff.

Have fun!

Credits: waydownsouth - For hours and hours of patient help

If anyone is struggling or if you have any questions, feel free to post in this monitored XDA thread and ask!

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License