Custom tag history aggregate question

So a customer of ours is asking to do some KWH usage reports for their plant.

This is just the beginning phase and I’m just trying to see what the best direction would be for this.

If we log the KW directly on a 1 minute tag group could I then use a custom tag aggregate to get the data back out in a KWH format?

IE in pseudo code ish…

def calckwh(qval, interpolated, finished, blockContext, queryContext):
    kwh = blockContext.getOrDefault('kwh',0.0)
    lastTs = blockContext.getOrDefault('lastTs',None)
    if qval.quality.isGood():
        if lastTs is None:
            #First data point. Save back the TS so we can calculate the next one
            blockContext['lastTs'] = qval.timestamp
            #Get the millis between the last and current
            millis =,qval.timestamp)
            #Calculate the kwH
            #Value/(3600000/millis between last values)
            thiskwh = float(qval.value)/(3600000/millis)
            blockContext['kwh'] = thiskwh + kwh
    if finished:
        return blockContext.getOrDefault('kwh',0.0)

The other option would be to do an expression tag that would represent the KWH and then do an aggregate off of that value.

I’d rather do the custom aggregate and not create another tag, however if I’m misinterpreting how to use it then I can go with the other options.

You should just use a history query where you can tell it how to total and then adjust things with filters.

1 Like

OK yeah… I’m over thinking things. Thanks for the head slap.

1 Like