Object MethodTouch and Actions

Automation.check(constraintTree)

Checks current device state against a condition tree and returns two booleans, matched and known. The first is true only when the verified condition is met.

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

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

When To Use It

Checks current device state against a condition tree and returns two booleans, matched and known. The first is true only when the verified condition is met.

Parameters and Return

Accepts a table or JSON tree. Leaf: kind="leaf", leafType and condition fields. Group: kind="group", operator="and"/"or", children. negate=true negates a node. An unknown result returns false, false and does not grant permission under tree-level NOT; decisive AND/OR branches may return known=true. For a negative Lua check use known and not matched. Malformed trees raise an error. The battery above threshold is inclusive: >=. New leaves: airplane_mode, nfc_enabled, location_enabled, music_active; battery_temperature {temperatureCelsius=30, comparisonMode="above"|"below"} (-50–150 °C, inclusive); weekdays {weekdays={1,2,3,4,5}} (ISO Monday=1, Sunday=7; unique and nonempty); day_of_month {dayStart=28,dayEnd=3} (inclusive 1–31, wrapping across the month boundary when start exceeds end); charging_source {chargingSource="ac"|"usb"|"wireless"|"dock"}; month_of_year {months={1,6,12}} (unique nonempty 1–12; default all months); time_since_boot {durationMs=60000,comparisonMode="above"|"below"} (0–315360000000 whole ms, default 0; inclusive >=/<=); auto_brightness (no parameters); brightness_level {levelPercent=50,comparisonMode="above"|"below"} (finite 0–100, default 50; inclusive >=/<=). New fields are additive; irrelevant nondefault values are rejected.

Best Combined With

Device Conditions mode in the Compare block performs the same check. Location reads only the device location switch, not GPS coordinates. NFC does not read tags; music checks overall music activity without selecting an app. Battery temperature is not CPU temperature. Month uses the current local calendar; elapsed boot time uses SystemClock.elapsedRealtime including sleep, independent of the wall clock. Brightness is the configured Android 0–255 setting percentage, not physical nits or an app override. Automatic brightness requires a known mode; use known and not matched to check manual mode. This call does not register listeners or wait for an event; use Trigger.run/watch for that.

Example Usage

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

if Automation.check({kind="leaf", leafType="battery", batteryMode="above", batteryPercent=20}) then
  Automation.action("toast", {text="Battery condition passed"})
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
if Automation.check({kind="leaf", leafType="battery", batteryMode="above", batteryPercent=20}) then
  Automation.action("toast", {text="Battery condition passed"})
end

Simple

Wraps the base call with minimal flow control.

Simple
local stepOk = true
if Automation.check({kind="leaf", leafType="battery", batteryMode="above", batteryPercent=20}) then
  Automation.action("toast", {text="Battery condition passed"})
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()
  if Automation.check({kind="leaf", leafType="battery", batteryMode="above", batteryPercent=20}) then
    Automation.action("toast", {text="Battery condition passed"})
  end
end)

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

Detailed

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

Detailed
-- Automation.check(constraintTree)
local function run_check_step()
  if Automation.check({kind="leaf", leafType="battery", batteryMode="above", batteryPercent=20}) then
    Automation.action("toast", {text="Battery condition passed"})
  end
end

local ok, err = pcall(run_check_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
if Automation.check({kind="leaf", leafType="battery", batteryMode="above", batteryPercent=20}) then
  Automation.action("toast", {text="Battery condition passed"})
end