# Best Way to Script from Tag History NOT using Vision

**URL:** <https://forum.inductiveautomation.com/t/best-way-to-script-from-tag-history-not-using-vision/97095>\
**Category:** Ignition\
**Tags:** vision, scripting\
**Created:** [October 10, 2024, 5:01pm UTC](https://forum.inductiveautomation.com/t/best-way-to-script-from-tag-history-not-using-vision/97095 "2024-10-10T17:01:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe\_Alcide](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@Joe\_Alcide](https://forum.inductiveautomation.com/u/Joe_Alcide)\
**Post date:** [October 10, 2024, 5:01pm UTC](https://forum.inductiveautomation.com/t/best-way-to-script-from-tag-history-not-using-vision/97095/1 "2024-10-10T17:01:25Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Benjamin\_Furlani](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/benjamin_furlani/32/66084_2.png) [@Benjamin\_Furlani](https://forum.inductiveautomation.com/u/Benjamin_Furlani)\
**Post date:** [October 10, 2024, 5:27pm UTC](https://forum.inductiveautomation.com/t/best-way-to-script-from-tag-history-not-using-vision/97095/2 "2024-10-10T17:27:33Z")

</div>

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

[system.tag.queryTagHistory | Ignition User Manual (inductiveautomation.com)](https://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:** [October 10, 2024, 5:30pm UTC](https://forum.inductiveautomation.com/t/best-way-to-script-from-tag-history-not-using-vision/97095/3 "2024-10-10T17:30:37Z")

</div>

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()`.

> [@Joe\_Alcide](#):
>
> Is there a better spot to put it than as a tag value change event script?

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

---

<div class="post-metadata">

**Author:** ![Joe\_Alcide](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@Joe\_Alcide](https://forum.inductiveautomation.com/u/Joe_Alcide)\
**Post date:** [October 10, 2024, 5:47pm UTC](https://forum.inductiveautomation.com/t/best-way-to-script-from-tag-history-not-using-vision/97095/4 "2024-10-10T17:47:25Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [October 10, 2024, 5:52pm UTC](https://forum.inductiveautomation.com/t/best-way-to-script-from-tag-history-not-using-vision/97095/5 "2024-10-10T17:52:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Joe\_Alcide](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@Joe\_Alcide](https://forum.inductiveautomation.com/u/Joe_Alcide)\
**Post date:** [October 10, 2024, 5:53pm UTC](https://forum.inductiveautomation.com/t/best-way-to-script-from-tag-history-not-using-vision/97095/6 "2024-10-10T17:53:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [October 10, 2024, 5:53pm UTC](https://forum.inductiveautomation.com/t/best-way-to-script-from-tag-history-not-using-vision/97095/7 "2024-10-10T17:53:32Z")

</div>

What are you going to do with that tag then?

---

<div class="post-metadata">

**Author:** ![Joe\_Alcide](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@Joe\_Alcide](https://forum.inductiveautomation.com/u/Joe_Alcide)\
**Post date:** [October 10, 2024, 5:54pm UTC](https://forum.inductiveautomation.com/t/best-way-to-script-from-tag-history-not-using-vision/97095/8 "2024-10-10T17:54:12Z")

</div>

Going to display it on a table

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [October 10, 2024, 5:57pm UTC](https://forum.inductiveautomation.com/t/best-way-to-script-from-tag-history-not-using-vision/97095/9 "2024-10-10T17:57:01Z")

</div>

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

```python
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

---

<div class="post-metadata">

**Author:** ![Joe\_Alcide](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@Joe\_Alcide](https://forum.inductiveautomation.com/u/Joe_Alcide)\
**Post date:** [October 10, 2024, 6:01pm UTC](https://forum.inductiveautomation.com/t/best-way-to-script-from-tag-history-not-using-vision/97095/10 "2024-10-10T18:01:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [October 10, 2024, 6:03pm UTC](https://forum.inductiveautomation.com/t/best-way-to-script-from-tag-history-not-using-vision/97095/11 "2024-10-10T18:03:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Joe\_Alcide](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@Joe\_Alcide](https://forum.inductiveautomation.com/u/Joe_Alcide)\
**Post date:** [October 10, 2024, 6:06pm UTC](https://forum.inductiveautomation.com/t/best-way-to-script-from-tag-history-not-using-vision/97095/12 "2024-10-10T18:06:21Z")

</div>

Got it, thanks man, I appreciate it!

---

<div class="post-metadata">

**Author:** ![Joe\_Alcide](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@Joe\_Alcide](https://forum.inductiveautomation.com/u/Joe_Alcide)\
**Post date:** [October 10, 2024, 6:11pm UTC](https://forum.inductiveautomation.com/t/best-way-to-script-from-tag-history-not-using-vision/97095/13 "2024-10-10T18:11:22Z")

</div>

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.

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

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [October 10, 2024, 6:30pm UTC](https://forum.inductiveautomation.com/t/best-way-to-script-from-tag-history-not-using-vision/97095/14 "2024-10-10T18:30:06Z")

</div>

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

> **[Database Query Browser | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/platform/designer/designer-tools/database-query-browser)**
>
> The Database Query Browser Panel

---

<div class="post-metadata">

**Author:** ![Joe\_Alcide](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@Joe\_Alcide](https://forum.inductiveautomation.com/u/Joe_Alcide)\
**Post date:** [October 10, 2024, 6:39pm UTC](https://forum.inductiveautomation.com/t/best-way-to-script-from-tag-history-not-using-vision/97095/15 "2024-10-10T18:39:29Z")

</div>

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?

```python
	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)
```

---

<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:** [October 10, 2024, 6:42pm UTC](https://forum.inductiveautomation.com/t/best-way-to-script-from-tag-history-not-using-vision/97095/16 "2024-10-10T18:42:29Z")

</div>

> [@amarks](#):
>
> tag historian. Open the database query browser from the tools menu and select the tag history table

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.

> [@Joe\_Alcide](#):
>
> I can see the column headers. Any ideas?

Depends? Where is this code located?

---

<div class="post-metadata">

**Author:** ![Joe\_Alcide](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@Joe\_Alcide](https://forum.inductiveautomation.com/u/Joe_Alcide)\
**Post date:** [October 10, 2024, 6:44pm UTC](https://forum.inductiveautomation.com/t/best-way-to-script-from-tag-history-not-using-vision/97095/17 "2024-10-10T18:44:09Z")

</div>

In a tag event script.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/b/4bbf5a1680d93079fa03822df5a54e2d4b504e29.png)  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/3/a3b103afad45318281b038b6e442b67d2df139c6.png)

---

<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:** [October 10, 2024, 6:49pm UTC](https://forum.inductiveautomation.com/t/best-way-to-script-from-tag-history-not-using-vision/97095/18 "2024-10-10T18:49:38Z")

</div>

> [@Joe\_Alcide](#):
>
> 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?

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [October 10, 2024, 6:56pm UTC](https://forum.inductiveautomation.com/t/best-way-to-script-from-tag-history-not-using-vision/97095/19 "2024-10-10T18:56:52Z")

</div>

> [@lrose](#):
>
> > [@amarks](#):
> >
> > tag historian. Open the database query browser from the tools menu and select the tag history table
> 
> Just want to say that this is not great advice, particularly if the historian tables are being partitioned.

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?

---

<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:** [October 10, 2024, 7:13pm UTC](https://forum.inductiveautomation.com/t/best-way-to-script-from-tag-history-not-using-vision/97095/20 "2024-10-10T19:13:38Z")

</div>

> [@amarks](#):
>
> 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:

> [@History tag - writing data to wrong table](https://forum.inductiveautomation.com/t/history-tag-writing-data-to-wrong-table/97075/2):
>
> The only supported way to query data out of the historian is via Ignition, and we will automatically bridge across as many tables as needed.

> [@amarks](#):
>
> Considering that Joe only wants to go back 12 hours

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.

[Next page](https://forum.inductiveautomation.com/t/best-way-to-script-from-tag-history-not-using-vision/97095.md?page=2)
