The ValueState object contains information about the state of the value (value, UTC time of obtaining the value, status).
Constructor
var myState = new ValueState();
Properties
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.
const GOOD = 0;
const BAD = 1;
const BAD_NO_COMMUNICATION = 2;
const BAD_TYPE_MISMATCH = 3;
const BAD_OUT_OF_RANGE = 4;
const BAD_WRITE_NOT_SUPPORTED = 5;
const BAD_NOT_READABLE = 6;
const BAD_LICENSE_EXPIRED = 7;
const BAD_NOT_FOUND = 8;
const BAD_USER_ACCESS_DENIED = 11;
const BAD_OBJECT_DELETED = 12;
const BAD_NODE_ID_UNKNOWN = 13;
const BAD_OUT_OF_SERVICE = 14;
const UNCERTAIN = 9;
const UNCERTAIN_INITIAL_VALUE = 10;
// 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:
// 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;
The following example shows an expression that returns a state sequence as a result.
// 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:
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