The ValueState object contains information about the state of the value (value, UTC time of obtaining the value, status).
Constructor
var myState =newValueState();
Fields
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.
constGOOD=0; // 0x00constBAD=-2147483648; // 0x80000000constBAD_NO_COMMUNICATION=-2144272384; // 0x80310000constBAD_TYPE_MISMATCH=-2139881472; // 0x80740000constBAD_OUT_OF_RANGE=-2143551488; // 0x803C0000constBAD_WRITE_NOT_SUPPORTED=-2139947008; // 0x80730000constBAD_NOT_READABLE=-2143682560; // 0x803A0000constBAD_LICENSE_EXPIRED=-2129788928; // 0x810E0000constBAD_NOT_FOUND=-2143420416; // 0x803E0000constBAD_USER_ACCESS_DENIED=-2145452032; // 0x801F0000constBAD_OBJECT_DELETED=-2143354880; // 0x803F0000constBAD_NODE_ID_UNKNOWN=-2144075776; // 0x80340000constBAD_OUT_OF_SERVICE=-2138243072; // 0x808D0000constUNCERTAIN=1073741824; // 0x40000000constUNCERTAIN_INITIAL_VALUE=1083310080; // 0x40920000// Saving the status in an intermediate variablevar status =context.data['some_value'].state.status;if (status ==BAD_NO_COMMUNICATION)returntrue;returnfalse;
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:
Please note that creating an instance of ValueState will immediately put the script module into demo mode if it is not licensed
// Initiating a ValueState objectvar state =newValueState();// 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 11state.time =newDate(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 sequencevar sequence =newArray();// Saving the milliseconds from January 1, 1970 00:00:00 to the present time UTCvar now =Date.now();for (var i =0; i <=5; i++){var s =newValueState();// Shifting the time of each state by 200 ms from the previous ones.time =newDate(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