Battery keeps charging if Mac fallen asleep
What is the issue? (required)
Battery will keep charging if Mac fallen asleep in the middle of a charging session, with battery maintain enabled.
Version
I used the CLI-only version v1.0.10
What exactly did you do to produce the issue? (required)
Let's say I set this script to maintain 60% charge. I am now at 50% charge. This sequence of events happened:
- I plugged in the charger;
- it started charging;
- my computer has fallen asleep;
- it will keep charging past 60%; (problem here)
- my computer wakes;
- it finds the actual charge is beyond 60% and stopped charging.
As a result, the final charge is well past 60%.
Expected behavior (required)
I would expect it to stay below 60%.
Error logs
03/31/23-14:14:47 - Charge above 55
03/31/23-14:14:47 - ππͺ« Disabling battery charging
03/31/23-18:56:02 - Charge below 55
03/31/23-18:56:02 - ππ Enabling battery charging
03/31/23-18:56:02 - πΌπͺ« Disabling battery discharging
# here: charger plugged in, charge below 55, started charging
03/31/23-23:10:04 - Charge above 55 # this is when my mac wakes up, charge at 70%
03/31/23-23:10:04 - ππͺ« Disabling battery charging
Possible Solution
My understanding about this problem is (correct me if it is wrong): when asleep, the main logic of the battery script will stop, so it cannot monitor the charge and stop charging, as a result, it will charge past the defined value.
I know this is not a real "bug" so I am here to find some possible solution.
- Maybe we can add an option to prohibit computer sleeping when the computer is still charging towards the predefined limit, so we can stop charging once it reaches the limit and enable sleep.
- Or we can stop charging just before computer enters sleep, and enable charging once it wakes up. So it will not keep charging when sleeping. (shell script alone, this can be hard to achieve)
A few thoughts on the possible solutions I mentioned before:
prohibit computer sleeping when the computer is still charging towards the predefined limit
using caffeinate may achieve this
stop charging just before computer enters sleep, and enable charging once it wakes up
It is probably difficult for a shell script to solve. It requires registering sleep notifications to IOKit using a system programming language like C. Or some third-party app like sleepwatcher may be required.
I would like to contribute but it seems there is no quick solution to this problem (due to the limitation of a shell script).
I really want to solve this quickly, so I have no choice but to spent a weekend giving my ideas a try, and came up with charlie0129/batt, which is written in Go and C. In addition to charge-limiting, it should be able to handle these sleep-related edge-cases fairly well :)
okay so here's what I've been doing:
$ cat sleep.sh
#!/bin/bash
if [ "$1" == "true" ]; then
sudo pmset -a disablesleep 0
echo "sleep set to true"
elif [ "$1" == "false" ]; then
sudo pmset -a disablesleep 1
echo "sleep set to false"
else
echo "wrong argument!"
fi
So I'd go
./sleep.sh false
battery maintain 80
and close the lid after making the display go to sleep (I have it set as one of my hot corners) because otherwise the screen doesn't turn off after you close the lid.
And then I go ./sleep.sh true to revert the not sleeping settings later.
Edit; this doesn't require any 3rd party apps to use. Edit2; go here for the latest version of the script and here for the solution I made
Glad to see your suggestion! @Paranoid-Dev
It should work well if my mac doesn't need to sleep often. But I use my MacBook outside fairly often, so closing the lid and sleep is a regular thing.
It seems the mac cannot sleep after you disabled it, unless sleep is enabled manually. So, for my use case, manually running a script every time before I want to sleep, which happens very often, seems a slight mental burden for me...
Hence, in my previous comment, I chose to write a program to do such things automatically through macOS power management using IOKit so that my computer can be used and sleep as usual :p )
@charlie0129 I might switch over to your program if the issue where Battery gets confused with previous values persists :P
I just wanted to share the script for people looking for a quick qnd easy fix for the sleeping problem.
Hey @charlie0129!
Did you test the stability of this? If so can you create a PR for this?
As a simple user of this nice app, I only like to inform that my "solution" to the problem is the following: I set the upper limit of charging to 70%, and so far I haven't seen any overdraft when charging overnight :)
Sorry, overdraft over 80%.