Skip to content

Just a quick guide on how I automated the charging process of my MacBook.

Notifications You must be signed in to change notification settings

xtianatilano/automate-mac-battery

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 

Repository files navigation

How I automated charging my Macbook

Why

I know MacOS already has a smart battery management.
This is just solely my preference, I don't like leaving my MacBook plugged in all the time, also I want to cycle my battery properly to prolong battery life(?).
Lastly, I'm lazy to turn on/off my charger every time my battery is full or low.
So if your in the same boat as me, feel free to follow the guide.

To achieve what I want, I decided to buy a cheap Smart Power Strip that has individual control and supports IFTTT.
You can also do this with a smart plug that has IFTTT support if you just want one specifically for your MacBook.

Requirements

  1. Macbook
  2. Smart Plug or Smart Power Strip with individual control that has IFTTT support
  3. IFTTT account

How

IFTTT:

  1. Signup for an IFTTT account
  2. Create an IFTTT applet
  3. Select WebHook service for If This condition
  4. Select your smart plug integration for Then That condition
  5. Get your maker webhook url via maker documentation
  6. replace {event} with the event name you created

example maker url to turn off:

event name = mac_battery_low
So my url is: https://maker.ifttt.com/trigger/mac_battery_low/with/key/{yourMakerKey}

MacBook:

Open AppleScript Editor, paste the code block below:

set chargeState to do shell script "pmset -g batt | awk '{printf \"%s %s\\n\", $4,$5;exit}'"
set percentLeft to do shell script "pmset -g batt | awk -F '[[:blank:]]+|;' 'NR==2 { print $4 }'"

considering numeric strings
    -- here you can set the percentage which you want to turn on the smart plug (currently set to 15)
	if chargeState contains "Battery Power" and percentLeft  15 then
        -- replace {{makerUrlToTurnOnPlug}} to your webhook url to turn on the smart plug
	    do shell script "curl -X POST {{makerUrlToTurnOnPlug}}"
	end if

	-- here you can set the percentage which you want to turn off the smart plug (currently set to 90)
	if chargeState contains "AC Power" and percentLeft  90 then
        -- replace {{makerUrlToTurnOffPlug}} to your webhook url to turn off the smart plug
	    do shell script "curl -X POST {{makerUrlToTurnOffPlug}}"
	end if
end considering

Save this wherever you want.

Quick explanation for this script:

  • It gets the battery percentage using shell script
  • It checks if the battery percentage is greater/less than the target values, then it will send a webhook to IFTTT maker url to trigger the command for your smart plug/smart power strip

launchd:

Next, we would need a launchd or a cron (in Linux terms) to execute the AppleScript we've just created in intervals.
We can do this in MacOS by creating a .plist file in /Library/LaunchAgents/
to understand more aboutlaunchd.plist, you can read this documentation.

Open your favorite code editor, then paste the code block below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>automate-battery.job</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/osascript</string>
        <string>location/of/your/applescript.scpt</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StartInterval</key>
    <integer>300</integer>
</dict>
</plist>
  1. Make sure to update the file location at line 10 to the location where you stored the AppleScript you've created
  2. You can change the interval at line 15 based to your liking, currently set to 300 seconds
  3. For best practice save this file similarly to your label, example would be automate-battery.plist
  4. If you get an error saving the .plist file in /Library/LaunchAgents/, try saving it again with admin privilages

Automation all set!

About

Just a quick guide on how I automated the charging process of my MacBook.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published