I’m trying to write a TICKscript that replaces missing values with average of last 5 points and write the new data to a bucket. Here is my code:
var currentDB = 'myDB' var currentRP = 'autogen' var measurement = 'machine26' var field = 'sensor155' var defval = batch |query('select mean("sensor155") as "sensor155" from "myDB".autogen.machine26 limit 5') .every(10s) .period(10s) .align() var realdata = batch |query('select "sensor155" as "sensor155" from "myDB".autogen.machine26') .every(10s) .period(10s) .align() realdata |join(defval) .as('realdata', 'defval') .tolerance(15s) |eval(lambda: if(isPresent("realdata.sensor155"), float("realdata.sensor155"), float("defval.sensor155"))) .as(sensor) |influxDBOut() .database('testDB') .retentionPolicy(currentRP) .measurement(measurement)
The thing I’m missing seems like the batch part that calculates the mean creates only one point. So join only creates one data point, whereas I want to check all points and if missing replace with the average. Also the first batch takes 5 points rather than last 5 valid points. I want to evaluate the last 5 valid points. I’m sure there must be a better way. How can I achieve this?
Thanks in advance.