hs.power
ModuleMonitor and control system power: prevent sleep, read battery state, respond to power events, and lock or sleep the machine.
Preventing sleep
// Prevent the display from sleeping while a task runs
hs.power.preventSleep("display")
// ... do work ...
hs.power.allowSleep("display")
Watching for system events
hs.power.on("screensDidLock", () => console.log("Screen locked!"))
Reading battery state
const info = hs.power.batteryInfo()
if (info) {
console.log(`Battery: ${info.percentage}%, ${info.timeRemaining} minutes remaining`)
}
Properties
hs.power.percentage
The current battery charge percentage (0–100), or -1 if no battery is present.
hs.power.isCharging
Whether the battery is currently charging.
Returns false when no battery is present.
hs.power.powerSource
The current power source.
Returns "ac" when plugged in, "battery" when on battery power, "ups" when
powered by a UPS, or "unknown" if the source cannot be determined.
hs.power.thermalState
The current thermal state of the system.
Returns one of: "nominal", "fair", "serious", "critical".
Methods
hs.power.preventSleep(type) -> boolean
Prevents the specified type of system sleep.
Creates an IOKit power assertion that stops macOS from allowing the specified
type of sleep. Call allowSleep with the same type to release the assertion.
hs.power.preventSleep(type) -> boolean
| Name | Type | Description |
|---|---|---|
| type | string | The sleep type to prevent. One of: "display" (prevent display |
true if the assertion was created successfully. hs.power.preventSleep("display")
hs.power.allowSleep(type) -> boolean
Releases a previously created sleep prevention assertion.
hs.power.allowSleep(type) -> boolean
| Name | Type | Description |
|---|---|---|
| type | string | The sleep type to allow again. One of: "display", "systemIdle", "system". |
true if an assertion existed and was released, false if none was active. hs.power.allowSleep("display")
hs.power.isSleepPrevented(type) -> boolean
Returns whether Hammerspoon is currently preventing the specified type of sleep.
hs.power.isSleepPrevented(type) -> boolean
| Name | Type | Description |
|---|---|---|
| type | string | The sleep type to check. One of: "display", "systemIdle", "system". |
true if this sleep type is currently being prevented. if (hs.power.isSleepPrevented("display")) console.log("display sleep is prevented")
hs.power.declareActivity() -> None
Simulates user activity, briefly resetting the display idle timer.
Equivalent to moving the mouse — does not create a persistent assertion.
hs.power.declareActivity() -> None
hs.power.declareActivity()
hs.power.currentAssertions() -> [[String: Any]]
Returns the active power management assertions from all processes on the system.
hs.power.currentAssertions() -> [[String: Any]]
pid (number), name (string), and type (string) properties. hs.power.currentAssertions().forEach(a => console.log(a.pid + " " + a.name))
hs.power.systemSleep() -> None
Puts the system to sleep immediately.
Requires the Automation permission for System Events.
hs.power.systemSleep() -> None
hs.power.systemSleep()
hs.power.lockScreen() -> None
Locks the screen immediately.
hs.power.lockScreen() -> None
hs.power.lockScreen()
hs.power.startScreensaver() -> None
Starts the screensaver immediately.
hs.power.startScreensaver() -> None
hs.power.startScreensaver()
hs.power.batteryInfo() -> [String: Any]
Returns a snapshot of all available battery information, or null if no battery is present.
The returned object contains (values may be null if unavailable on the current hardware):
percentage— charge level 0–100isCharging— whether chargingisCharged— whether fully chargedsource—"ac","battery","ups", or"unknown"health— health string, e.g."Good"healthCondition— health condition detail, ornullcycleCount— charge cycle countcapacity— current capacity in mAhmaxCapacity— current maximum capacity in mAhdesignCapacity— original design capacity in mAhvoltage— voltage in millivoltsamperage— current in milliamps (negative when discharging)watts— power in watts (negative when discharging)temperature— temperature in °CtimeRemaining— estimated minutes remaining, ornullif calculatingtimeToFullCharge— estimated minutes to full charge, ornullif not applicableserial— battery serial number
hs.power.batteryInfo() -> [String: Any]
null if no battery is present. const info = hs.power.batteryInfo()
if (info) console.log(`${info.percentage}% — ${info.timeRemaining}m remaining`)
hs.power.on(event, listener) -> None
Register a listener for system power/session events, or battery state changes.
hs.power.on(event, listener) -> None
| Name | Type | Description |
|---|---|---|
| event | any | The event to listen for. "change" fires whenever battery state changes; the rest are system power/session events. |
| listener | any | Called with no arguments when the event occurs; call batteryInfo() inside a "change" listener to inspect the new battery state |
try {
hs.power.on('systemWillSleep', () => console.log("Going to sleep"))
hs.power.on('change', () => console.log("Battery now: " + hs.power.batteryInfo().percentage + "%"))
} catch (err) {
console.error(err.message)
}
hs.power.off(event, listener) -> None
Remove a previously registered hs.power listener.
hs.power.off(event, listener) -> None
| Name | Type | Description |
|---|---|---|
| event | any | The event the listener was registered for |
| listener | any | The function originally passed to on |
const onSleep = () => console.log("sleeping")
hs.power.on('systemWillSleep', onSleep)
// later…
hs.power.off('systemWillSleep', onSleep)
hs.power.once(event, listener) -> None
Register a listener that fires at most once for the given hs.power event.
hs.power.once(event, listener) -> None
| Name | Type | Description |
|---|---|---|
| event | any | The event to listen for |
| listener | any | Called once, then automatically removed |
try {
hs.power.once('systemDidWake', () => console.log("Welcome back"))
} catch (err) {
console.error(err.message)
}