Script Averaging Calculation

hi,

I am trying to use some scripting, but I don’t actually know Python, I am more of a scriptkiddie, I can copy scripts I find and modify it enough to usually get where I need to.

I am trying to get an average of flows from 3 hour blocks of time. I have this script running on a tag binding, and it seems to work correctly, I get a dataset that has 5 minute datapoints that I want from yesterday between midnight and 3am.

paths = ['[default]/ST25/_25_FI_110X_Total']
    now = system.date.now()
    previousDay = system.date.addDays(now, -1)
    time_midnight_previous_day =system.date.midnight(previousDay)
    startDate = time_midnight_previous_day
    endDate = system.date.addHours(startDate, 3)
    dataSetDay1=system.tag.queryTagHistory(paths=paths, startDate=startDate, endDate=endDate, aggregationMode = 'SimpleAverage', intervalMinutes = 5, returnFormat='Wide')
    return dataSetDay1

I then tried an additional expression:

mean({this.props.params.Day1}, "ST25/_25_FI_110X_Total")

and it seemed to work initially but now I am getting random “Data Quality: Bad_Stale”

There seems to be a way to get the average in Python but I don’t understand exactly how that needs to be written. Or is there a better option?

Thanks

I would use queryTagCalculations instead of queryTagHistory

system.tag.queryTagCalculations | Ignition User Manual

2 Likes

Thanks,

I guess that does what I wanted in 1 step. I was able to get it working with this:

    now = system.date.now()
    previousDay = system.date.addDays(now, -1)
    time_3am_previous_day = system.date.setTime(previousDay, 3, 0, 0)
    startDate = time_3am_previous_day
    endDate = system.date.addHours(startDate, 3)    
    
    data = system.tag.queryTagCalculations(
    paths=['[default]ST25/_25_FI_110X_Total'],
    calculations=['Average'],
    startDate=startDate,
    endDate=endDate
)
    return data.getValueAt(0,1)