[Android Software/Hacking General [Developers Only]] cazwell220:
Cool %Battery Drain/Hr Widget using Tasker and Minimalistic Text

Minimalist widget that displays:

- % of battery drain or charge per hour... ie "-6.89 /hr"

- time in hours remaining till fully charged or discharged at the accumulated average % drain per hour... ie "14.2 hrs"

- % of battery lost or gained in hours since plugged in or unplugged... ie "-2% in 0.29"

Requires Minimalistic Text App from Market: https://market.android.com/details?id=de.devmil.minimaltext&hl=en

If anyone has any suggestions on how to simplify it... let me know. :D

4 Profiles Total:

Profile 1 - OnBoot - Clears and sets variables for new battery drain/charge stats

Profile 2 - PluggedIn (AC or USB) - Clears and sets variables for new battery drain/charge stats

Profile 3 - BattCharging - Calculates %charge/hr, time till 100% charge and %battery increase since start of charging

Profile 4 - BattDraining - Calculates %drain/hr, time till 0% charge and %battery decrease since start of discharging

The first two profiles are designed to clear and reset the battery% and timestart variables when the phone boots up or when the power profile changes. The first one is the OnBoot profile which is a useful profile to have in the first place. You can have your phone start a program or do some other useful task when your phone first starts up.

The second one will activate when the phone is plugged in either via usb or the wall plug. This is also a useful profile for other reasons... for example you can make your timeout longer when the phone is plugged in and make it go back to a shorter timeout when it's unplugged with the exit task.

I would imagine some people already have an OnBoot and a Charging profile, so just add the following tasklist to them to keep from cluttering up your tasks if it's viable, otherwise just create the profiles as they are below.

Profile 1: OnBoot

Context: Event > System > Device Boot

Task:

1. Variable Clear: %BATTSTART

2. Variable Clear: %TIMESTART

3. Variable Set: %BATTSTART To %BATT

4. Variable Set: %TIMESTART To %TIMES

5. Minimalistic Text: Configuration - %BATTDRAIN = Please

6. Minimalistic Text: Configuration - %BATTLEFT = wait...

7. Minimalistic Text: Configuration - %TIMEBATT = (blank)

Profile 2: PluggedIn

Context: - Choose State > Power > Power > Source: Any

Task:

1. Variable Clear: %BATTSTART

2. Variable Clear: %TIMESTART

3. Variable Set: %BATTSTART To %BATT

4. Variable Set: %TIMESTART To %TIMES

5. Minimalistic Text: Configuration - %BATTDRAIN = Please

6. Minimalistic Text: Configuration - %BATTLEFT = wait...

7. Minimalistic Text: Configuration - %TIMEBATT = (blank)

Exit Task:

1. Variable Clear: %BATTSTART

2. Variable Clear: %TIMESTART

3. Variable Set: %BATTSTART To %BATT

4. Variable Set: %TIMESTART To %TIMES

5. Minimalistic Text: Configuration - %BATTDRAIN = Please

6. Minimalistic Text: Configuration - %BATTLEFT = wait...

7. Minimalistic Text: Configuration - %TIMEBATT = (blank)

The next two profiles are where all the math is done. One profile is setup for when the phone is plugged in and charging. The other is setup for when the phone is unplugged and discharging. An IF statement was implemented because Tasker seems to track battery changes very frequently even when the actual battery percent hasn't changed. So a variable called %BATTPREV was created to do a quick check... If the battery % hasn't changed then it doesn't do anything until it does change. Also... I personally have the results written to a battlog.txt file because I found it handy to see a log of data.

I used the Variable Section task to drop one of the decimal points... Precision to 3 decimals is not necessary I don't feel, but there is no way to round it to 1 or 2 decimals yet. I think to the 10ths is plenty for this application.

Profile 3: BattCharging:

Context: Event > Power > Battery Changed

Context: State > Power > Power > Source: Any

Task:

1. If - %BATT !~ %BATTPREV

2. Variable Set: %BATTUSED To %BATT - %BATTSTART - Check "Do Maths"

3. Variable Set: %TIMEBATT To (%TIMES - %TIMESTART)/3600 - Check "Do Maths"

4. Variable Section: %TIMEBATT From 1 Length 4

5. Variable Set: %BATTDRAIN To %BATTUSED/%TIMEBATT - Check "Do Maths"

6. Variable Section: %BATTDRAIN From 1 Length 4

7. Variable Set: %BATTLEFT To (100-%BATT)/%BATTDRAIN - Check "Do Maths"

8. Variable Section: %BATTLEFT From 1 to Length 4

9. Minimalistic Text: Configuration - %BATTLEFT = %BATTLEFT hrs

10. Minimalistic Text: Configuration - %BATTDRAIN = +%BATTDRAIN /hr

11. Minimalistic Text: Configuration - %TIMEBATT = +%BATTUSED in %TIMEBATT

12. OPTIONAL - Write File: battlog.txt Append:On - Text:"CHG: %DATE, %TIME, %BATT, +%BATTDRAIN / hr %UPS"

13. End If

14. Variable Set: %BATTPREV To %BATT

Profile 4: BattDraining:

Context: Event > Power > Battery Changed

Context: State > Power > Power > Source: Any INVERT!!

Task:

1. If - %BATT !~ %BATTPREV

2. Variable Set: %BATTUSED To %BATT - %BATTSTART - Check "Do Maths"

3. Variable Set: %TIMEBATT To (%TIMES - %TIMESTART)/3600 - Check "Do Maths"

4. Variable Section: %TIMEBATT From 1 Length 4

5. Variable Set: %BATTDRAIN To (%BATTUSED/%TIMEBATT)*-1 - Check "Do Maths"

6. Variable Section: %BATTDRAIN From 1 Length 4

7. Variable Set: %BATTLEFT To %BATT/%BATTDRAIN - Check "Do Maths"

8. Variable Section: %BATTLEFT From 1 to Length 4

9. Minimalistic Text: Configuration - %BATTLEFT = %BATTLEFT hrs

10. Minimalistic Text: Configuration - %BATTDRAIN = +%BATTDRAIN /hr

11. Minimalistic Text: Configuration - %TIMEBATT = -%BATTUSED in %TIMEBATT

12. OPTIONAL - Write File: battlog.txt Append:On - Text:"CHG: %DATE, %TIME, %BATT, -%BATTDRAIN / hr %UPS"

13. End If

14. Variable Set: %BATTPREV To %BATT

Finally.... Create a Minimalistic Text Widget size of 1x1 to display the data on your homescreen

Setup a Custom Layout with 3 Levels...

Level 1: Add Local Variable from the Misc tab: Variable Name: %BATTDRAIN, Style: Normal Override Size: 15

Level 2: Add Local Variable from the Misc tab: Variable Name: %BATTLEFT, Style: Normal, Override Size: 15

Level 3 (OPTIONAL): Add Local Variable from the Misc tab: Variable Name: %TIMEBATT, Style: Normal, Override Size: 10