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.
- Macbook
- Smart Plug or Smart Power Strip with individual control that has IFTTT support
- IFTTT account
- Signup for an IFTTT account
- Create an IFTTT applet
- Select
WebHook
service forIf This
condition - Select your smart plug integration for
Then That
condition - Get your maker webhook url via maker documentation
- to get this, go to your account > services > WebHooks > Documentation
- it would like something like this: https://maker.ifttt.com/trigger/{event}/with/key/{yourMakerKey}
- 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}
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
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>
- Make sure to update the file location at line 10 to the location where you stored the AppleScript you've created
- You can change the interval at line 15 based to your liking, currently set to
300
seconds - For best practice save this file similarly to your label, example would be
automate-battery.plist
- If you get an error saving the
.plist
file in/Library/LaunchAgents/
, try saving it again with admin privilages
Automation all set!