Question:
How can I calculate a historic tag’s average inside a start/end date/time range?
Solução:
To do so, use Average function.
Average (src, type, startTime [, endTime])
This function returns the average of a historic data set. src parameter determines the name of the tag recorded in the historic. type parameter determines the time interval to be considered to calculate the average, which could be:
Time interval
0 – Year
1 – Month
2 – Week
3 – Day
4 – Hour
5 – Minute
So basically, you will need to use the script below:
Hist1.Open()
average = Hist1.Average(“tag001”,1,StaDate,EndDate)
Hist1.Close()
Where:
- Hist1 is the historic being used
- average is a TagRam where the average value will be saved
- tag001 is the name of the tag whose average is being calculated
- StaDate is a TagRam with the start date and time values, in dd/mm/yyyy hh:mm:ss format
- EndDate is a TagRam with the end date and time values, in dd/mm/yyyy hh:mm:ss format
This means that the historic will first be opened with Hist1.Open(). After that, the average TagRam will be attributed to Average function. Finally, the historic will be closed after calculation with Hist1.Close().
Notice that in this example, average, StaDate and EndDate are RAM tags. To set up the date/time ranges, just add setpoints to the screen, link them to their respective tags, and set them up in Date Time format.