Export PowerChart to CSV with 30 min increments

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.


 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

Welcome to the forum.

Please see Wiki - how to post code on this forum. Then hit the edit icon below your post to fix the code. You've lost all the Python indentation.

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.
  3. the function calls at the bottom need parenthesis and arguments. Here are links to the documentation for reference.
  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)).

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:

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)
1 Like

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 (193.9 KB)

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.

1 Like

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 (464.5 KB)

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)

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.