# ValueState

The ValueState object contains information about the state of the value (value, UTC time of obtaining the value, status).

### Constructor <a href="#h.ash8fsgyvx9m" id="h.ash8fsgyvx9m"></a>

```javascript
var myState = new ValueState();
```

### Fields <a href="#t.346d8797d524e2760005308b7abf0942fdace6e5" id="t.346d8797d524e2760005308b7abf0942fdace6e5"></a>

| **Name** | **Description**                                                                                         |
| -------- | ------------------------------------------------------------------------------------------------------- |
| value    | Contains a value of arbitrary type                                                                      |
| status   | Contains the status of the value (number), e.g. *Good*, *BadNoCommunication*, *BadLicenseExpired,* etc. |
| time     | Contains the UTC time of obtaining the value (Date)                                                     |

### Examples

The following example demonstrates an expression that returns *True* if the data trigger state has changed from *BadNoCommunication*, otherwise returning *False*. In this example, the expression has a data trigger named *some\_value*.

```javascript
const GOOD = 0; // 0x00

const BAD = -2147483648; // 0x80000000
const BAD_NO_COMMUNICATION = -2144272384; // 0x80310000
const BAD_TYPE_MISMATCH = -2139881472; // 0x80740000
const BAD_OUT_OF_RANGE = -2143551488; // 0x803C0000
const BAD_WRITE_NOT_SUPPORTED = -2139947008; // 0x80730000
const BAD_NOT_READABLE = -2143682560; // 0x803A0000
const BAD_LICENSE_EXPIRED = -2129788928; // 0x810E0000
const BAD_NOT_FOUND = -2143420416; // 0x803E0000
const BAD_USER_ACCESS_DENIED = -2145452032; // 0x801F0000
const BAD_OBJECT_DELETED = -2143354880; // 0x803F0000
const BAD_NODE_ID_UNKNOWN = -2144075776; // 0x80340000
const BAD_OUT_OF_SERVICE = -2138243072; // 0x808D0000

const UNCERTAIN = 1073741824; // 0x40000000
const UNCERTAIN_INITIAL_VALUE = 1083310080; // 0x40920000

// Saving the status in an intermediate variable
var status = context.data['some_value'].state.status;

if (status == BAD_NO_COMMUNICATION)
  return true;
return false;
```

In the previous examples, the expression always returned only a value, but you can return the state of the expression as a result by defining a value, UTC time, and status:

{% hint style="warning" %}
Please note that creating an instance of ValueState will immediately put the script module into demo mode if it is not licensed
{% endhint %}

```javascript
// Initiating a ValueState object
var state = new ValueState();

// Value (must match the returned data type)
state.value = 12345;
state.status = 0; // Good

// Time: January 1, 1970, 14:15:16 UTC
// Note that in JavaScript, the month is represented by a number from 0 to 11
state.time = new Date(Date.UTC(1970, 0, 1, 14, 15, 16));

return state;
```

![](https://4282443477-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3cwznMbQcEQxtnjiRDYX%2Fuploads%2Fllw4btpufbaF50psjg49%2FValueState%20\(360039482532\)_image-0.png?alt=media\&token=52a31b3c-2587-4b66-98fa-ae1b7aa80c8d)

The following example shows an expression that returns a state sequence as a result.

```javascript
// The array where we will add up the state sequence
var sequence = new Array();

// Saving the milliseconds from January 1, 1970 00:00:00 to the present time UTC
var now = Date.now();

for (var i = 0; i <= 5; i++)
{
	var s = new ValueState();

	// Shifting the time of each state by 200 ms from the previous one
	s.time = new Date(now + i * 200);

	s.value = i;
	s.status = 0;   

	sequence.push(s);
}

return sequence;
```

The returned sequence cannot be seen in *Go Online* mode. In these cases, you can observe only the last element of the sequence (*number 5*), but if you specify this expression as the data source of the time series and set the discrete sampling mode for the series, you will see the following picture:

![](https://4282443477-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3cwznMbQcEQxtnjiRDYX%2Fuploads%2FLAYls3Qr7VqO2frxndjp%2FValueState%20\(360039482532\)_image-1.png?alt=media\&token=c066cbbf-cc23-46e7-b843-1d73642d0f88)

The feature described in the previous example allows you to create all sorts of filters or, for example, "unwrap" numerical arrays (INT\[]) into a sequence of numbers (INT, INT, INT...).

Note that the code ***return \[1, 2, 3, 4, 5]*** is not the same as the code described in the example above, in this case a value of INT\[] (array of integer) type is returned when the result of the sample is a sequence of values where each value is represented by an INT type
