Last active
September 3, 2023 03:01
-
-
Save marksowell/be3690ce80d55cd0b8a73968e5cdde04 to your computer and use it in GitHub Desktop.
Hammerspoon macOS Battery Percentage <=10% Alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local alertShown = false | |
function checkBatteryStatus() | |
local currentCapacitymAh = hs.battery.capacity() | |
local maxCapacitymAh = hs.battery.maxCapacity() | |
local currentCapacityPercentage = (currentCapacitymAh / maxCapacitymAh) * 100 | |
local isCharging = hs.battery.isCharging() | |
if currentCapacityPercentage <= 5 and not alertShown then | |
hs.alert.show("Battery capacity <= 5%!", 500.0) -- 500 seconds duration for the alert | |
alertShown = true | |
elseif currentCapacityPercentage > 5 or isCharging then | |
hs.alert.closeAll() -- Close the alert if capacity is more than 10% or if power is connected | |
alertShown = false | |
end | |
end | |
batteryWatcher = hs.battery.watcher.new(checkBatteryStatus) | |
batteryWatcher:start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment