Object MethodSystem Control

System.getDeviceUptime()

Reads milliseconds since device boot, including sleep. It is not the wall clock.

Documented for app version 1.0.64Updated:

Detailed Explanation

This section explains when to use the API, how to call it, and which structures it works best with in production flow.

How To Call It

System is a global table: call its functions with a dot (System.<method>(...)), not a colon. Requires Macro Handler 1.0.62 or later.

When To Use It

In system flow, design steps as observe first and act second when the API can affect device state.

Parameters and Return

Takes no arguments; extra arguments return nil. Uses nonnegative SystemClock.elapsedRealtime and resets at reboot. Stopped execution, read failure or a negative result returns nil. It is not sleep-excluding uptimeMillis or a date/time value.

Options and Limits

Uses no settings service and requests no permission. This pure monotonic clock read is also available on the main UI thread. Wall-clock or time-zone changes do not change it; do not compare readings from different boots.

Best Combined With

Automation.check({kind="leaf",leafType="time_since_boot",durationMs=60000,comparisonMode="above"}) tests the same sleep-inclusive duration as a condition. It does not create a timer or persistent trigger.

Example Usage

The snippet below is a starter pattern that can be applied directly in runtime flow.

local elapsed = System.getDeviceUptime()
if elapsed ~= nil then print("Milliseconds since boot", elapsed) end

Copyable Progressive Examples

From foundation to combined usage, each level is provided as a separate code block so you can copy the level you need and adapt it directly.

Foundation

Shows the shortest direct way to call the API.

Foundation
local elapsed = System.getDeviceUptime()
if elapsed ~= nil then print("Milliseconds since boot", elapsed) end

Simple

Wraps the base call with minimal flow control.

Simple
local stepOk = true
local elapsed = System.getDeviceUptime()
if elapsed ~= nil then print("Milliseconds since boot", elapsed) end
if stepOk then
  wait(200)
end

Practical Flow

A practical pattern for real macros with pcall, logging, and guards.

Practical Flow
local ok, err = pcall(function()
  local elapsed = System.getDeviceUptime()
  if elapsed ~= nil then print("Milliseconds since boot", elapsed) end
end)

if not ok then
  print("API step failed: System.getDeviceUptime: " .. tostring(err))
  requestStop()
end

Detailed

This level packages the API into a reusable helper with error reporting.

Detailed
-- In system flow, design steps as observe first and act second when the API can affect device state.
local function run_getdeviceuptime_step()
  local elapsed = System.getDeviceUptime()
  if elapsed ~= nil then print("Milliseconds since boot", elapsed) end
end

local ok, err = pcall(run_getdeviceuptime_step)
if not ok then
  toast("Step failed")
  print(err)
end

Combined

Combines the API with related structures to form a more realistic workflow.

Combined
local elapsed = System.getDeviceUptime()
if elapsed == nil then error("Device uptime unavailable") end
print(elapsed / 60000, "minutes since boot")