# Use variables

> Create Self, Parent, Position, and Workflow variables, insert them into fields with the variable browser, and transform them with expressions.

Once you understand [where a variable is visible](/docs/buttons/v1.10/variables/understand-variable-scope), using one is the same everywhere in Buttons: browse for it, insert it, and optionally transform it with an expression.

## Before you begin

- A variable already defined, see the scope-specific steps below if you haven't created one yet.

## Create a Self or Parent variable

1. Select the button or section that should hold the variable.
2. In the inspector, open its **Variables** tab.
3. Select **+** next to **Self** to add a variable that only this element can see, or next to **Parent** to add one to the section this element sits in, shared by everything directly inside that section. The **Parent** heading names that section.
4. In **Add Variable**, enter a **Name**, an optional **Description**, and a **Type**, then select **Create**.
5. Enter an **Initial** value for the new variable, then select **Apply**.

![The Variables tab of a section named Custom Router 1, with a Self variable called camera whose Initial value is Camera 1, an empty Parent group for Root Section, and a Position variable called menu](https://bitfocus.io/api/docs-media/file/section-variables-tab.png?prefix=docs-media)

Each variable shows the reference to use it by, such as `$(self.state.camera)`, next to its name.

## Create a Position Variable

1. Open the position's **Configuration** tab and find **Position Variables**, or open any element's **Variables** tab and use its **Position** group.
2. Select **+**.
3. Enter a **Name** and an optional **Description**, and choose a **Type**: **Boolean**, **Connection**, **Feedback**, **Number**, **Routing Endpoint**, or **String**. For **Connection**, also choose the **Connection Type (Module)** the variable may point to.

![The Add Variable dialog for a Position variable named switcher, with the description Vision mixer for this desk, the type Connection, and the module Blackmagic Design ATEM 4.3.1](https://bitfocus.io/api/docs-media/file/add-position-variable.png?prefix=docs-media)

4. Select **Create**, then set its **Initial** value for this position and select **Apply**. Each position that uses this variable needs its own value set independently.

To use a Position Variable to swap which connection a control targets, set its type to **Connection**, then use the **Set Position Variable** Internal Action on a button to change it at runtime.

## Create a Workflow Variable

1. Open **Workflow**, and open or create a Workflow.
2. Drag a **Workflow Variable** node from the node pool onto the canvas. The **Create Variable** dialog opens.
3. Give it a **Variable Key**: this is the individual variable's own key, separate from the Workflow's own Variable Key (which defaults to the Workflow's name and is used for other purposes on the Workflow itself).
4. Set **Type**: String, Number, or Boolean.
5. Set an **Initial Value**, or turn on **Persist Value** to keep the variable's current value across a restart instead of resetting to its initial value.

![The Create Variable dialog with the Variable Key show_running, the type Boolean, and the Persist Value and Initial Value switches both off](https://bitfocus.io/api/docs-media/file/create-workflow-variable.png?prefix=docs-media)

6. Select the check mark to create the node, then select **Apply** on the workflow.

Change its value from anywhere using the **Set Workflow Variable** Internal Action.

## Find and insert a variable

Most text fields in the inspector have a small folder icon at the end. Selecting it opens the field in an editor with **Browse Variables** underneath. Variables are grouped into folders: **self**, **parent**, **position**, **time**, then one folder per connection (named by its Variable Key) and one per Workflow. The number on a folder is how many variables it holds. A folder without a number, such as a connection or Workflow with no variables, has nothing to insert.

![The Edit Text dialog with Browse Variables listing folders for self, parent, position and time, then connections such as Videohub, ATEM and Mini, then workflows such as WFVariable, each with a count of its variables](https://bitfocus.io/api/docs-media/file/browse-variables.png?prefix=docs-media)

Open a folder and select a variable to insert it, then select **Done**.

## Include the connection's prefix

A connection variable's full name starts with that connection's **Variable Key** (set on the connection's **Config** tab), then a dot, then the variable's own name. If you leave the prefix out, Buttons can't tell which connection you mean, and the reference doesn't work.

For a connection whose Variable Key is `ATEM`:

| Write this | Not this |
| --- | --- |
| `$(ATEM.pgm1_usk_1_onAir)` | `$(pgm1_usk_1_onAir)` |
| `$(ATEM.device_ip)` | `$(device_ip)` |

The same applies inside an expression. An expression that's true while upstream key 1 is on air on M/E 1 is written as:

```
$(ATEM.pgm1_usk_1_onAir) == true
```

If you rename a connection's Variable Key, for example from `ATEM` to `StudioA`, every reference has to use the new prefix: `$(StudioA.pgm1_usk_1_onAir)`.

<callout color="green" title="Tip">
  Pick variables from the variable browser or the expression editor's suggestions rather than typing them. Both insert the full name, prefix included.
</callout>

## Transform a variable with an expression

When a variable's raw value isn't quite what you need, use an expression instead of the variable directly:

1. Open the expression editor from the same field you'd insert a variable into.
2. Reference the variable inside the expression, and combine it with functions as needed. For example, `concat` combines several strings and variables into one value.

The expression editor suggests available functions and variables as you type, so you don't need to remember exact syntax.

## If you get stuck

| What you see | What to try |
| --- | --- |
| Two variables seem to conflict, or one shows the wrong value. | Check their scope: a Self variable in one element and an identically-named Self variable in another are never actually the same variable. |
| A Position Variable shows a different value than you expect. | Confirm you're looking at the right position: the definition is shared, but each position holds its own independent value. |
| A Workflow Variable resets after a restart when you expected it to keep its value. | Turn on **Persist Value** for that variable. |
| An expression fails with an error such as **Unexpected token**, or a condition always takes its **Else** branch. | Check that every connection variable includes its prefix: `$(ATEM.pgm1_usk_1_onAir)`, not `$(pgm1_usk_1_onAir)`. The error doesn't name the variable, so check each `$(…)` in the expression. |
| You can't find a variable you know exists. | Open the variable browser (the folder icon) and check under the scope you expect it in: Self and Parent variables only appear while you're inside the right element. |

## Where to go next

- [Understand variable scope](/docs/buttons/v1.10/variables/understand-variable-scope).
- [Understand nested variables](/docs/buttons/v1.10/variables/nested-variables).
- [Expression functions and operators reference](/docs/buttons/v1.10/reference/expressions).
