HistorianGetData

(Engine-Level Function)

Description:

Queries an Historian data store and returns an array of processed records.

For most uses, GetTagHistory is recommended in place of HistorianGetData.

Returns: Invalid if parameters are invalid, otherwise 0.
Usage: Script Only.
Threaded: Yes
Related to: HistorianConnect | HistorianDeleteRecords | HistorianGetInfo | HistorianReadRecords | HistorianWriteRecords
Format: HistorianGetData (ConnectionHandle, TagName, Schema, Data, FieldNames, TPP, StartTime, EndTime, NumEntries,  Modes, StaleTimes, LastRestartTime, ErrorCode)
Parameters:  
ConnectionHandle
As returned by an HistorianConnect call.
TagName
A text string representing the desired table name. Engine code may modify this to conform with storage limitations.
Schema
A text expression describing the schema from which to retrieve data.
Data
A variable in which the data will be returned.

This will be a two-dimensional array representing the returned data, indexed by [col][row]. If TPP is non-zero, the number of rows in the result set will equal the value of the NumEntries parameter. If TPP is zero there could be fewer rows than specified by NumEntries
FieldNames
Names of the fields.
Typically "Value" for most tag values, or "Value!" for tag attribute data. (Value! was introduced with VTScada version 12 and is not available in legacy data. It cannot be retrieved by SQL queries.)
TPP
Required. Any numeric expression giving the time span in seconds for each array entry. Each array element will contain the data which correspond exactly to this time period which corresponds to 0 or more data points. If TPP is positive and FieldNames selects a text value, the first entry which falls in a time is read and Mode is ignored.

If TPP is equal to 0, the data is read and placed in the array on a one to one basis.

If TPP is less than 0, an error will be returned.
StartTime
Timestamp in UTC. The report will include values matching and later than this time.
EndTime
Ending timestamp. Ignored if TPP is non-zero. The report will include values less than and matching this value.
NumEntries
Number of log entries in the array (numEntriesRequested - determined by Size or TPP parameters of Getlog)
Use a negative value to retrieve values in reverse chronological order.
Modes

Optional numeric expression giving the method of handling the data. If TPP is greater than 0, the values that fall in each time span will be represented as follows:

Mode

Data Collection

0

Time-weighted average

1

Minimum in range

2

Maximum in range

3

Change in value over the range

4

Value at start of range

5

TimeOfMinInRange

6

TimeOfMaxInRange

7

SumOfZtoNZTransitions

8

SumOfNonZeroTime

9

Totalizer. (Invalid is counted as zero)

10

Deprecated. Equivalent to mode 4.

11

Difference between the start and end

values of a range (see following note)

12 Bitwise OR (see following note)
13 Bitwise AND (see following note)

Mode 11:

This is for hardware that can count to a limited number, then rolls over to start a fresh count. For example, a tag that counts equipment starts, but which can only count from 0 to 15 before wrapping around to start again. This gives a value of sixteen per roll-over since there must be one more start after fifteen to have the roll-over occur. A graph of a very simple case where there is a start every second would look like the following:

(Each rollover must be followed by a fresh start, not a gradually declining value. If there are a series of declining values then, because each is less than the previous, each counts as another rollover.)

The formula used is (RolloverCount * RolloverValue) + (LastValidValue - FirstValidValue).

Where, for any given time interval:

  • Rollovers are counted when a new value is smaller than the last valid value.
  • The RolloverValue is passed to the function in the parameter StaleTime.
  • LastValidValue starts with the last valid value in the previous time interval (if any), and is updated with each new valid value within the time interval.
  • FirstValidValue starts with the last valid value in the previous time interval (if any), and is updated with only the first valid value found in the current time span.

Given this example, suppose that you want a count of starts in each thirty-second interval. In the first interval there is no previous valid value, one wrap-around and a last valid value of 12. (1*16) + (12 - 0) = 28.

This is an idealized case. Consider a more realistic case where not all values are logged. (This may be due to factors such as polling intervals, deadbands, hardware, etc.) The following graph shows the same data where the orange dots represent values obtained and logged and the gray line represents what really happened:

In the first 30 seconds there is one roll-over when the value drops from 13 to 2. The first value is 0 and the last is 11. The Historian will return 27: (1*16) + (11-0) = 27.

The Historian will return 30 for the period from 30 to 60. The first valid value (carried from the previous period) is 11. The last valid value is 9 and there are two roll-overs. (2*16) + (9-11) = 30.

Modes 12 and 13:

These are intended to provide a summary of the bit-wise Historian value attributes within a query. (Value attributes were added in VTScada version 11.5 / 12.0)

With mode 12, the first value is OR'd with zero, and each successive value in the time period is OR'd with result of the last comparison. From the result you will know whether any particular attribute bit was set on any values within the period.

With mode 13, the first value is AND'd with a value having all bits set to 1 and each successive value is AND'd with the result of the last comparison. From the result you will know whether any particular attribute bit was set on all values within the period.

In the case of modes 5 and 6, FieldName should still be set to indicate the field on which the mode is to act. The return values will be times indicating the maximum or minimum in that field for each time span.

If TPP is less than or equal to 0, Mode is ignored. If the data is text, the first entry in a given time range is used for the array entry and Mode is ignored.

It is possible to retrieve more than one mode in a single GetTagHistory statement. To do this, pass an array of values in as the Mode parameter.

StaleTimes

The meaning of StaleTime depends on the selected Mode.
For all modes but 11, this optional parameter sets a maximum validity duration for data elements that are being TPP processed. Normally, every data point is treated as remaining valid until the next data point. If a valid StaleTime parameter is given, then any data point will be treated as invalid, StaleTime seconds after the recorded time. If TPP is less than or equal to 0, StaleTime is ignored. If StaleTime is not required but EnableDowntimeOverride is, then StaleTime should be given as an Invalid value.

For Mode 11, this is the maximum value of a range (see comments in the Mode parameter.).
It is possible to specify more than one stale time in a single GetTagHistory statement. To do this, pass an array of values in as the StaleTime parameter.

LastRestartTime
A Timestamp value used in UpTime processing. (see comments)
ErrorCode

A value in which the error code (if any) will be returned.

Defined error codes are provided in the reference chapter. See: Historian Error Codes

Comments:

If a record has been written that duplicates the timestamp of an existing record then, when reading, the newer record will be supplied in place of the older. This arbitration is performed by means of a Secondary (or Generation) Timestamp.

To support this, properties can be associated with Schema columns. One property defines the Primary Timestamp for the record and another property value defines the Secondary (or Generation) Timestamp for the record. If no secondary timestamp column is defined, then arbitration will not take place. If no primary timestamp column is defined, then retrieval of a specific time range is not possible.

Uptime processing allows VTScada to show gaps in data for periods when VTScada did not record any data. As long as there is at least one Historian logging on a machine, the logging system is deemed available and there is no downtime. Any gap indicates that the logging system was down. (That is, the application may be running but the Historian has lost its connection to its SQL database). The UpTime information is logged by an Historian tag and is therefore to be found in the Historian’s normal log data table.

No downtime invalids are inserted when reading in the reverse order.

Value Attributes...

Querying Value Attributes

Version 12 of VTScada introduced value attributes. These can be queried using "Value!" rather than "Value" for the field name or array of field names. Value! stores bitwise values according the following table.

Attributes flagged with a star (*) apply only if the I/O tag is attached to a DNP3 driver or a custom driver supporting this feature.

Refer to the constant, #NUM_DATA_ATTR_BITS, to discover the number of bits in data attributes for your version of VTScada.

Bit Number Defined Constant Name Meaning
0 #DATA_ATTR_MANUAL_DATA The value was from the tag's ManualData parameter
1 #DATA_ATTR_MANUAL_ENTRY The value was manually entered
2 #DATA_ATTR_EDITED_DATA The historical value was manually edited
3 #DATA_ATTR_QUESTIONABLE The tag was marked as questionable
4 #DATA_ATTR_STALE_DATA (*) The value was reported as stale by VTSDriver
5 #DATA_ATTR_IMPORTED (*) The value was imported from a CSV file
6 #DATA_ATTR_COMMFAIL (*) The driver experienced a comm failure
7 #DATA_ATTR_QUALITY (*) The tag has marked the value as unreliable
8 #DATA_ATTR_DRIVER_COMMFAIL (*) The PLC reported a downstream comm failure
9 #DATA_ATTR_DRIVER_QUALITY (*) The PLC reported the value may be unreliable
10 #DATA_ATTR_DRIVER_OVERRIDDEN (*) The PLC reported the value was overridden
11 #DATA_ATTR_DRIVER_NODATA (*) The PLC reported there is no data available

Example:

No example provided. This function is not recommended for most use.