custom_variable
Use this action to compute a value and store it in a variable for later actions.
Read the live config shape of each action type from GET /v1/automation/meta/action before you build on it.
In the automation editor, this action is Perform a calculation in the Code & variables family. Its group is action, and it works with every trigger.
Send it in the actions array of Create an automation or Update an automation. An update replaces the whole array, so include the other actions.
Example
This action estimates the yearly yield of a solar system from its size in kWp and stores the result in the variable Annual yield (kWh):
{
"type": "custom_variable",
"config": {
"assignment_code": [
"Math.round(Number(",
{ "kind": "variable", "source": "field", "field_id": 702, "field_type": "number", "previous": false, "collection": false, "triggering": false },
") * 950)"
],
"custom_variable_defs": [
{ "custom_type": "any", "label": "Annual yield (kWh)" }
]
}
}
Config
| Property | Type | Description | Example value |
|---|---|---|---|
assignment_code | code value | Required. One JavaScript expression. Tape stores its value in the declared variable. A plain string works too. | ["1 + 1"] |
custom_variable_defs | array of variable declarations | The variable that receives the value. Each entry is { "custom_type", "label" }, and both keys are required. Empty when left out. | [{ "custom_type": "any", "label": "Total" }] |
Variable types
custom_type takes one of these lower-case tokens:
anysingle_filemulti_filesingle_linkhtml_table_rows
With an empty or missing custom_variable_defs, this action still validates and saves. When the automation runs, Tape skips it without a per-action log, as it does with an empty assignment_code. Declare one variable so the action has a target.
Use the variable in a later action
Tape stores the value in the first variable of custom_variable_defs. A later action reads it with a custom reference that has the same custom_type and label:
{ "kind": "variable", "source": "custom", "custom_type": "any", "label": "Annual yield (kWh)" }
General behavior
assignment_codeis the only required key. It has no default.custom_variable_defsdefaults to[].- Tape evaluates
assignment_codeas one expression. Write1 + 1, notreturn 1 + 1;. A top-levelreturnfails when the automation runs, withIllegal return statement. - To store an object literal, wrap it in parentheses:
["({ total: ", …, " })"]. Without them, JavaScript reads the braces as a block. - Declare one variable per action.
- The action's own
assignment_codecan't reference the variable. - The write path doesn't check the syntax. A broken expression saves and passes
validate. The error shows up only in the run log. - For a block of statements instead of one expression, use
custom_code.
Requirements
- To add or change this action through the API, your token needs the
automations:editcapability. See Capabilities. - You must administer the workspace that the automation's database app belongs to.