Ignition Dataset History Retrieval from Canary

I am trying to historize datasets in Canary Historian and retrive their value which I am able to do but I can't seem to read the values in the historized dataset.I am also not able to convert it to a dataset as it is unicode.


ds1[0][1] is of type unicode per your print statement so that is why you can't directly convert that to a dataset.

To use system.dataset.toDataSet() system.dataset.toDataSet - Ignition User Manual 8.1 - Ignition Documentation you should either feed it the full PyDataSet directly like system.dataset.toDataSet(ds1) or you can manually make a dataset with a row of headers and list of lists, like system.dataset.toDataSet(['header1', 'header1', [[1,2], [3, 4]])

Worth pointing out system.tag.queryTagHistory returns a Dataset initially, your ds variable is already a basic dataset.

2 Likes

so how can I access the values in the dataset (ds1[0][1]) that is in unicode? or how to convert unicode to dataset?

You already have a dataset. The value at row 0, column 1, is the string representation of a dataset, meaning that the data in the inner dataset has already been lost. It's not immediately clear to me whether this is a thing Canary is even capable of supporting through our queryTagHistory API; every Ignition system is going to expect a tag history result to be 'scalar' in nature, even if datasets are technically capable of this nesting operation.

You will probably need a more specific Canary function/operation to retrieve the 'nested' datasets in a more purpose-built structure. I don't know whether Canary already contains such functionality or not.

Here is a script I wrote that is working with Ignition 8.1.47 and Canary 23.2.0.23331 to return API data using parameters... it gives an idea for iterating through the json result which I think would be a good alternative to the OP.

import json

# Define the URL for the GET request
url = https://<hostname>:55236/api/v2/getTagData

# Define the parameters
params = {
			"accessToken":"<>",
			"tags": "< tagPath >",
			"startTime": "Now-1000Hours",
			"endTime": "Now",
			"includeQuality": 'true',
			"maxSize": 15
}

# Create a query string from the parameters
query_string = "&".join(["{}={}".format(key, value) for key, value in params.items()])

# Complete URL with query parameters
complete_url = "{}?{}".format(url, query_string)

# Create an HTTP client with bypass_cert_validation = True
client = system.net.httpClient(bypass_cert_validation=True)

# Perform the GET request
response = client.get(complete_url)

# Check if the response is good
if response.good:
    print "GET Response Content:", response.text
    try:
        # Parse the response as JSON
        response_data = json.loads(response.text)
        print "Parsed GET Response Data:", response_data
        
        # Access specific fields if needed
        status_code = response_data.get("statusCode", "Unknown")
        errors = response_data.get("errors", [])
        data = response_data.get("data", {})
        
        # Print or process the data as necessary
        print "Status Code:", status_code
        print "Errors:", errors
        print "Data:", data
        
        # If you need to access specific tag data
        tag_data = data.get('<tagPath>', [])
        print "Tag Data:"
        for entry in tag_data:
            timestamp = entry.get('t')
            value = entry.get('v')
            quality = entry.get('q', 'Unknown')  # Assuming 'q' is the field for quality, default to 'Unknown' if missing
            print "Timestamp: {}, Value: {}, Quality: {}".format(timestamp, value, quality)
        
    except Exception as e:
        print "Failed to parse JSON response:", str(e)
else:
    print "Failed GET request. Status code:", response.statusCode
    print "Response message:", response.message
1 Like