# Error Power chart

**URL:** <https://forum.inductiveautomation.com/t/error-power-chart/66795>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [November 22, 2022, 8:04am UTC](https://forum.inductiveautomation.com/t/error-power-chart/66795 "2022-11-22T08:04:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sebastien\_ng](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sebastien_ng/32/55297_2.png) [@sebastien\_ng](https://forum.inductiveautomation.com/u/sebastien_ng)\
**Post date:** [November 22, 2022, 8:04am UTC](https://forum.inductiveautomation.com/t/error-power-chart/66795/1 "2022-11-22T08:04:11Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/a/eaa40d3cc86201bb8f26fafa6c310afd5d6668c8.png)

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/3/63b10b95048b7a43e87fa552f9765d65426ff6ff.png)

Here is the error that is displayed

```auto
Error_ScriptEval
Property
root/Chart.series[0].data
Description
Traceback (most recent call last): File "<transform>", line 24, in transform at java.base/jdk.internal.util.Preconditions.outOfBounds(Unknown Source) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Unknown Source) at java.base/jdk.internal.util.Preconditions.checkIndex(Unknown Source) at java.base/java.util.Objects.checkIndex(Unknown Source) at java.base/java.util.ArrayList.get(Unknown Source) at com.inductiveautomation.ignition.gateway.datasource.BasicStreamingDataset.getValueAt(BasicStreamingDataset.java:161) at jdk.internal.reflect.GeneratedMethodAccessor54.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.base/java.lang.reflect.Method.invoke(Unknown Source) java.lang.IndexOutOfBoundsException: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0

```

Voici le code

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/8/680c14a77c4d05315a6d5981bd6aae34262b6803.png)

```auto
	# If timeSelection is 0, set minutes equal to minutes param.
	if value['timeSelection'] == 0:
		minutes = value['minutes']
	else:
		# Otherwise, get the amount of minutes based on the
		# timeSelection. 1M=1min,1H=60min,1D=1440min,1W=10080min.
		minuteMap = {1:1,2:60,3:1440,4:10080}
		minutes = minuteMap[value['timeSelection']]
	
	# Query the historical data using now() as the endTime and
	# 0 - minutes (figured out minutes above) as the startTime,
	# and then put the data in a format expected by the chart
	# and return it.
	data = []
	if value['tag'] != '':
		path = value['tag']
		system.perspective.print('querying data for: ' + str(path))
		endTime = system.date.now()
		startTime = system.date.addMinutes(endTime,(0-minutes))
		history = system.tag.queryTagHistory(paths=[path],startDate=startTime,endDate=endTime,aggregationMode='LastValue',returnSize=300,noInterpolation=0)
		
		#return history.getValueAt(0,1)
		if history.getValueAt(0,1) == None:
			return data
		else:
			for x in range(0,history.getRowCount()):
				data.append({'time':system.date.toMillis(history.getValueAt(x,0)),'value':history.getValueAt(x,1)})
	
	return data

```

I would like this error not to appear when there is no historical data recorded in the database

---

<div class="post-metadata">

**Author:** ![sebastien\_ng](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sebastien_ng/32/55297_2.png) [@sebastien\_ng](https://forum.inductiveautomation.com/u/sebastien_ng)\
**Post date:** [November 22, 2022, 8:16am UTC](https://forum.inductiveautomation.com/t/error-power-chart/66795/2 "2022-11-22T08:16:47Z")

</div>

For exemple if I want show data in most last 30 min and if I haven't stored data in this time, this power chart show me error.

How can I avoiding this Error

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [November 22, 2022, 9:18am UTC](https://forum.inductiveautomation.com/t/error-power-chart/66795/3 "2022-11-22T09:18:34Z")

</div>

> [@sebastien\_ng](#):
>
> I would like this error not to appear when there is no historical data recorded in the database

Then you'll have to check for the dataset length before trying to reference values from it.  
this:

> [@sebastien\_ng](#):
>
> `history.getValueAt(0,1)`

Is trying to get something from an empty dataset.

While I'm at it, there are a few things you can change in your script:

- you don't need to compute a `startDate` if the `endDate` is now. You can use `rangeMinutes` instead.
- you can include `value[minutes]` in your `minuteMap` to make things a bit simpler.

```python

# no path provided, return empty list
if value['tag'] == '':
	return []

minuteMap = [value['minutes'], 1, 60, 1440, 10080]
minutes = minuteMap[value['timeSelection']]

history = system.tag.queryTagHistory(
	paths=[value['tag']],
	rangeMinutes=minutes,
	aggregationMode='LastValue',
	returnSize=300,
	noInterpolation=0
)

return [
	{
		'time': system.date.toMillis(history.getValueAt(x, 0)),
		'value': history.getValueAt(x, 1)
	} for x in xrange(history.getRowCount())
]

```
