Coder Social home page Coder Social logo

battery's Introduction

Battery charge limiter for Apple Silicon Macbook devices

This tool makes it possible to keep a chronically plugged in Apple Silicon Macbook at 80% battery, since that will prolong the longevity of the battery. It is free and open-source and will remain that way.

Want to know if this tool does anything or is just a placebo? Read this excellent article. TL;DR: keep your battery cool, keep it at 80% when plugged in, and discharge it as shallowly as feasible.

Requirements

This is an app for Apple Silicon Macs. It will not work on Intel macs. Do you have an older Mac? Consider the free version of the Al Dente software package. It is a good alternative and has a premium version with many more features.

Installation

  • Option 1: install the app through brew with brew install battery
  • Option 2: download the app dmg version here
  • Option 3: install ONLY the command line interface (see section below)

When installing via brew or dmg, opening the macOS app is required to complete the installation.

The first time you open the app, it will ask for your administator password so it can install the needed components. Please note that the app:

  • Discharges your battery until it reaches 80%, even when plugged in
  • Disables charging when your battery is above 80% charged
  • Enables charging when your battery is under 80% charged
  • Keeps the limit engaged even after rebooting
  • Keeps the limit engaged even after closing the tray app
  • Also automatically installs the battery command line tool. If you want a custom charging percentage, the CLI is the only way to do that.

Do you have questions, comments, or feature requests? Open an issue here or Tweet at me.


๐Ÿ–ฅ Command-line version

If you don't know what a "command line" is, ignore this section. You don't need it.

The GUI app uses a command line tool under the hood. Installing the GUI automatically installs the CLI as well. You can also separately install the CLI.

The CLI is used for managing the battery charging status for Apple Silicon Macbooks. Can be used to enable/disable the Macbook from charging the battery when plugged into power.

Installation

One-line installation:

curl -s https://raw.githubusercontent.com/actuallymentor/battery/main/setup.sh | bash

This will:

  1. Download the precompiled smc tool in this repo (built from the hholtmann/smcFanControl repository)
  2. Install smc to /usr/local/bin
  3. Install battery to /usr/local/bin

Usage

Example usage:

# This will enable charging when your battery dips under 80, and disable it when it exceeds 80
battery maintain 80

After running a command like battery charging off you can verify the change visually by looking at the battery icon:

Battery not charging

After running battery charging on you will see it change to this:

Battery charging

For help, run battery without parameters:

Battery CLI utility v1.0.1

Usage:

  battery status
    output battery SMC status, % and time remaining

  battery maintain LEVEL[1-100,stop]
    reboot-persistent battery level maintenance: turn off charging above, and on below a certain value
    eg: battery maintain 80
    eg: battery maintain stop

  battery charging SETTING[on/off]
    manually set the battery to (not) charge
    eg: battery charging on

  battery adapter SETTING[on/off]
    manually set the adapter to (not) charge even when plugged in
    eg: battery adapter off

  battery charge LEVEL[1-100]
    charge the battery to a certain percentage, and disable charging when that percentage is reached
    eg: battery charge 90

  battery discharge LEVEL[1-100]
    block power input from the adapter until battery falls to this level
    eg: battery discharge 90

  battery visudo
    ensure you don't need to call battery with sudo
    This is already used in the setup script, so you should't need it.

  battery update
    update the battery utility to the latest version

  battery reinstall
    reinstall the battery utility to the latest version (reruns the installation script)

  battery uninstall
    enable charging, remove the smc tool, and the battery script

FAQ & Troubleshooting

Why does this exist?

I was looking at the Al Dente software package for battery limiting, but I found the license too limiting for a poweruser like myself.

I would actually have preferred using Al Dente, but decided to create a command-line utility to replace it as a side-project on holiday. A colleague mentioned they would like a GUI, so I spend a few evenings setting up an Electron app. And voila, here we are.

"It's not working"

If you used one of the earlier versions of the battery utility, you may run into path/permission issues. This is not your fault but mine. To fix it:

sudo rm -rf ~/.battery
binfolder=/usr/local/bin
sudo rm -v "$binfolder/smc" "$binfolder/battery"

Then reopen the app and things should work. If not, open an issue and I'll try to help you fix it.

A note to Little Snitch users

This tool calls a number of urls, blocking all of them will only break auto-updates.

  1. unidentifiedanalytics.web.app is a self-made app that tracks app installations, I use it to see if enough people use the app to justify spending time on it. It tracks only how many unique ip addresses open the app.
  2. icanhazip.com is used to see if there is an internet connection
  3. github.com is used both as a liveness check and as the source of updates for the underlying command-line utility
  4. electronjs.org hosts the update server for the GUI

All urls are called over https and so not leak data. Unidentified Analytics keeps track of unique ip addresses that open the app, but nothing else.

What distinguishes this project from Optimized Charging?

Optimized Charging, a feature that is built into MacOS, aims to ensure the longevity and health of your battery. It does so by "delaying charging the battery past 80% when it predicts that youโ€™ll be plugged in for an extended period of time, and aims to charge the battery before you unplug," as explained in Apple's user guide.

Additionally, Optimized Charging uses machine learning to decide when the battery should be held at 80%, and when it should become fully charged. If your Mac is not plugged in on a regular schedule, optimized charging will not work as intended.

This app is a similar alternative to Optimized Charging, giving the user control over when it is activated, what percentage the battery should be held at, and more.

How do I support this project?

Do you know how to code? Open a pull-request for a feature with the label help wanted (PR welcome).

Do you have an awesome feature idea? Add a feature request

Do you just want to keep me motivated to update the app? Tweet at me

battery's People

Contributors

actuallymentor avatar adowls avatar alexkarpitski avatar bjarketrux avatar chr1s70ph avatar clemenstyp avatar dalisoft avatar evolutionxbox avatar georgfleig avatar giorgioren avatar gotgenes avatar ibrado avatar igorkulman avatar johnmcdowell avatar kyrosweb avatar landabaso avatar lcsvcn avatar lephilippe avatar lodewijkvankeizerswaard avatar mihaiconstantin avatar mineiwik avatar olivierbnt avatar qpenko avatar rguca avatar ryanburnett avatar safferli avatar scienceowl13 avatar somebdyelse avatar thdrmrphy avatar weiyou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

battery's Issues

Menu bar icon suddenly white

After installing 1.0.6 and also upgraded to 1.0.7 the menu bar icon is now whiteโ€”whereas it used to be black to match the rest of the icons.

Icon not updating

I ran the command battery charge off and got as a response:

23:22:59 - Enabling battery charging
23:22:59 - Battery at 100% (0:00 remaining), smc charging enabled
23:22:59 - Charging to 80% from 100%
23:22:59 - Enabling battery charging
23:22:59 - Disabling battery charging
23:22:59 - Charging completed at 100%

But the icon of powering is still shown on the menu bar, is it working?

How does this handle sleep?

I looked hard and couldn't find anything anywhere that explains what happens when using this program when you sleep your Mac. Also, I don't see any mention of what Apple's settings should be set to when using this app.

The tool stopped working in MacOS Ventura

I tried installing it from the source [the standard installation (brew and DMG) is broken, doesn't install SMC (typo in install command line), and permission problems in battery.log file]

disabled charging (battery charging off) - Nothing happens even though the logs say it worked.

Error when running battery charging off with latest version

After updating the app, when I run battery charging off, I keep getting the following error,

touch: /Users/xxx/.battery/battery.log: Permission denied 15:47:15 - Setting charging to off touch: /Users/xxx/.battery/battery.log: Permission denied touch: /Users/xxx/.battery/battery.log: Permission denied 15:47:15 - Enabling battery charging touch: /Users/xxx/.battery/battery.log: Permission denied 15:47:15 - Battery at 63% ((no remaining), smc charging enabled 15:47:15 - Disabling battery charging

I am unable to access the .battery folder. I have tried deleting the folder and reinstalling battery as well. Still get the same error.

I also ran sudo battery visudo. Still have the same error.

Version 1.0.5 doesn't update to 1.0.6 [Homebrew]

What is the issue? (required)
Homebrew installation doesn't find the new version (1.0.6)

What exactly did you do to produce the issue? (required)
With the battery update command it keeps the 1.0.5 version

This will run curl -sS https://raw.githubusercontent.com/actuallymentor/battery/main/update.sh | bash
Press any key to continue

๐Ÿ”‹ Starting battery update

[ 1/3 ] Cloning battery repository
[ 2/3 ] Writing script to /usr/local/bin/battery
[ 3/3 ] Removed temporary folder

๐ŸŽ‰ Battery tool updated.

Screenshots (optional)
image

Not Working

Battery continues to charge at 100%. Even in iTerm2 when i run battery charging off it says it is disabling battery charging but upon checking it does not say it is not charging anymore

Impossible to install the dmg on M1 pro 14"

What is the issue? (required)
Impossible to install the battery software using the option with the dmg file.

What exactly did you do to produce the issue? (required)
After copying the dmg to the application folder,
Launching the dmg for the installation leads to multiples errors :
Capture dโ€™eฬcran 2023-02-05 aฬ€ 17 22 33
Capture dโ€™eฬcran 2023-02-05 aฬ€ 17 22 55
Capture dโ€™eฬcran 2023-02-05 aฬ€ 17 23 08
Capture dโ€™eฬcran 2023-02-05 aฬ€ 17 23 16

Then the installation finished and the icon is on the top bar and on the application list, but it is impossible to launch it or setup it.
Capture dโ€™eฬcran 2023-02-05 aฬ€ 17 23 34

Expected behavior (required)
I expected has explained in the tutorial to be able to acces to the menu on the top bar and be able to launch the application.

Additional context (optional)
Thanks you a lot for your soft, the dmg file make it very more user friendly.

Respectfully,
Lucas

How is the log file written to after reboot?

This question might seem a bit silly, but hear me out. So I read the code and when battery maintain is called in turn

nohup battery maintain_synchronous $setting >> $logfile &

is called.

Now this spawns a process which writes to the log file. So far so good. Now what happens when I reboot? Then the LaunchAgent gets executed which calls

/usr/local/bin/battery maintain_synchronous recover

spawning a process, which is NOT piped to the logfile. (Edit: but it seems easy to do, cf. ServerFault Answer) But now the strangest things happen. There is stuff written to the logfile on reboot anyway. I rebooted and got

18:17:57 - Charging to and maintaining at 79% from 91%
00:41:35 - Charge below 79
00:41:35 - Enabling battery charging
sudo: you do not exist in the passwd database
sudo: you do not exist in the passwd database

I am totally confused for three reasons:

  1. How can it write to the logfile when battery.sh is just full of echos and the .plist does not pipe to the logfile
  2. My battery percentage was 85% at reboot, so the log entries are false in that sense. But they are also false in another sense, since when I run battery status it behaves correctly:
โฏ battery status
00:45:13 - Battery at 87% (11:33 remaining), smc charging disabled
00:45:13 - Your battery is currently being maintained at 79%
  1. Where does the sudo: you do not exist in the passwd database come from?

So all in all it does seem to behave correctly in maintaining the charge limit, but the logging is odd (and I cannot fathom how they even happen), the logs are false and there is a sudo error in them.

thanks in advance, I will provide you all further info you need :)

Further info: I did the command-line only installation and have an M1 Pro machine from 2021.

Starting at login

Hello :) I added this code to the beginning of the charging routine in order to prevent the program to run if the Mac is not plugged into power:

power_plugged_in=pmset -g batt | tail -n1 | awk {print $4}
if [[ "$power_plugged_in" == "discharging;" ]]; then
exit 0
fi

If I run it from the terminal, it works perfectly fine (it just stops if not plugged into power). However, I am struggling to add it as a log in item using automator. I tried usr/local/bin/battery charge 80 and I used "as arguments" and added it as a log in item. However, after the startup it keeps charging, even though testing in automator shows the correct result. Any ideas on that? Thanks a lot in advance!!

M2 Macs?

Does this work for all Apple silicon Macs? Can I use it on Macbook Air M2?

"Error installing battery limiter: undefined" when launching UI on Monterey 12.5.1

I installed Battery v1.0.3 using homebrew and then started the UI. After installing the command line component it shows a dialog saying Battery limiter error: undefined and after relaunching the UI, it shows a dialog with the error message Error installing battery limiter: undefined. Not sure what to try to fix this, let me know if you need any other information.

It just doesn't work on my M1

I figured out your script uses 'smc' under the hood. The two commands smc -k CH0B -w 02 and smc -k CH0C -w 02 should do the trick and stop the charging, if I understand battery.sh correctly. Running these two commands however don't seem to do anything :(

Can you explain why you think the exact above two commands should stop the charging? Any idea what can be wrong?

Thanks!

How to use battery visudo

I saw battery visudo in the Usage, but I don't know what is the use of this function, can you tell me what it does?

battery visudo
instructions on how to make which utility exempt from sudo, highly recommended

Issue with GUI and CLI "sudo: smc: command not found"

Hi,

I'm having some issues with both battery GUI and CLI.

On the GUI after installing and downloading the components, I get the following prompt, "Error installing battery limiter: undefined"

With the CLI, whenever I run a command, I get this error.

sudo: smc: command not found sudo: smc: command not found
/us/local/bin/battery: line 110: smc: command not found

"It's not working"

After 'brew install battery' and 'open /Applications/Battery.app', I get these two errors:

  • image
  • image

Whatever I configure, my M1 stays charging :(
image

The command line program gives no error:
image

Any help? :)

Errors after installing latest update 1.0.7

What is the issue? (required)
I just installed the latest release, 1.0.7, which seems to have introduce a few bugs. When I open the app I get a pop up: "Error installing battery limiter: undefined". The only option is to click OK. Then the menubar icon says "updating." Clicking Disable from the dropdown gives a blank error box pop up, then another one that says "Battery limiter error: Cannot read properties of undefined (reading 'split')", then that same one again. Enabling it again doesn't cause any errors, but also doesn't seem to be working, though maybe I just need to wait a while longer.

I'm on a 2021 16" MBP with the M1 Max, running 12.6.1

Expected behavior (required)
A clear and concise description of what you expected to happen.

Screenshots (optional)
If applicable, add screenshots to help explain your problem. On a Mac you can click control+command+shift+4 to create a screenshot that you can paste into this field with command+4.

Error logs
๐Ÿ‘พ Battery CLI logs:

02/08/23-16:22:06 - Battery at 100% (target 80%)
02/08/23-16:23:06 - Battery at 100% (target 80%)
02/08/23-16:24:06 - Battery at 100% (target 80%)
02/08/23-16:25:06 - Battery at 100% (target 80%)
02/08/23-16:26:06 - Battery at 100% (target 80%)
02/08/23-16:27:06 - Battery at 100% (target 80%)
02/08/23-16:28:06 - Battery at 100% (target 80%)
02/08/23-16:29:07 - Battery at 100% (target 80%)
02/08/23-16:30:07 - Battery at 100% (target 80%)
02/08/23-16:31:07 - Battery at 100% (target 80%)
02/08/23-16:32:07 - Battery at 100% (target 80%)
02/08/23-16:33:07 - Battery at 100% (target 80%)
02/08/23-16:34:07 - Battery at 100% (target 80%)
02/08/23-16:35:07 - Battery at 100% (target 80%)
02/08/23-16:36:07 - Battery at 100% (target 80%)
02/08/23-16:37:07 - Battery at 100% (target 80%)
02/08/23-16:38:08 - Battery at 100% (target 80%)
02/08/23-16:39:08 - Battery at 100% (target 80%)
02/08/23-16:40:08 - Battery at 100% (target 80%)
02/08/23-16:41:08 - Battery at 100% (target 80%)
02/08/23-16:42:08 - Battery at 100% (target 80%)
02/08/23-16:43:08 - Battery at 100% (target 80%)
02/08/23-16:44:08 - Battery at 100% (target 80%)
02/08/23-16:45:08 - Battery at 100% (target 80%)
02/08/23-16:46:08 - Battery at 100% (target 80%)
02/08/23-16:47:08 - Battery at 100% (target 80%)
02/08/23-16:48:09 - Battery at 100% (target 80%)
02/08/23-16:49:09 - Battery at 100% (target 80%)
02/08/23-16:50:09 - Battery at 100% (target 80%)
02/08/23-16:51:09 - Battery at 100% (target 80%)
02/08/23-16:52:09 - Battery at 100% (target 80%)
02/08/23-16:53:09 - Battery at 100% (target 80%)
02/08/23-16:54:09 - Battery at 100% (target 80%)
02/08/23-16:55:09 - Battery at 100% (target 80%)
02/08/23-16:56:09 - Battery at 100% (target 80%)
02/08/23-16:57:09 - Battery at 100% (target 80%)
02/08/23-16:58:10 - Battery at 100% (target 80%)
02/08/23-16:59:10 - Battery at 100% (target 80%)
02/08/23-17:00:10 - Battery at 100% (target 80%)
02/08/23-17:01:10 - Battery at 100% (target 80%)
02/08/23-17:02:10 - Battery at 100% (target 80%)
02/08/23-17:03:10 - Battery at 100% (target 80%)
02/08/23-17:04:10 - Battery at 100% (target 80%)
02/08/23-17:05:10 - Battery at 100% (target 80%)
02/08/23-17:06:10 - Battery at 100% (target 80%)
02/08/23-17:07:10 - Battery at 100% (target 80%)
02/08/23-17:08:10 - Battery at 100% (target 80%)
02/08/23-17:09:11 - Battery at 100% (target 80%)
02/08/23-17:10:11 - Battery at 100% (target 80%)
02/08/23-17:11:11 - Battery at 100% (target 80%)
02/08/23-17:12:11 - Battery at 100% (target 80%)
02/08/23-17:13:11 - Battery at 100% (target 80%)
02/08/23-17:14:11 - Battery at 100% (target 80%)
02/08/23-17:15:11 - Battery at 100% (target 80%)
02/08/23-17:16:12 - Battery at 100% (target 80%)
02/08/23-17:17:12 - Battery at 100% (target 80%)
02/08/23-17:18:12 - Battery at 100% (target 80%)
02/08/23-17:19:12 - Battery at 100% (target 80%)
02/08/23-17:20:12 - Battery at 100% (target 80%)
02/08/23-17:21:12 - Battery at 100% (target 80%)
02/08/23-17:22:12 - Battery at 100% (target 80%)
02/08/23-17:23:12 - Battery at 100% (target 80%)
02/08/23-17:24:13 - Battery at 100% (target 80%)
02/08/23-17:25:13 - Battery at 100% (target 80%)
02/08/23-17:26:13 - Battery at 100% (target 80%)
02/08/23-17:27:13 - Battery at 100% (target 80%)
02/08/23-17:27:17 - Triggering discharge to 80 before enabling charging limiter
02/08/23-17:27:17 - Discharging to 80% from 100%
02/08/23-17:27:17 - ๐Ÿ”ฝ๐Ÿชซ Enabling battery discharging
02/08/23-17:27:17 - Battery at 100% (target 80%)
02/08/23-17:28:13 - Battery at 100% (target 80%)
02/08/23-17:28:17 - Battery at 100% (target 80%)
02/08/23-17:28:53 - Triggering discharge to 80 before enabling charging limiter
02/08/23-17:28:53 - Discharging to 80% from 100%
02/08/23-17:28:53 - ๐Ÿ”ฝ๐Ÿชซ Enabling battery discharging
02/08/23-17:28:53 - Battery at 100% (target 80%)
02/08/23-17:29:13 - Battery at 100% (target 80%)
02/08/23-17:29:17 - Battery at 100% (target 80%)
02/08/23-17:29:53 - Battery at 100% (target 80%)
02/08/23-17:30:13 - Battery at 100% (target 80%)
02/08/23-17:30:17 - Battery at 100% (target 80%)
02/08/23-17:30:53 - Battery at 100% (target 80%)
02/08/23-17:31:13 - Battery at 100% (target 80%)
02/08/23-17:31:18 - Battery at 100% (target 80%)
02/08/23-17:31:32 - Triggering discharge to 80 before enabling charging limiter
02/08/23-17:31:32 - Discharging to 80% from 100%
02/08/23-17:31:32 - ๐Ÿ”ฝ๐Ÿชซ Enabling battery discharging
02/08/23-17:31:32 - Battery at 100% (target 80%)
02/08/23-17:31:54 - Battery at 100% (target 80%)
02/08/23-17:32:13 - Battery at 100% (target 80%)
02/08/23-17:32:18 - Battery at 100% (target 80%)
02/08/23-17:32:32 - Battery at 100% (target 80%)
02/08/23-17:32:46 - Triggering discharge to 80 before enabling charging limiter
02/08/23-17:32:46 - Discharging to 80% from 100%
02/08/23-17:32:46 - ๐Ÿ”ฝ๐Ÿชซ Enabling battery discharging
02/08/23-17:32:46 - Battery at 100% (target 80%)
02/08/23-17:32:54 - Battery at 100% (target 80%)
02/08/23-17:33:13 - Battery at 100% (target 80%)
02/08/23-17:33:18 - Battery at 100% (target 80%)
02/08/23-17:33:32 - Battery at 100% (target 80%)

๐Ÿ–ฅ๏ธ Battery GUI logs:

Refreshing tray icon...
Battery status:
{"percentage":"100","remaining":"0:00","charging":true,"discharging":false,"battery_state":"100% (0:00 remaining)","daemon_state":"smc charging enabled"}
Executing PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/usr/sbin:/opt/homebrew:/usr/bin/ battery status
Limiter status message:
02/08/23-17:32:45 - Battery at 100% (0:00 remaining), smc charging enabled

Refreshing interface update timer
Generate app menu percentage: 100
Executing PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/usr/sbin:/opt/homebrew:/usr/bin/ battery status_csv
Display percentage 95 based on 100
Resetting interface timer speed
Get inactive logo for 100
Setting interface refresh speed to 10 minutes
Battery status:
{"percentage":"100","remaining":"0:00","charging":true,"discharging":false,"battery_state":"100% (0:00 remaining)","daemon_state":"smc charging enabled"}
Refreshing tray icon...
Executing PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/usr/sbin:/opt/homebrew:/usr/bin/ battery status_csv
Executing PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/usr/sbin:/opt/homebrew:/usr/bin/ battery status
Battery status:
{"percentage":"100","remaining":"0:00","charging":true,"discharging":false,"battery_state":"100% (0:00 remaining)","daemon_state":"smc charging enabled"}
Limiter status message:
02/08/23-17:32:45 - Battery at 100% (0:00 remaining), smc charging enabled

Refreshing interface update timer
Executing PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/usr/sbin:/opt/homebrew:/usr/bin/ battery status_csv
Resetting interface timer speed
Get inactive logo for 100
Generate app menu percentage: 100
Display percentage 95 based on 100
Battery status:
{"percentage":"100","remaining":"0:00","charging":true,"discharging":false,"battery_state":"100% (0:00 remaining)","daemon_state":"smc charging enabled"}
Setting interface refresh speed to 10 minutes
Enable limiter
Display percentage 80 based on 80
Get active logo for 80
Refresh logo for percentage 80, force active
Executing PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/usr/sbin:/opt/homebrew:/usr/bin/ battery status_csv
Executing PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/usr/sbin:/opt/homebrew:/usr/bin/ battery maintain 80
Battery status:
{"percentage":"100","remaining":"0:00","charging":true,"discharging":false,"battery_state":"100% (0:00 remaining)","daemon_state":"smc charging enabled"}
Executing PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/usr/sbin:/opt/homebrew:/usr/bin/ battery status_csv
Refreshing tray icon...
Executing PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/usr/sbin:/opt/homebrew:/usr/bin/ battery status
Battery status:
{"percentage":"100","remaining":"0:00","charging":true,"discharging":false,"maintain_percentage":"80","battery_state":"100% (0:00 remaining)","daemon_state":"smc charging enabled"}
Generate app menu percentage: 100
Get active logo for 100
Resetting interface timer speed
Limiter status message:
02/08/23-17:32:46 - Battery at 100% (0:00 remaining), smc charging enabled
02/08/23-17:32:46 - Your battery is currently being maintained at 80%

Executing PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/usr/sbin:/opt/homebrew:/usr/bin/ battery status_csv
Refreshing interface update timer
Display percentage 95 based on 100
Battery status:
{"percentage":"100","remaining":"0:00","charging":true,"discharging":false,"maintain_percentage":"80","battery_state":"100% (0:00 remaining)","daemon_state":"smc charging enabled"}
Setting interface refresh speed to 10 minutes
Executing PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/usr/sbin:/opt/homebrew:/usr/bin/ battery status_csv
Refreshing tray icon...
Battery status:
{"percentage":"100","remaining":"0:00","charging":true,"discharging":false,"maintain_percentage":"80","battery_state":"100% (0:00 remaining)","daemon_state":"smc charging enabled"}
Executing PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/usr/sbin:/opt/homebrew:/usr/bin/ battery status
Generate app menu percentage: 100
Resetting interface timer speed
Limiter status message:
02/08/23-17:32:46 - Battery at 100% (0:00 remaining), smc charging enabled
02/08/23-17:32:46 - Your battery is currently being maintained at 80%

Executing PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/usr/sbin:/opt/homebrew:/usr/bin/ battery status_csv
Display percentage 95 based on 100
Get active logo for 100
Refreshing interface update timer
Setting interface refresh speed to 10 minutes
Battery status:
{"percentage":"100","remaining":"0:00","charging":true,"discharging":false,"maintain_percentage":"80","battery_state":"100% (0:00 remaining)","daemon_state":"smc charging enabled"}
enable_battery_limiter exec complete
Interface enabled limiter, percentage remaining: 100
Refresh logo for percentage 100, force active
Get active logo for 100
Display percentage 95 based on 100
Refreshing tray icon...
Executing PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/usr/sbin:/opt/homebrew:/usr/bin/ battery status_csv
Executing PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/usr/sbin:/opt/homebrew:/usr/bin/ battery status
Battery status:
{"percentage":"100","remaining":"0:00","charging":true,"discharging":false,"maintain_percentage":"80","battery_state":"100% (0:00 remaining)","daemon_state":"smc charging enabled"}
Generate app menu percentage: 100
Get active logo for 100
Refreshing interface update timer
Executing PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/usr/sbin:/opt/homebrew:/usr/bin/ battery status_csv
Display percentage 95 based on 100
Resetting interface timer speed
Limiter status message:
02/08/23-17:32:48 - Battery at 100% (0:00 remaining), smc charging enabled
02/08/23-17:32:48 - Your battery is currently being maintained at 80%

Setting interface refresh speed to 10 minutes
Battery status:
{"percentage":"100","remaining":"0:00","charging":true,"discharging":false,"maintain_percentage":"80","battery_state":"100% (0:00 remaining)","daemon_state":"smc charging enabled"}

๐Ÿ“ Config folder details:

total 152
drwxr-xr-x 6 alexander staff 192B Feb 8 17:32 .
drwxr-x---+ 39 alexander staff 1.2K Feb 8 17:33 ..
-rwxr-xr-x 1 alexander staff 25K Feb 8 17:33 battery.log
-rw-r--r-- 1 alexander staff 6B Feb 8 17:32 battery.pid
-rw-r--r-- 1 alexander staff 37K Feb 8 17:32 gui.log
-rw-r--r-- 1 alexander staff 3B Feb 8 17:32 maintain.percentage

โš™๏ธ Battery data:

02/08/23-17:33:38 - Battery at 100% (0:00 remaining), smc charging enabled
02/08/23-17:33:38 - Your battery is currently being maintained at 80%
Battery CLI utility v1.0.1

Additional context (optional)
Add any other context about the problem here.

cli works fine, but app cannot initialize

I have uninstalled battery cli tool and installed app instead:
image

after installing cli tool back again and restarting app:
image

if I am trying to maintain 80 level from app:
image

Feature request: low threshold to resume charging

It seems like the "maintain" command will cycle the charger in a pretty tight window. I wonder if it could easily enough be made configurable about when to turn the charging on to create a wider cycle. Is that even something that should happen?

if [[ "$battery_percentage" -gt "$setting" && "$is_charging" == "enabled" ]]; then

			log "Charge above $setting"
			disable_charging

		elif [[ "$battery_percentage" -lt "$setting" && "$is_charging" == "disabled" ]]; then

			log "Charge below $setting"
			enable_charging

		fi

I'm thinking something like if -gt $setting disable_charging, but elif -lt $low_setting enable_charging

Is this something you'd consider? I was told it's good to let the battery drain down by an Apple tech, so occasionally I'd like to set it to go down to 5% then recharge to 80%.

Maybe make the second argument optional, like
battery maintain 80 does the original behavior, but battery maintain 80 10 does the new?

MBP charging enable after reboot eventhough high battery level

It worked well after installed. But while reboot and run "battery status"

10:16:40 - Battery at 98% (0:23 remaining), smc charging enabled
10:16:40 - Your battery is currently being maintained at 80%

MBP start charging after reboot eventhough 98% battery level.
Thanks.

Hide menubar icon

As a user, I want to be able to hide/unhide the menubar icon from the preferences. Now app icon just sits there and takes space. I want to set it and forget it. And I should be able to restore the settings window when launching the app.

The battery level setting is preserved, but it still charges until I run battery again

What is the issue? (required)
If I reboot (or some other reset situation?) the battery status shows the level of the battery to be maintained has been preserved, but the charging is enabled even if the battery level is much higher until I run battery maintain.

What exactly did you do to produce the issue? (required)
Steps to reproduce the behavior:

  1. sudo battery maintain 40
  2. reboot
  3. sudo battery status
  4. See "Your battery is currently being maintained at 40%" which is correct (battery level preserved) but also nonsensically see "smc charging enabled"
  5. sudo battery maintain 40
  6. sudo battery status
  7. See "Your battery is currently being maintained at 40%" but also "smc charging disabled" which is obviously what I want

Expected behavior (required)
If the battery level is above what the maintenance level is, charging should be disabled.

Screenshots (optional)
Screen Shot 2023-01-21 at 10 51 37 PM

Looking for a discharge functionality.

Hi, great work here!

I have just one little additional problem. New monitor with type-C port have typically charging capability, which means you can use an external monitor while charging in a single type-C cable. But if mac's battery exceeds the limit, I can't have it reduce to a lower percentage. Aldente has a Discharge functionality which is exactly what I'm describing (but I'm a CLI guy sadly).

Can you please work on this?

I'm willing to code and just don't know how to achieve this.

How to reset?

when I uninstall, how to reset custom settings, recovery system setting?

Top-up feature

Hi, I wonder if it would be possible to have an ability to top-up (charge to 100%), but still keep the original maintenance setting. Currently, if I set battery maintain 80 and before business trip I do battery charge 100, it removes the 80% maintenance level. It'd be ideal if that maintenance level is kept, so that I don't forget about it.

Mac keeps asking for sudo password after uninstalling the utility

What is the issue? (required)
My mac keeps asking for sudo password even after I uninstalled the app. I know it's asking the password because of Battery since I've opened Activity Monitor and I checked which files were open in the bash process that shows the sudo password prompt. It shows "/usr/local/bin/battery" but the file has been removed.

What exactly did you do to produce the issue? (required)
I uninstalled it through the command "battery uninstall". It didn't work so I tried reinstalling it and uninstalling it through brew. Didn't work either.

"Charging off" mode does not work

Hello, I installed Battery from the Brew repository, I followed all the instructions successfully, but when I try to turn off my laptop's battery charge autonomously it turns back on, and when I let it discharge almost completely when connecting it reloads to 100%. I'm using a MB Pro M1 with Big Sur 11.7.3

SCR-20230126-quf

Thoughts on being able to change the battery ceiling to a non-80% value?

Some people like to use their MacBooks as desktops, and for those types of people an option to further decrease the limit to 50-60% might not be a bad idea. Others frequently keep it plugged in but also travel a lot with it, so for those types a 70% limit may make more sense. Thoughts on implementing the aforementioned functionality? (As it stands, battery can only set the limit to 80%)

Error when enabling/disabling charging

Glad to see such a simple and useful tool.

There's am error log always shows up on my mac (MacBook Pro, 2019, Intel) when toggling the switch for charging:

$ battery charge 85 
22:36:41 - Charging to 80% from 77%
22:36:41 - Enabling battery charging
Error: SMCWriteKey() = e00002bc
22:36:41 - Battery at 77%
22:37:41 - Battery at 78%
22:38:41 - Battery at 79%
22:39:41 - Battery at 80%
22:40:41 - Disabling battery charging
Error: SMCWriteKey() = e00002bc
22:40:41 - Charging completed at 80%

But it seems not a fatal one, after all it actually works fine to me.
Should I ignore this?

Installation fails with error "command not found"

Hi

first of all thanks for creating and sharing this.

I suspect my error might be related to the other open issue #16", but not sure.

I am using an Apple M1 Pro with Ventura 13.0.1.

I got these errors on installation via the DMG file, and some errors later when I tried the installation via command line:

- Error installing battery limiter: undefined
- Battery limiter error: Command failed: PATH=$PATH:/usr/local/bin/battery battery status
/bin/bash: battery: command not found

Then when I tried to click "Enable 80% battery limit:

- Command failed: PATH=$PATH:/usr/local/bin/battery battery maintain 80
/bin/bash: battery: command not found

Without uninstalling, I tried to install via the command line:

secret@machine ~ % curl -s https://raw.githubusercontent.com/actuallymentor/battery/main/setup.sh | bash
####################################################################
# ๐Ÿ‘‹ Welcome, this is the setup script for the battery CLI tool.
# Note: this script will ask for your password once or multiple times.
####################################################################

Password:
๐Ÿ”‹ Starting battery installation

[ 1/9 ] Superuser permissions acquired.
[ 2/9 ] Cloning fan control version of smc
bash: line 30: cd: /Users/secret/.battery-tmp/smc/smc-command: No such file or directory
[ 3/9 ] Building smc from source
[ 4/9 ] Move smc to executable folder
mv: rename /Users/secret/.battery-tmp/smc/smc-command/smc to /usr/local/bin/smc: No such file or directory
chmod: /usr/local/bin/smc: No such file or directory
[ 5/9 ] Cloning battery repository
[ 6/9 ] Writing script to /usr/local/bin/battery for user secret
cp: /Users/secret/.battery-tmp/battery/battery.sh: No such file or directory
chown: /usr/local/bin/battery: No such file or directory
chmod: /usr/local/bin/battery: No such file or directory
chmod: /usr/local/bin/battery: No such file or directory
chown: /usr/local/bin/battery: No such file or directory
bash: /Users/secret/.battery-tmp/battery/battery.sh: No such file or directory
[ 7/9 ] Set up visudo declarations
[ 8/9 ] Removing temp folder /Users/secret/.battery-tmp
[ 9/9 ] Removed temporary build files

๐ŸŽ‰ Battery tool installed. Type "battery help" for instructions.

secret@machine ~ % battery maintain 80
zsh: command not found: battery


I manually created these two folder with "sudo mkdir", but still got errors in the installation:

/usr/local/bin/smc
/usr/local/bin/battery:

secret@machine ~ % curl -s https://raw.githubusercontent.com/actuallymentor/battery/main/setup.sh | bash

####################################################################
# ๐Ÿ‘‹ Welcome, this is the setup script for the battery CLI tool.
# Note: this script will ask for your password once or multiple times.
####################################################################

๐Ÿ”‹ Starting battery installation

[ 1/9 ] Superuser permissions acquired.
[ 2/9 ] Cloning fan control version of smc
bash: line 30: cd: /Users/secret/.battery-tmp/smc/smc-command: No such file or directory
[ 3/9 ] Building smc from source
[ 4/9 ] Move smc to executable folder
mv: /Users/secret/.battery-tmp/smc/smc-command/smc: No such file or directory
[ 5/9 ] Cloning battery repository
[ 6/9 ] Writing script to /usr/local/bin/battery for user secret
cp: /Users/secret/.battery-tmp/battery/battery.sh: No such file or directory
bash: /Users/secret/.battery-tmp/battery/battery.sh: No such file or directory
[ 7/9 ] Set up visudo declarations
[ 8/9 ] Removing temp folder /Users/secret/.battery-tmp
[ 9/9 ] Removed temporary build files

๐ŸŽ‰ Battery tool installed. Type "battery help" for instructions.

Any help is appreciated.

Thanks,
Ruben

[BUG] Low resolution and missing menu bar icon

What is the issue? (required)
Low resolution menu bar icon being displayed.

What exactly did you do to produce the issue? (required)
Run battery 1.0.7

Expected behavior (required)
High resolution menu bar icon being displayed.

Screenshots (optional)
battery

Additional context (optional)
M1 MBP 13" macOS 13.2 (22D49) battery 1.0.7

Edit

Seems to only affect the 100% indicator.

Edit 2

Icon missing completely under 20%

Edit 3

Only affects the 100% indicator when enabled and in dark mode.

Error handling when running without sudo

sudo is required to run most battery commands. Currently the output without sudo is:

12:14:12 - Starting battery maintenance at 75%
/usr/local/bin/battery: line 305: /Users/tok/.battery/battery.pid: Permission denied
/usr/local/bin/battery: line 307: /Users/tok/.battery/maintain.percentage: Permission denied
12:14:12 - Maintaining battery at 75%

It is ambiguous wether the command succeeded. I'm assuming superuser permissions are required, otherwise less permissions would be less secure. Code to check for the UID 0 bit should be added, and a help message should be printed.
I may be happy to open a PR when I have time.

[Feature Request] Improve Menu GUI information

Is your feature request related to a problem? Please describe.
The GUI menu only shows a standard set of options:

  • Enable 80% Limit;
  • Disable 80% Limit;

image

Describe the solution you'd like
Let the Top bar menu show the setup done in the CLI.
For example the informations I get with the battery statuscommand

A nice set of information to be displayed in that menu is:

  • Mantain percentage set;
  • Charging Enabled/Disabled;
  • Battery charging YES or NOT;

Describe alternatives you've considered
The alternative is to get those info inside the terminal. Not properly comfortable.

Not an issue just to ask if it is needed the fan control

I noticed that in addition to the battery script it is necessary to compile the smc tool (actually it mentioned on the cli installation section)
In Apple Silicon MacBooks only the pro versions have a fan, so what about the MBA?

Not working for M2 Max MacBook Pro 16 inch

What is the issue? (required)
The app fails to launch on my M2 Max MacBook Pro with the following error:

Error installing battery limiter: Command failed: ./applet/bin/sh: ./applet: Bad CPU type in executable

What exactly did you do to produce the issue? (required)
Steps to reproduce the behavior:

  1. Install battery via Homebrew: brew install battery. Installation succeeds.
  2. Open /Applications/Battery.app.
  3. Observe "Bad CPU type in executable" error.

Expected behavior (required)
The application should launch without error.

Screenshots (optional)
image

Error logs (optional)
The log for ~/.battery/battery.log does not exist. Here is the log for ~/.battery/gui.log:

Starting tray app
Tray app boot complete
Triggering boot-time auto-update
Is installed? 
false
Installing battery for tylermilner...
Update/install error: 
Error: Command failed: ./applet
/bin/sh: ./applet: Bad CPU type in executable

Update process complete
Error getting battery status: 
Error: Command failed: PATH=$PATH:/usr/local/bin/battery battery status
/bin/bash: battery: command not found

Additional context (optional)
Add any other context about the problem here.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.