# Custom tag history aggregate question

**URL:** <https://forum.inductiveautomation.com/t/custom-tag-history-aggregate-question/45405>\
**Category:** Ignition\
**Tags:** ignition80\
**Created:** [April 8, 2021, 5:55pm UTC](https://forum.inductiveautomation.com/t/custom-tag-history-aggregate-question/45405 "2021-04-08T17:55:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bschroeder](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bschroeder/32/13260_2.png) [@bschroeder](https://forum.inductiveautomation.com/u/bschroeder)\
**Post date:** [April 8, 2021, 5:55pm UTC](https://forum.inductiveautomation.com/t/custom-tag-history-aggregate-question/45405/1 "2021-04-08T17:55:09Z")

</div>

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…

```auto
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
        else:
            #Get the millis between the last and current
            millis = system.data.millisBetween(lastTs,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.

---

<div class="post-metadata">

**Author:** ![hankhaines](https://avatars.discourse-cdn.com/v4/letter/h/ecd19e/32.png) [@hankhaines](https://forum.inductiveautomation.com/u/hankhaines)\
**Post date:** [April 8, 2021, 6:59pm UTC](https://forum.inductiveautomation.com/t/custom-tag-history-aggregate-question/45405/2 "2021-04-08T18:59:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bschroeder](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bschroeder/32/13260_2.png) [@bschroeder](https://forum.inductiveautomation.com/u/bschroeder)\
**Post date:** [April 8, 2021, 7:13pm UTC](https://forum.inductiveautomation.com/t/custom-tag-history-aggregate-question/45405/4 "2021-04-08T19:13:24Z")

</div>

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