# Can we derive history from historical tag - duration above a certain value

**URL:** <https://forum.inductiveautomation.com/t/can-we-derive-history-from-historical-tag-duration-above-a-certain-value/62224>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [July 22, 2022, 4:03pm UTC](https://forum.inductiveautomation.com/t/can-we-derive-history-from-historical-tag-duration-above-a-certain-value/62224 "2022-07-22T16:03:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [July 22, 2022, 4:03pm UTC](https://forum.inductiveautomation.com/t/can-we-derive-history-from-historical-tag-duration-above-a-certain-value/62224/1 "2022-07-22T16:03:07Z")

</div>

I have a tag monitoring an analog process value. It has been logged in the historian for many months. I’m displaying the value on a PowerChart.  
I would like to add a label below the chart showing the amount of time the value was above a certain threshold.

Can anyone suggest a way of doing that with a history query? It would be like setting the aggregation mode to **DurationOn** but with a threshold of my choosing.

Many thanks.

---

<div class="post-metadata">

**Author:** ![craigb](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@craigb](https://forum.inductiveautomation.com/u/craigb)\
**Post date:** [July 24, 2022, 3:28pm UTC](https://forum.inductiveautomation.com/t/can-we-derive-history-from-historical-tag-duration-above-a-certain-value/62224/2 "2022-07-24T15:28:17Z")

</div>

A very very crude way if its saved periodically is COUNT rows where value \>= \* poll rate.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [July 25, 2022, 3:07pm UTC](https://forum.inductiveautomation.com/t/can-we-derive-history-from-historical-tag-duration-above-a-certain-value/62224/3 "2022-07-25T15:07:02Z")

</div>

There is a little used/known, hidden in plain sight, capability of the historian scripting functions to accept custom aggregates. [Custom Tag History Aggregates - Ignition User Manual 8.1 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC81/Custom+Tag+History+Aggregates)

There are some restraints to their use for them to be the most performant:

- The return size provided must be greater than 0
- They should be used with a project script
- The script must be located in the Gateway Scripting Project
- The script library must have a name that starts with “shared” all lower case.

All of this is in the documentation.

Then you can provide a function similar to this to get what you are looking for.

```python
def durationAbove(qval, interpolated, finished, blockContext, queryContext):
	duration = blockContext.getOrDefault('duration',0)
	lastTime = blockContext.getOrDefault('lastTime',None)
	if qval.quality.isGood() and qval.value >= 200:
		if not lastTime:
			blockContext['lastTime'] = qval.timestamp
		else:
			blockContext['duration'] += system.date.millisBetween(lastTime,qval.timestamp)
			blockContext['lastTime'] = qval.timestamp
	
	if finished:
		return blockContext.getOrDefault('duration',0)

```

Note: This is just an example and is completely untested. You’ll probably need to play around with it to get the results you want.
