Skip to content

Automated MacBook battery health monitor. Tracks cycle count and capacity over time, storing data in a CSV file and generating insightful plots. Easy to set up with cron job automation for effortless long-term monitoring.

License

Notifications You must be signed in to change notification settings

CedricDeVijt/MacOS_Battery_Tracker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MacOS Battery Tracker

Overview

MacOS Battery Tracker is an automated tool designed to monitor and analyze the health of your MacBook's battery over time. By tracking key metrics such as cycle count and maximum capacity, this application provides valuable insights into your battery's performance and degradation patterns.

Features

  • Automated Data Collection: Regularly captures battery cycle count and maximum capacity.
  • Data Persistence: Stores collected data in a CSV file for long-term tracking.
  • Visual Analytics: Generates time-series plots to visualize battery health trends.
  • Effortless Monitoring: Eliminates the need for manual checks through system reports.
  • Organized Data Storage:
    • CSV data is stored in the data directory.
    • Generated plots are saved as PNG files in the graphs directory.

Example Output

Below are sample visualizations generated by the MacOS Battery Tracker:

Battery Capacity Over Time Battery Cycle Count Over Time

Installation

  1. Clone the repository:

    git clone https://github.com/CedricDeVijt/MacOS_Battery_Tracker.git
    cd MacOS_Battery_Tracker
    
  2. Set execution permissions for the script:

    chmod +x battery_tracker.sh
    

Usage

Run the script manually:

./battery_tracker.sh

After running the script, you can find:

  • The CSV file with battery data in the data directory.
  • The generated plots in the graphs directory.

Automation

Option 1: Using the Automation Script

  1. Make the automation script executable:

    chmod +x automate.sh
    
  2. Run the automation script:

    ./automate.sh
    

This will set up a cron job to run the battery tracker every Monday at 12:00 PM.

Option 2: Manual Cron Job Setup

  1. Open your crontab file:

    crontab -e
    
  2. Add the following line (adjust the path as necessary):

    0 12 * * 1 /path/to/battery_tracker.sh
    
  3. Save and exit the crontab editor.

Configuration

You can modify the cron job schedule by editing the automate.sh file or by manually adjusting the crontab entry. Refer to crontab.guru for assistance with cron job syntax.

Dependencies

  • Python 3
  • pandas
  • matplotlib

Dependencies are automatically installed in a virtual environment when running the battery_tracker.sh script.

Troubleshooting

If you encounter issues with cron job execution, check the system logs:

grep CRON /var/log/syslog

Technical Details

Program Structure

The MacOS Battery Tracker consists of several key components:

  1. battery_tracker.sh: This shell script serves as the entry point for the application. It sets up a Python virtual environment, installs dependencies, and runs the main Python script.

  2. battery_tracker.py: This is the core Python script that performs the following tasks:

    • Retrieves battery data using system commands
    • Updates the CSV file with new data
    • Generates plots for visualization
  3. automate.sh: This script automates the process of setting up a cron job for regular execution of the battery tracker.

Data Collection

The program uses the system_profiler command-line tool to retrieve battery information:

cmd = ["/usr/sbin/system_profiler", "SPPowerDataType"]
result = subprocess.run(cmd, capture_output=True, text=True)

It then parses the output to extract the cycle count and maximum capacity.

Data Storage

Battery data is stored in a CSV file (data/battery_health_tracker.csv) with the following structure:

Date,Cycle Count,Maximum Capacity (%)

The script checks if the cycle count has changed before adding a new entry, ensuring that data is only recorded when there's a meaningful change.

Visualization

The program uses matplotlib to create two types of plots:

  1. Battery Capacity Over Time
  2. Battery Cycle Count Over Time

These plots are saved as PNG files in the graphs directory.

Contributing

Contributions to the MacOS Battery Tracker are welcome! Please feel free to submit pull requests or create issues for bugs and feature requests.

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

Automated MacBook battery health monitor. Tracks cycle count and capacity over time, storing data in a CSV file and generating insightful plots. Easy to set up with cron job automation for effortless long-term monitoring.

Topics

Resources

License

Stars

Watchers

Forks