actuallymentor / battery

CLI for managing the battery charging status for M1 Macs
MIT License
3.2k stars 140 forks source link

Sailing Mode like in AlDente #150

Open Kriepke opened 1 year ago

Kriepke commented 1 year ago

Is it possible to set, for example, to stop charging at 80 percent and turn on only below 45 percent?

IIodyne commented 11 months ago

I don't know exactly how AlDente does it but I would reccomend making a shortcut to do it for you. Here's one I threw together: https://www.icloud.com/shortcuts/49872c533bae4316a106851fd609d2fc

I chose a range between 40% and 60%. At the end you set how often it should run the script (currently set for 5x60 seconds = 5 minutes). At the beginning I have it set to repeat 12x24x30 times = 12 5-minutes / hours x 24 hours/day x 30days. Obviously you can change the times around.

Let me know if you come up with a better implementation; good luck!

xoralex commented 11 months ago

If I understand correctly for the sailing mode it is better to use "battery charging off" and "battery charging on" instead of "battery discharge 40" and "battery charge 60".

Kriepke commented 11 months ago

Thank You, I will try.

I don't know exactly how AlDente does it but I would reccomend making a shortcut to do it for you. Here's one I threw together: https://www.icloud.com/shortcuts/49872c533bae4316a106851fd609d2fc

I chose a range between 40% and 60%. At the end you set how often it should run the script (currently set for 5x60 seconds = 5 minutes). At the beginning I have it set to repeat 12x24x30 times = 12 5-minutes / hours x 24 hours/day x 30days. Obviously you can change the times around.

Let me know if you come up with a better implementation; good luck!

actuallymentor commented 11 months ago

This is a scripting use and beyond the intended scope of this app. That said, I am open to a PR if you really care about the feature.

jakjakob commented 11 months ago

I don't know exactly how AlDente does it but I would reccomend making a shortcut to do it for you. Here's one I threw together: https://www.icloud.com/shortcuts/49872c533bae4316a106851fd609d2fc

I chose a range between 40% and 60%. At the end you set how often it should run the script (currently set for 5x60 seconds = 5 minutes). At the beginning I have it set to repeat 12x24x30 times = 12 5-minutes / hours x 24 hours/day x 30days. Obviously you can change the times around.

Let me know if you come up with a better implementation; good luck!

Don't know if it works very good, on iPhone shortcuts eventually stop (even while running) AND additionally, running an itself repeating shortcut isn't very efficient and is power and CPU intense. (Also just having the shortcuts app open isn't very efficient...) (Tried it with a simple loop like you did with only a one sec. wait action on a M2 MacBook Air)

Kriepke commented 11 months ago

While the charging of the MacBook is paused, i.e. the charge level is exactly at the set charge limit, the power supply is primarily used as a power source. However, small discharges of the battery still occur when a lot of power is needed for a short time. Then the battery of the MacBook steps in as a buffer. If the charge level drops below the charge limit due to these short discharges, Battery-app recharges the battery to the desired charge limit. A second effect on why the battery loses charge is time. If a battery is left for weeks without charging or discharging, it will also lose charge over time. Also, in this case, Battery app would detect that the charge level has dropped below the charge limit and would recharge the battery to the selected charge limit. Furthermore, if you unplug your MacBook only for a short time and use only a couple of percent, your Battery-app would charge MacBook again. Since it is theoretically healthier for the battery to be charged 10% once instead of 1% 10 times.

landabaso commented 10 months ago

Similarly, for those that prefer using cron, add this file in $HOME/.local/bin/batterySailingMode.bash:

#!/bin/bash

# Fetch the battery percentage
BATTERY_PERCENTAGE=$(/usr/local/bin/battery status | awk -F' ' '{print $5}' | tr -d '%')
# Fetch charging status
CHARGING_STATUS=$(/usr/local/bin/battery status | grep -o "smc charging .*" | awk '{print $3}')

# Compare the battery level and adjust charging based on the current state
if (( BATTERY_PERCENTAGE > 60 )) && [[ $CHARGING_STATUS == "enabled" ]]; then
    /usr/local/bin/battery charging off
elif (( BATTERY_PERCENTAGE < 40 )) && [[ $CHARGING_STATUS == "disabled" ]]; then
    /usr/local/bin/battery charging on
fi

Then chmod +x $HOME/.local/bin/batterySailingMode.bash Then crontab -e, which will open your favourite editor and add this line to run the script every 5 minutes:

*/5 * * * * /Users/<YOUR USERNAME>/.local/bin/batterySailingMode.bash

yourivdlans commented 10 months ago

Thanks @landabaso! This is perfect.

The BATTERY_PERCENTAGE also contained a newline and "is" string for me, changing this line to the following worked for me:

BATTERY_PERCENTAGE=$(/usr/local/bin/battery status | awk -F' ' '{print $5; exit}' | tr -d '%')
dJani97 commented 7 months ago

This would be great, the goal of this program is to prolong the longevity of the battery, so in theory this should help in achieving that goal.

Using cron jobs and shortcuts is inconvenient because when I actually want to charge my Macbook to a 100%, these automations will keep setting the charge limit every few minutes.

Kaiman77 commented 4 months ago

I absolutely agree - at the moment it can happen, that you use the macbook withour power supply for 10 minutes, bringing battery state to 78% and it will charge it immediately back to 80....not necessarily helpful for the batter lifetime.

Usually what you use for such situations is a hysteresis with two limits.