Resources
Install: Google Play
Important resources:
- All-In-One-Page Guide (very long): https://automagic4android.com/components_en.html
- MAIN Docs Site
- Scripting:
- Action: Scripting
- Condition: Expression
- Built-in Functions
- Scripting:
Automagic - How Do I...
- Print something to the global console
- In Script action: Use
log() - Built-in Action:
Write to Log
- In Script action: Use
- Initialize an object:
- You have to start with empty object, using
fromJSON
fromJSON('{}') myObj['myKey'] = 'myVal'; - You have to start with empty object, using
- Check if a variable has a value / is not null
- Use
getValue-getValue(String variableName, Object, defaultIfUndefined)
isDefined = getValue('myVariable', false); if (isDefined) { // Do something } - Use
- Use time comparisons and date utilities
- Triggers will add a flow variable,
triggertime, which is stored as aNumberMS timestamp- Although it is a
Number, it works with functions likeaddDay().
- Although it is a
- There are multiple built-in functions for getting date/time info:
Date getDate()(and variations) - get current date & timeNumber getUptimeMillis()- MS since boot, excluding sleep timeNumber getElapsedRealtimeMillis()- MS since boot, including sleep time
- Use
dateformatfor formatting timestamps - There are multiple built-in functions for offsetting date and time
Date add___(Date d, Number a)addDays(d, a)addHours(d, a)addMinutes(d, a)addSeconds(d, a)
- Triggers will add a flow variable,
- Persist a value across executions
- There are many reasons why you might want to persist a value across executions; for example, for multiple counters, to track last execution time(s), or to cache API responses.
- Approaches
- By far, the easiest way to persist a value is with a Global variable. These are shared across flows, and persisted in non-volatile memory (reloaded if you restart Automagic).
- Simply prefix your variable name with
global_, such asglobal_myVar = "my string"; - Be careful about conflicting global variable names
- Simply prefix your variable name with
- Writing and reading out of text files might be a decent approach for persistent large amount of data (such as stringified JSON) (limit of 1MB)
- Use the
Write to Fileaction to write out a value to a file - Use the
Init Variable Text Fileaction to read the text file back into memory, as a variable
- Use the
- By far, the easiest way to persist a value is with a Global variable. These are shared across flows, and persisted in non-volatile memory (reloaded if you restart Automagic).
Automagic Example Script - Throttling Executions
This code can be used in an Expression condition step, to throttle executions of the flow to once per every 10 minutes.
throttleMins = 10;
lastExecutedAt = getValue('global_lastExecAct', 0);
minSinceLastExecuted = ((triggertime - lastExecutedAt) / 1000 / 60);
if (minSinceLastExecuted > throttleMins) {
global_lastExecAct = triggertime;
return true;
}
else {
log("Skipping execution - last executed " + minSinceLastExecuted + " min. ago");
return false;
}