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`)
}
Event Types
The events this module's watchers emit, and when. In TypeScript, each type is a union of its event names that you can use as a type of your own:
HSPowerEvent
An event name for hs.power.on(), hs.power.off() and hs.power.once()
| Event | Description |
|---|---|
"screensDidSleep" |
The displays went to sleep |
"screensDidWake" |
The displays woke up |
"screensDidLock" |
The screen was locked |
"screensDidUnlock" |
The screen was unlocked |
"screensaverDidStart" |
The screensaver started |
"screensaverDidStop" |
The screensaver stopped |
"screensaverWillStop" |
The screensaver is about to stop |
"systemWillSleep" |
The Mac is about to sleep |
"systemDidWake" |
The Mac woke from sleep |
"systemWillPowerOff" |
The user is logging out, or the Mac is about to shut down or restart |
"sessionDidBecomeActive" |
This user's session became the active one again, after fast user switching |
"sessionDidResignActive" |
Fast user switching moved to another user's session |
"change" |
The battery state changed. Read hs.power.batteryInfo() for the new state |
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 idle sleep), "systemIdle" (prevent system idle sleep), "system" (prevent all system sleep, including from power button or lid close). |
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() -> object[]
Returns the active power management assertions from all processes on the system.
hs.power.currentAssertions() -> object[]
| Field | Type | Description |
|---|---|---|
| pid | number | The process that holds the assertion |
| name optional | string | The assertion's name, if it has one |
| type optional | string | The kind of assertion, e.g. "PreventUserIdleDisplaySleep" |
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() -> object | null
Returns a snapshot of all available battery information, or null if no battery is present.
hs.power.batteryInfo() -> object | null
null if no battery is present. Values are null if unavailable on the current hardware| Field | Type | Description |
|---|---|---|
| percentage | number | Charge level 0–100, or -1 if unknown |
| isCharging | boolean | Whether it's charging |
| isCharged | boolean | Whether it's fully charged |
| source | "ac" | "battery" | "ups" | "unknown" | What the Mac is running on |
| health | string | Health string, e.g. "Good", or "Unknown" |
| healthCondition | string | null | Health condition detail |
| cycleCount | number | null | Charge cycle count |
| capacity | number | null | Current capacity in mAh |
| maxCapacity | number | null | Current maximum capacity in mAh |
| designCapacity | number | null | Original design capacity in mAh |
| voltage | number | null | Voltage in millivolts |
| amperage | number | null | Current in milliamps (negative when discharging) |
| watts | number | null | Power in watts (negative when discharging) |
| temperature | number | null | Temperature in °C |
| timeRemaining | number | null | Estimated minutes remaining, or null if calculating |
| timeToFullCharge | number | null | Estimated minutes to full charge, or null if not applicable |
| serial | string | null | Battery serial number |
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 | HSPowerEvent | The event to listen for. "change" fires whenever battery state changes; the rest are system power/session events. |
| listener | () => void | Called with no arguments when the event occurs; call batteryInfo() inside a "change" listener to inspect the new battery state |
| Event | Description |
|---|---|
"screensDidSleep" |
The displays went to sleep |
"screensDidWake" |
The displays woke up |
"screensDidLock" |
The screen was locked |
"screensDidUnlock" |
The screen was unlocked |
"screensaverDidStart" |
The screensaver started |
"screensaverDidStop" |
The screensaver stopped |
"screensaverWillStop" |
The screensaver is about to stop |
"systemWillSleep" |
The Mac is about to sleep |
"systemDidWake" |
The Mac woke from sleep |
"systemWillPowerOff" |
The user is logging out, or the Mac is about to shut down or restart |
"sessionDidBecomeActive" |
This user's session became the active one again, after fast user switching |
"sessionDidResignActive" |
Fast user switching moved to another user's session |
"change" |
The battery state changed. Read hs.power.batteryInfo() for the new 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 | HSPowerEvent | The event the listener was registered for |
| listener | () => void | The function originally passed to on |
| Event | Description |
|---|---|
"screensDidSleep" |
The displays went to sleep |
"screensDidWake" |
The displays woke up |
"screensDidLock" |
The screen was locked |
"screensDidUnlock" |
The screen was unlocked |
"screensaverDidStart" |
The screensaver started |
"screensaverDidStop" |
The screensaver stopped |
"screensaverWillStop" |
The screensaver is about to stop |
"systemWillSleep" |
The Mac is about to sleep |
"systemDidWake" |
The Mac woke from sleep |
"systemWillPowerOff" |
The user is logging out, or the Mac is about to shut down or restart |
"sessionDidBecomeActive" |
This user's session became the active one again, after fast user switching |
"sessionDidResignActive" |
Fast user switching moved to another user's session |
"change" |
The battery state changed. Read hs.power.batteryInfo() for the new state |
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 | HSPowerEvent | The event to listen for |
| listener | () => void | Called once, then automatically removed |
| Event | Description |
|---|---|
"screensDidSleep" |
The displays went to sleep |
"screensDidWake" |
The displays woke up |
"screensDidLock" |
The screen was locked |
"screensDidUnlock" |
The screen was unlocked |
"screensaverDidStart" |
The screensaver started |
"screensaverDidStop" |
The screensaver stopped |
"screensaverWillStop" |
The screensaver is about to stop |
"systemWillSleep" |
The Mac is about to sleep |
"systemDidWake" |
The Mac woke from sleep |
"systemWillPowerOff" |
The user is logging out, or the Mac is about to shut down or restart |
"sessionDidBecomeActive" |
This user's session became the active one again, after fast user switching |
"sessionDidResignActive" |
Fast user switching moved to another user's session |
"change" |
The battery state changed. Read hs.power.batteryInfo() for the new state |
try {
hs.power.once('systemDidWake', () => console.log("Welcome back"))
} catch (err) {
console.error(err.message)
}