Best Way to Script from Tag History NOT using Vision

Hi everyone,

I would like to be able to record the most recent 12 values of a tag. There is a script that puts a value in this tag every hour. I know Ignition has tag history, but it doesn't lend itself well to scripting. My idea is to instead write to and fill a float array memory tag as apart of a tag value change script on the first tag im filling. I would then like to have a third expression tag that averages the values in the floating array.

A few questions:

  • Is there a better way to do this?
  • How would I be able to replace the oldest value in the array when it gets the 13th value?
  • Is there a better spot to put it than as a tag value change event script?

Any help would be appreciated, thanks!

I am not sure what you mean it doesn't lend itself to scripting.

system.tag.queryTagHistory | Ignition User Manual (inductiveautomation.com)

Use a dataset memory tag, once the dataset is full, remove the top row, and append the newest row.

You can use system.dataset.addRow(), system.dataset.deleteRow().

Yes, this should be in a Gateway Tag Change event.

1 Like

Thanks!

I've done something similar to what you're describing. I have a "running" tag that is monitored. When the value changes to 1 I log the current timestamp into an array of the last 5 start times. I then use these start times and a pre-defined start duration to show the voltage and current during the last 5 startups.

However, you do not need something as complex for what you desire.
It may be much easier and require almost no scripting.
First, I must know, what is your purpose of having this data?
Are you just going to display it? Are you going to do something else with it?

Want to be able to average the last twelve data points and store that value in a tag.

What are you going to do with that tag then?

Going to display it on a table

You can probably do something like this in a named query and skip the array of the last 12 values entirely.

SELECT AVG(Q.ColA)
FROM (
      SELECT TOP 12 ColA
      FROM Table_Name
      ORDER BY SomeColumn
     ) Q

Use the database query browser to help you define your table and column.
Then, call this in an expression using runScript when your hourly value updates

Cool, what would my table name be then? Is this pulling from a dataset tag or the tag historian?

tag historian. Open the database query browser from the tools menu and select the tag history table. You can double-click the table to have it generate a select statement and you can modify it from there. I don't have a gateway handy with the historian enabled so I don't have any exact syntax to give you.

So, I guess that this is also assuming you have the tag historian module.

If you don't have that module you would have to go the "manual" array route.

Got it, thanks man, I appreciate it!

Can I do this from the tag history internal historian? That is where I can the tag history going as of now. Or do I need another type of historian/history provider? I dont see the tag history table as of now with it.

Use the toolbar at the top of the designer and select Tools>Database Query Browser

Decided to use this,

Coded not working as I intended, its not filling the database with the actual values. it did send the dataset though and I can see the column headers. Any ideas?

	endTime = system.date.now()
	startTime = system.date.addHours(endTime,-13)
	dataSet = system.tag.queryTagHistory(paths=["[.]Current Total Mass Balance"], startDate=startTime, endDate=endTime, noInterpolation=True, ignoreBadQuality=True)
	system.tag.write("[.]Mass Balance History Storage",dataSet)

Just want to say that this is not great advice, particularly if the historian tables are being partitioned. You should only use the provided methods for accessing the Tag Historian tables.

Depends? Where is this code located?

In a tag event script.


Okay,

First of all, don't do this there. There are reasons why which I won't get into, but Tag Value Change scripts should execute in single digit milliseconds. Which means you should never use any function which touches a database. This script should be located in a Gateway Tag Change script, which should probably be a single line into a Project Script Library script.

My first guess is that either, you don't have data available from the time range you are querying, or the tag path isn't being correctly dereferenced to the historical tag path.

Does the Mass Balance History Storage tag have it's history enabled? Is the provider set?

Considering that Joe only wants to go back 12 hours I don't think the partitions would be an issue.

In any case, what would be the "official" recommendation for querying data from the tag historian for use in a tag?

Well, depends on what you want to do with the data. In this case, since it seems that the end goal is to get an average, I would probably recommend system.tag.queryTagCalculations().

A tag history binding on the table might also be acceptable.

For reference:

It isn't just 12 hours once though, its the last 12 hours. Which to me means that it is rolling, and so unless partitioning is off completely, at some point the query will break.

2 Likes