# Export PowerChart to CSV with 30 min increments

**URL:** <https://forum.inductiveautomation.com/t/export-powerchart-to-csv-with-30-min-increments/99365>\
**Category:** Ignition\
**Tags:** vision, perspective\
**Created:** [December 3, 2024, 3:37pm UTC](https://forum.inductiveautomation.com/t/export-powerchart-to-csv-with-30-min-increments/99365 "2024-12-03T15:37:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Christ](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@Andrew\_Christ](https://forum.inductiveautomation.com/u/Andrew_Christ)\
**Post date:** [December 3, 2024, 3:37pm UTC](https://forum.inductiveautomation.com/t/export-powerchart-to-csv-with-30-min-increments/99365/1 "2024-12-03T15:37:53Z")

</div>

I'm trying to export 2 tags in a Power chart over the course of a year. Specifically, 7/1/2023-7/1/2024. I tried using the export option of the Power Chart, but I can't edit the specific time interval of each data point. It just takes 2 random times each day. I need it to have a data point every 30 mins for every day (ex. 10 am, 10:30am, 11am etc).

I found this code to create a button that will do what I want (I think) but it's written in Vision and I can't seem to get it to work in Perspective. I keep getting errors like Error running action 'dom.onClick' on AdHocTrends/AdHocTrends@D/root/Button: Traceback (most recent call last): File "function:runAction", line 2, in runAction AttributeError: 'com.inductiveautomation.ignition.common.script.ada' object has no attribute 'source'

This is the code I'm using.

```python

 def runAction(self, event):
	data = system.tag.queryTagHistory(
	# you can have more than one tag path in the list. They'll show up as separate columns
	['tagPath'],
	startDate=event.source.parent.getComponent('PowerChart').startDate, # replace
	endDate=event.source.parent.getComponent('PowerChart').endDate, #replace
	aggregationMode='Average', #read about various aggregation modes. Pick the one you like
	columnNames=['tagValue'],
	intervalMinutes=30, # only available in 8.1.0 or later
	noInterpolation=0 # Investigate whether you want this on or off
)
#this line is for testing. You'd actually want to do something with the dataset.

system.dataset.toExcel 
system.perspective.download

```

---

<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:** [December 3, 2024, 3:46pm UTC](https://forum.inductiveautomation.com/t/export-powerchart-to-csv-with-30-min-increments/99365/2 "2024-12-03T15:46:07Z")

</div>

Welcome to the forum.

Please see [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831). Then hit the edit icon below your post to fix the code. You've lost all the Python indentation.

---

<div class="post-metadata">

**Author:** ![zacht](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@zacht](https://forum.inductiveautomation.com/u/zacht)\
**Post date:** [December 3, 2024, 5:13pm UTC](https://forum.inductiveautomation.com/t/export-powerchart-to-csv-with-30-min-increments/99365/3 "2024-12-03T17:13:33Z")

</div>

Several issues that I can see:

1. Indentation is wrong at the beginning. There's an extra space before def.
  - This is probably a relic of copy-paste and not actually a problem in your script.

2. `event.source.parent.getComponent(...)` is a vision construction that doesn't have a meaning in perspective. Specifically, the `event` `object has no attribute 'source'`, which is what your error message is telling you.
  - The exact analog would be something like `self.getSibling("PowerChart").props.config.startDate`, but you can use the property selector to make things easier.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/f/2feb20db32863ebc112eb193f3b85be0075e3de3.png)

3. the function calls at the bottom need parenthesis and arguments. Here are links to the documentation for reference.
  - [system.dataset.toExcel](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-dataset/system-dataset-toExcel)
  - [system.perspective.download](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-perspective/system-perspective-download)

4. ~~The runAction function doesn't return anything.~~
5. ~~You never actually call the runAction function. You need to call the function and use the results.~~ (Corrected by @lrose. The extra-wrong indentation confused me.)

Note: `system.tag.queryTagHistory` with the arguments in your script isn't going to get you the value of the tag at 30 minute increments as you've described. `aggregationMode='Average'` means this query "will return the time-weighted average value of all samples in that time slice." (quote from Ignition docs ([system.tag.queryTagHistory](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-tag/system-tag-queryTagHistory))).

---

<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:** [December 3, 2024, 6:46pm UTC](https://forum.inductiveautomation.com/t/export-powerchart-to-csv-with-30-min-increments/99365/4 "2024-12-03T18:46:59Z")

</div>

> [@zacht](#):
>
> You never actually call the runAction function. You need to call the function and use the results.

You can not call the 'runAction' function, it is called for you when you click the button. The indentation in the script is not correct as the functions at the end should be part of the runAction script.

@Andrew_Christ

The script should look something like this:

```python
def runAction(self,event):
    data = system.tag.queryTagHistory(
        ['tagPath'],
        startDate = self.getSibling('PowerChart').props.config.startDate,
        endDate = self.getSibling('PowerChart').props.config.endDate,
        aggregationMode = 'LastValue',
        columnNames = ['tagValues'],
        intervalMinutes = 30,
        noInterpolation = True
        )
    excelBytes = system.dataset.toExcel(False,data)
    system.perspective.download("Suggested_File_Name",excelBytes)

```

---

<div class="post-metadata">

**Author:** ![Andrew\_Christ](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@Andrew\_Christ](https://forum.inductiveautomation.com/u/Andrew_Christ)\
**Post date:** [December 5, 2024, 1:45pm UTC](https://forum.inductiveautomation.com/t/export-powerchart-to-csv-with-30-min-increments/99365/5 "2024-12-05T13:45:18Z")

</div>

> [@lrose](#):
>
> ```python
> def runAction(self,event):
> data = system.tag.queryTagHistory(
> ['tagPath'],
> startDate = self.getSibling('PowerChart').props.config.startDate,
> endDate = self.getSibling('PowerChart').props.config.endDate,
> aggregationMode = 'LastValue',
> columnNames = ['tagValues'],
> intervalMinutes = 30,
> noInterpolation = True
> )
> excelBytes = system.dataset.toExcel(False,data)
> system.perspective.download("Suggested_File_Name",excelBytes)
> 
> ```

That is close to what I need, only problem is it downloaded as a .txt file with a bunch of gibberish inside of it. I attached it for reference.  
[AdHocTrends.csv.txt](https://forum.inductiveautomation.com/uploads/short-url/edA9QYFHxGVkwAbz3Ey3hGoIf67.txt) (193.9 KB)

---

<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:** [December 5, 2024, 2:25pm UTC](https://forum.inductiveautomation.com/t/export-powerchart-to-csv-with-30-min-increments/99365/6 "2024-12-05T14:25:48Z")

</div>

> [@Andrew\_Christ](#):
>
> downloaded as a .txt file with a bunch of gibberish

The client needs to save the file as an Excel File. The `toExcel()` function returns a `ByteArray` of the data from an Excel file. If you want a CSV instead, then you need to use the `system.dataset.toCSV()` function instead.

---

<div class="post-metadata">

**Author:** ![Andrew\_Christ](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@Andrew\_Christ](https://forum.inductiveautomation.com/u/Andrew_Christ)\
**Post date:** [December 5, 2024, 3:53pm UTC](https://forum.inductiveautomation.com/t/export-powerchart-to-csv-with-30-min-increments/99365/7 "2024-12-05T15:53:17Z")

</div>

I got it to download and open as a .csv. The timestamps are correct but there aren't any tag values appearing.  
This is the Excel file and the code I'm using.  
[Test (1).csv](https://forum.inductiveautomation.com/uploads/short-url/XUHdooa2BSPPDUCBU7RVyor0X0.csv) (464.5 KB)

```python
def runAction(self, event):

    data = system.tag.queryTagHistory(
        ['tagPath'],
        startDate = self.getSibling('PowerChart').props.config.startDate,
        endDate = self.getSibling('PowerChart').props.config.endDate,
        aggregationMode = 'LastValue',
        columnNames = ['tagValues'],
        intervalMinutes = 30,
        noInterpolation = True
        )
    excelBytes = system.dataset.toCSV(data, forExport = False)
    system.perspective.download("Test.csv",excelBytes)

```

---

<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:** [December 5, 2024, 4:31pm UTC](https://forum.inductiveautomation.com/t/export-powerchart-to-csv-with-30-min-increments/99365/8 "2024-12-05T16:31:32Z")

</div>

Do you have a tag named `TagPath `? Is it being historized?

I wrote the original snippet with place holders because I do not know your actual paths.
