> For the complete documentation index, see [llms.txt](https://www.opal-rt.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.opal-rt.com/docs/hypersim/automation-api-and-scripting/automate-using-testview/testview-examples/use-variables-and-conditional-logic.md).

# Use Variables & Conditional Logic

## Description

This example builds on the [**Automate Fault Timing Tests**](https://www.opal-rt.com/docs/hypersim/automation-api-and-scripting/automate-using-testview/testview-examples/automate-fault-timing-tests.md) example by introducing the use of **variables**, **loops**, and **conditional expressions** in TestView. Its objective is to demonstrate how model parameters can be modified dynamically during a test sequence. More specifically, this example shows how to adjust the resistance of **Ld2** in the **HVAC\_500kV\_6Bus** example model found under **Transmission**.

## Detailed explanation

{% stepper %}
{% step %}
**Prepare the example**

Prepare the base of the example by follow the steps in **Launch TestView & Run a Test** section: Open the **HVAC\_500kV\_6Bus** example model, load the `.csv` sensors file, start and stop the simulation once, then launch TestView and create a new TestView project, study, and test, adding the required **HYPERSIM Test** and **HYPERSIM Settings** commands.
{% endstep %}

{% step %}
**Verify HYPERSIM Settings**

In the TestView test sequence, double‑click the **HYPERSIM Settings** command to open the dialog and ensure **Disable Time Events (clrtab)** is **unchecked**. This ensures time‑based events (faults, operations) will be executed.

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-3de08487c289ed2c13a4f00058fa58e61f49b0d7%2Fimage%20(1333).png?alt=media" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
**Add a 1‑second delay using the Sleep command**

Adding a 1-second delay ensures the simulation reaches steady‑state before the fault. In the **Palette** tab, click **Sleep**, accept the default **1‑second** delay and click **Apply**.

The script now begins with:

* HYPERSIM Test
* HYPERSIM Settings
* Sleep (1s)

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-36f5251f48c68c4f950fd22bed063b705464bdf8%2Fimage%20(1334).png?alt=media" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
**Add an Assign command**

Add an **Assign** command to define all variables required in the equation. Initialize them to **0**, ensuring TestView treats them as numeric values.

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-ae40aad018cbd867e9178bb07b4631b7aac88ba8%2Fimage%20(1422).png?alt=media" alt=""><figcaption></figcaption></figure>

1. First give a title to the **Assign** command.
2. In **Symbol**, type **max\_fault\_current**.
3. In **Value**, type **0.0**.
4. Click **Add**. The variable will be added to the list.
5. Repeat 2, 3, 4 for **time\_fault**, **user\_value** and **res\_RLd2**.
6. Then click **Apply**.

Failure to initialize these variables will cause the script to crash during the first evaluation.
{% endstep %}

{% step %}
**Add a Miscellaneous command to enable the fault**

1. Add a **Miscellaneous** command. In the **Parameter** section, each drop‑down menu should display a list of components and parameters from the model.

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-4f9c096174522e4d828a16330d64b02395c449a7%2Fimage%20(1398).png?alt=media" alt="" width="432"><figcaption></figcaption></figure>

{% hint style="info" %}
If drop‑downs are empty, reconnect TestView to HYPERSIM: Close HYPERSIM and TestView, open the **copy** of the model located in `ProjectName\bank\hnk\ProjectName.ecf`, run the model once and re-open TestView. Alternatively, use the **connection icon** to disconnect/reconnect.
{% endhint %}

2. In the **Component** drop-down list, select **Fault**. In the **Parameter** drop-down list, select **EnaGen**. In the **Value** field, type **1**. Click **Add**.

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-2e75e00fa22e999f8c785d03b3aa3b8ba98b8914%2Fimage%20(1399).png?alt=media" alt=""><figcaption></figcaption></figure>

3. Add a second parameter for the **Ld2** resistance to ensure it always starts with a consistent value. In the **Component** drop-down list, select **Ld2**. In the **Parameter** drop-down list, select **R**. In the **Value** field, type **500**. Click **Add**, then **Apply.**

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-07ee650ab3dff1677d64ca83c7f4c03260f5064f%2Fimage%20(1423).png?alt=media" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
**Add a Snapshot command**

Snapshots preserve currents and voltages, ensuring each iteration starts under identical conditions.

Add a **Snapshot** command, select **Take a snapshot of the simulation** and click **Apply**.

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-36a0c5bd2b6747a74b12a9b738b9a2261f877d13%2Fimage%20(1401).png?alt=media" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
**Add a Loop command for three iterations**

Add a **Loop** command, check the **User defined** and **Operation** boxes, set **Counter variable name** to index, **Initial value** to 0, **Final value** to 4 and **Step value** to 1. Click **Apply**.

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-d8e70a985e430b7ccbf5c0ffddb2ad636e472389%2Fimage%20(1424).png?alt=media" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
**Load the Snapshot inside the loop**

Add another **Snapshot** command. Uncheck **Take snapshot**, check **Load the current snapshot**, and click **Apply**. This ensures each iteration starts from the same steady‑state.

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-dc706f553f399d1d2c7505ccbd503d844a7fc32b%2Fimage%20(1403).png?alt=media" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
**Set fault timing using the Breaker command**

1. Add a **Breaker** command, enable **multi‑param**, select component **Fault** and check parameters **SW\_T1** and **SW\_T2**.

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-dbc00a166de38125021ec6da5a7e392c712d721d%2Fimage%20(1404).png?alt=media" alt=""><figcaption></figcaption></figure>

2. For both SW\_T1 and SW\_T2 tabs, press **Add** three times and enter the timings **0.025 / 0.050 / 0.075 / 0.09**, then click **Apply**.

{% hint style="info" %}
If a tab is red, it does not have the same number of rows as the others.

The loop runs **4 iterations**.
{% endhint %}

The test sequence should now look like this:

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-da48b281077f84ff7e03501b8ff066619e61d345%2Fimage%20(1425).png?alt=media" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
**Add if/else logic**

1. Add an **If(expression)** command, type the expression `%{index} < 2` and click **Apply**.

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-ee4132af1f7b8d4b3d8bf239c63a77bb0b597cda%2Fimage%20(1426).png?alt=media" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
The `%{}` syntax is essential for TestView to correctly interpret a name as a **variable** rather than a literal **string**. Without it, TestView treats the content as plain text. This would cause the script to fail. For example, attempting to compare a string to an integer during a conditional expression would lead to a crash.
{% endhint %}

2. Add a **Pause/Message** command, uncheck the **Pause** box (optional), and type any message you would like to see in the **stdout** console at this stage. Click **Apply**.

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-049ba1a42d1f2c340c68a4580673adac25ab9e38%2Fimage%20(1427).png?alt=media" alt=""><figcaption></figcaption></figure>

3. Add an **Else\[if(expression)]** command. Do **not** enter any expression in the dialog box; simply click **Apply**.

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-464c381f3978160a76ca50263c7a0416389cd033%2Fimage%20(1278).png?alt=media" alt=""><figcaption></figcaption></figure>

4. Add another **Pause/Message** command.

The test sequence should now look like this:

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-32f45ef1f12b5668f6a3768eddf7f5406b94544c%2Fimage%20(1428).png?alt=media" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
If the commands are not correctly indented, use the arrow buttons at the bottom of the main TestView window to move them to their proper positions.
{% endhint %}
{% endstep %}

{% step %}
**Ask for user input and compute res\_RLd2 (first two iterations)**

Inside the **If** branch:

1. Add an **Input Value** command, enter `user_value` then click **Apply**.

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-7d96f543af93547745586f9c05f74199d6cda81d%2Fimage%20(1429).png?alt=media" alt=""><figcaption></figcaption></figure>

2. Add an **Eval** command, enter `res_RLd2 = 300*%{user_value} - %{max_fault_current}/500` then click **Apply**.

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-5b59570e125cd87fb82eb1e566e857f559479626%2Fimage%20(1430).png?alt=media" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Notes:

* `res_RLd2` should **not** be wrapped in `%{}` (it is a variable being assigned).
* In the first iteration (index=0), `{max_fault_current}`=0 therefore `res_RLd2` equals to 300 times the user input value `user_value`.
* All referenced variables **must** use `%{}` so TestView substitutes their values.
  {% endhint %}
  {% endstep %}

{% step %}
**Compute res\_RLd2 (final two iterations)**

Inside the **Else** branch, add an **Expression** command, enter `res_RLd2 = 300 - %{time_fault}*1000`, click **Add** then click **Apply**.

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-f0671345e9d604f167910352393d74f6d2161e53%2Fimage%20(1431).png?alt=media" alt=""><figcaption></figcaption></figure>

The test sequence should now look like this:

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-009366e77fb1c17bd2872deebbf197186bdd94c5%2Fimage%20(1432).png?alt=media" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
**Apply the computed res\_RLd2**

Add a **Miscellaneous** command (outside If/Else, but inside the Loop). In the **Component** drop-down list, select **Ld2**. In the **Parameter** drop-down list, select **R**. In the **Value** field, type `%{res_RLd2}`. Click **Add**. Then click **Apply**.

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-a1865f66e9d889ab6e33dce9471cae3da2b0ee9a%2Fimage%20(1434).png?alt=media" alt=""><figcaption></figcaption></figure>

The **Miscellaneous** command must be outside the **If/Else**, but inside the **Loop**. The test sequence should now look like this:

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-9a531837b5f8b0bd5f6e9f734253f1c8ff021b50%2Fimage%20(1435).png?alt=media" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
**Add the Processing command**

Add a **Processing** command from the **Main** folder to acquire signals using ScopeView’s engine.

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-d880c805eeba97dbae52fdf3a2ef129b85dc58bd%2Fimage%20(1405).png?alt=media" alt="" width="152"><figcaption></figcaption></figure>

In the **General** tab:

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-901295807be6a8c61b6d6b0098c6f21a252b53b7%2Fimage%20(1438).png?alt=media" alt=""><figcaption></figcaption></figure>

1. Set the **Sampling rate** = `1 / timestep` = **20,000 Hz** for 50 µs timestep and check both the **Synchronization** (POW-based) and **Operation sequence** (executes fault and breaker sequence) boxes.
2. Ensure **Show graphic** boxes are **checked** for both processing modes.
3. Click **Signals Key**, select the **Breaker** category and click once on **Fault.CMDa** (command signal from **Fault** block), **Di1.Ia**, **Di1.Ib** and **Di1.Ic** (currents from the breaker near the fault location) to select them.

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-e8b63e79133242158d9747ef042a5bb8a192d3dc%2Fimage%20(1406).png?alt=media" alt=""><figcaption></figcaption></figure>

4. Save as a new keyname, give a name and then click **Ok**.

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-cf6ac15a32c80c3689663f7aafa67eaed31f5950%2Fimage%20(1407).png?alt=media" alt=""><figcaption></figcaption></figure>

In the **Processing** tab:

1. Only the signals from the Signals Key appear. Click once on **Fault.CMDa**, **Di1.Ia**, **Di1.Ib** and **Di1.Ic** to select them.

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-161eca014a7e59a4fe3a284a537214b9e2ea232e%2Fimage%20(1409).png?alt=media" alt=""><figcaption></figcaption></figure>

2. In the **Selected Signals** list, type the two following functions:

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-e5af70ae9d8e4e12c9daac56b9342d5a8322b501%2Fimage%20(1436).png?alt=media" alt=""><figcaption></figcaption></figure>

In the **Post-Processing** tab:

Add the two resulting values for comparison over the four iterations.

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-28b26131b45a3f6b04c321ed42aa4ff8bd66a549%2Fimage%20(1437).png?alt=media" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
**Update variables**

Add an **Expression** command and give it a **Title.**

1. Type the expression `max_fault_current=%{max_current_phaseA}`, then click **Add**.
2. Type the expression `time_fault=%{time_CMDa}`, click **Add**, then click **Apply**.

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-f1d2ec152c68011cba6cb78e7eb5d21418efcbe3%2Fimage%20(289).png?alt=media" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
**Review the test sequence**

The test sequence is now complete and should look like in the following image.

Go to the **View** tab and check **Show** under the **Processing** section. This displays waveforms after each iteration.

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-14419312c9ccd0d94037d5520513713cae90a7b3%2Fimage%20(290).png?alt=media" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
**Run the test**

In the **Runner** tab, choose **Current test only**, then press **Play**.

{% hint style="danger" %}
Do **not** enter values ≤ 0 in the user input. This results in zero or negative resistance, causing the test to fail.
{% endhint %}

Expected processing waveform:

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-a102d7abc5c84485e0a18a3338b8e327422b31a3%2Fimage%20(291).png?alt=media" alt=""><figcaption></figcaption></figure>

Expected post‑processing graphs (four points each, one per iteration):

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-bff8918f56958b1d473270c856b69b940ad0ffb5%2Fimage%20(292).png?alt=media" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
In post‑processing, the x‑axis represents **test iteration number**, not time.
{% endhint %}
{% endstep %}
{% endstepper %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.opal-rt.com/docs/hypersim/automation-api-and-scripting/automate-using-testview/testview-examples/use-variables-and-conditional-logic.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
