API Docs

Monitor 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

number
The current battery charge percentage (0–100), or `-1` if no battery is present.

hs.power.isCharging

boolean
Whether the battery is currently charging. Returns `false` when no battery is present.

hs.power.powerSource

string
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.isLowPowerMode

boolean
Whether Low Power Mode is currently active.

hs.power.thermalState

string
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. idle sleep), `"systemIdle"` (prevent system idle sleep), `"system"` (prevent all system sleep, including from power button or lid close).
hs.power.preventSleep(type) -> boolean
Name Type Description
type string The sleep type to prevent. One of: `"display"` (prevent display
boolean
`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"`.
boolean
`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"`.
boolean
`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
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]]
[[String: Any]]
An array of objects with `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
None
  hs.power.systemSleep()

hs.power.lockScreen() -> None

Locks the screen immediately.
hs.power.lockScreen() -> None
None
This function uses private API to lock the screen, it may break in a future macOS update
  hs.power.lockScreen()

hs.power.startScreensaver() -> None

Starts the screensaver immediately.
hs.power.startScreensaver() -> None
None
  hs.power.startScreensaver()

hs.power.batteryInfo() -> [String: Any]

Returns a snapshot of all available battery information, or `null` if no battery is present.
hs.power.batteryInfo() -> [String: Any]
[String: Any]
An object with battery fields, or `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
None
Throws an Error on failure; wrap calls in try/catch to handle it.
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`
None
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
None
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
hs.power.once('systemDidWake', () => console.log("Welcome back"))
} catch (err) {
console.error(err.message)
}