jump to content

Logging laptop battery life

1005 ha netbook in shiny whiteI bought an Asus EeePC 1005HA Seashell net book for Christmas with Windows 7 and have been very impressed with battery life and lightness. Still… I wasn’t sure if Sleep mode was working as expected, so I logged the battery life for a couple of days under normal use.

I didn’t use any rigorous scientific testing — ‘normal use’ for me was watching a bit of YouTube or iPlayer, checking my email, using Facebook and that kind of thing. If power was still being consumed in Sleep mode, then I would expect big falls in power levels as the net book logged the level after coming out of Sleep.

Results

Download BatteryStatusData for CSV data file of this graph.
line graph of battery life of my network in percentage remaining and calculated number of hours left
The gaps in the hours remaining green line are where I’ve removed data that reported -1 hours remaining.

Hours remaining appears to be calculated depending on the current task when the log was taken. For example, if I was watching a video, the projected hours remaining if I kept just watching videos is less than if I closed the video and browsed the web.

Percentage remaining shows a steady decline. Logging only takes place when the laptop is open, so this is as expected if sleep mode is working as expected. Looks like everything is working as it should be.

Logging battery life

Download BatteryLogging.exe and Windows 7 scheduled task.

The battery logger is a scheduled task that runs a simple C# console application regularly. It requires at least version 2.0 of the .net framework due to the PowerStatus class.

1.  string logFile = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop), "BatteryStatus.csv");
2.
3.  if (!System.IO.File.Exists(logFile))
4.    System.IO.File.WriteAllText(logFile, "Date,Life in seconds,Life in percentage" + System.Environment.NewLine);
5.
6.  string CSV = string.Join(",", new string[] {
7.    System.DateTime.Now.ToString("s"),
8.    System.Windows.Forms.SystemInformation.PowerStatus.BatteryLifeRemaining.ToString(),
9.    System.Windows.Forms.SystemInformation.PowerStatus.BatteryLifePercent.ToString(),
10.   System.Environment.NewLine});
11.
12. System.IO.File.AppendAllText(logFile, CSV);

Code breakdown

1. logFile uses the SpecialFolder enum to create a file path to the user’s desktop
3. Checks whether the file already exists.
4. WriteAllText will create the file and add the headings of the columns. Environment.NewLine is used to avoid hard-coding in line breaks.
6. CSV will hold line of data to add to the CSV file. string.Join creates the comma separated list.
7. "s" is the standard date and time format for the sortable date time pattern e.g. 2010-01-17T13:45:30.
8. BatteryLifeRemaining gets the approximate number of seconds of battery time remaining using the PowerStatus class.
9. BatteryLifeRemaining gets the approximate percentage of full battery time remaining and appears to be a bit more accurate sometimes for my netbook than BatteryLifeRemaining.
12. AppendAllText open, adds the line of data and closes the file.

Creating a Window 7 scheduled task

Press the Windows key and enter ‘task scheduler’ into the search box. Select Task Scheduler from the Programs section.

screen shot of task scheduler

Select Create Task… from the Actions menu.

screen shot of creating a new scheduled task

Complete the fields Name and Description in the General tab. Leave the other options as default.

screen shot of creating a scheduled task

From the Triggers tab, select New… Under advanced settings, make sure Repeat task and Enabled are set.

screen shot of setting a scheduled task trigger

When the task trigger has been hit, we want the BatteryLogger program to run.

screen shot of scheduled task actions

By default, scheduled tasks are only run when the computer is plugged into the power socket.

screen shot of scheduled task conditions

The new task is ready to run.

screen shot of completed scheduled task

Leave a reply