# Variable Functions

Variable functions are used to read/write variables defined in the inSCADA Platform with script expression.

{% hint style="info" %}
Variable Object: A JSON object returned by variable functions. It contains the time stamp and value.

{"@class":"com.inscada.mono.communication.model.values.NumberVariableValue","value":60,"date":1564649882811}
{% endhint %}

## Get Variable Value

#### object ins.getVariableValue(var variablename)

Reads the value of a variable defined in inSCADA.

variablename : String

Return Value: Returns the Variable object. If the value returned is "null", it means the operation failed. Variable could not be read.

#### Syntax

```javascript
var variable=ins.getVariableValue("UNIT01_Active_Power");
if (variable!==null) {
    if (variable.value>0) {
        ins.notify("info","Unit 01","Unit 1 is working");
    }
    return variable.value;
}
return -1;
```

## Get Variable Previous Values

#### object ins.getVariableValue(var variablename,var index)

Reads the value of a variable defined in inSCADA from previous scans. inSCADA stores the last 10 readings of the variables. You can reach these 10 values with index.

variablename : String

Return Value: Returns the Variable object. If the value returned is "null", it means the operation failed. Variable could not be read.

#### Syntax

```javascript
var variable=ins.getVariableValue("UNI01_Active_Power");
if (variable===null) {return -1}
var prv_variable=ins.getVariableValue("UNIT01_Active_Power",1);
if (prv_variable===null) {return -1}
if (variable.value!==prv_variable.value) {
    ins.notify("info","Unit 01","Power value is changed");
}
return variable.value;
```

## Get Variables Values

#### Array\[] ins.getVariableValues(var variablenames\[])

Reads the values of the requested variable list defined in inSCADA.

variablenames\[] : String array

Return Value: Returns an array of objects of the requested variables.

#### Syntax

```javascript
var TAGS=["UNCORRECTED_FLOW_RATE",
"MASS_FLOW_RATE",
"CORRECTED_FLOW_RATE",
"ENERGY_FLOW_RATE",
"INDICATED_UNCORRECTED_FLOW_RATE",
"UNCORRECTED_TOTAL",
"MASS_TOTAL",
"CORRECTED_TOTAL",
"ENERGY_TOTAL",
"INDICATED_UNCORRECTED_TOTAL",
"CONVERTION_FACTOR",
"CORRECTED_FACTOR",
"LINE_COMPRESIBILITY",
"BASE_COMPRESIBILITY",
"LINE_PRESSURE",
"LINE_TEMPERATURE",
"LINE_DENSITY",
"BASE_DENSITY"];

var VALUES=ins.getVariableValues(TAGS);
if (VALUES===null) {return -1;}

var mass_flow_rate_value=VALUES[1].value;
var mass_flow_rate_timestamp=VALUES[1].date;
```

## Get Project Variables

#### object ins.getProjectVariableValues()

Reads the value of all variables connected to the project in inSCADA .

Return Value: Returns a JSON object with all variables and values for the project.

#### Syntax

```javascript
var result=ins.getProjectVariableValues();
if (result!==null) {
    result;
    }
return -1;
```

## Set Variable Value

#### ins.setVariableValue(String variablename, object value)

Writes a value to a variable defined in inSCADA.

variablename: The name of the variable to write the value to.

#### Syntax

```javascript
ins.setVariableValue("UNIT01_Active_Power_Set",{value:100});
```

## Set Variables Values

#### ins.setVariableValues(object variables)

Writes values to variables defined in inSCADA at once.

variables: A JSON object containing variables and the values to be written.

{% hint style="info" %}
ins.setVariableValues, unlike ins.setVariableValue, allows values to be written to more than one variable at a time. When doing this, it optimizes the writing process in accordance with the protocol and sends the values in the same data block to the connected device at one time. A function that can be used to transfer multiple data.
{% endhint %}

#### Syntax

```javascript
ins.setVariableValues(obj);
```

## Map Variable Value

#### ins.mapVariableValue(String source, String destination)

Writes a variable value to another variable.

Source: The variable name of the value to be read. A source variable.

Destination: Name of the variable to be write the value. A target variable.

#### Syntax

```javascript
ins.mapVariableValue("UNIT01_Active_Power","UNIT01_Active_Power_Set");
```

#### ins.mapVariableValue(String source, String destination, var defaultvalue) <a href="#ins-mapvariablevalue-string-source-string-destination-var-defaultvalue" id="ins-mapvariablevalue-string-source-string-destination-var-defaultvalue"></a>

If the source variable cannot be read, it writes a default value to the target variable.

```javascript
ins.mapVariableValue("UNIT01_Active_Power","UNIT01_Active_Power_Set",-1);
```
