# Problems with system.tag.queryTagHistory

**URL:** <https://forum.inductiveautomation.com/t/problems-with-system-tag-querytaghistory/105866>\
**Category:** Ignition\
**Tags:** ignition81, scripting\
**Created:** [May 5, 2025, 7:27am UTC](https://forum.inductiveautomation.com/t/problems-with-system-tag-querytaghistory/105866 "2025-05-05T07:27:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Filarm27](https://avatars.discourse-cdn.com/v4/letter/f/b9bd4f/32.png) [@Filarm27](https://forum.inductiveautomation.com/u/Filarm27)\
**Post date:** [May 5, 2025, 7:27am UTC](https://forum.inductiveautomation.com/t/problems-with-system-tag-querytaghistory/105866/1 "2025-05-05T07:27:20Z")

</div>

Hi everyone,

I'm trying to implement a count of the number of times a valve has opened since midnight of the current day via script using system.tag.queryTagHistory.

I have a tag boolean that indicates the valve's open status, and I have enabled history for this tag.

Currently, I'm attempting to use the script triggered by a property change on a numeric label.

This is the script I'm using:

```python
if event.propertyName == 'BitComandEv': #This is the custom property of the bit valve open

	valve = "tag path of the bit valve open"
	
	actual_hour = system.date.now() 
	
	midnight_current_day = system.date.setTime(actual_hour , 0, 0, 0)
	
	
	result = system.tag.queryTagHistory(
	    paths=[valve],
	    startDate=midnight_current_day ,
	    endDate=actual_hour ,
	    returnSize=1,
	    aggregationMode="CountOn")
	
	numbers_of_opening = result.getValueAt(0, 1)
	
	system.tag.writeBlocking("Tag path archive", numbers_of_opening )

```

I am currently experiencing two issues:

1. The count value displayed on the numeric label only updates when I open the window containing it. It doesn't seem to update afterwards as new valve openings occur.

2. This script seems to be generating many entries in the `AUDIT_EVENTS` table for the `output_count_tag_path` tag write. This suggests the script might be running repeatedly (triggered frequently by changes to `BitComandEv`?).

Thanks to everyone in advance

---

<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:** [May 5, 2025, 7:55am UTC](https://forum.inductiveautomation.com/t/problems-with-system-tag-querytaghistory/105866/2 "2025-05-05T07:55:54Z")

</div>

Can you show the binding on `BitCommandEv`? Q1 is typical of a view's parameter evaluation which only occurs once - but I'm not sure that's your problem.

I can't think what's causing the events in Q2.

Tip: [system.date.midnight | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-date/system-date-midnight).

---

<div class="post-metadata">

**Author:** ![Filarm27](https://avatars.discourse-cdn.com/v4/letter/f/b9bd4f/32.png) [@Filarm27](https://forum.inductiveautomation.com/u/Filarm27)\
**Post date:** [May 5, 2025, 8:29am UTC](https://forum.inductiveautomation.com/t/problems-with-system-tag-querytaghistory/105866/3 "2025-05-05T08:29:53Z")

</div>

The binding is a simple OPC boolean tag.

Thanks for the tip of midnight

---

<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:** [May 5, 2025, 8:53am UTC](https://forum.inductiveautomation.com/t/problems-with-system-tag-querytaghistory/105866/4 "2025-05-05T08:53:48Z")

</div>

One thing that might trigger the multiple events is a change in the boolean tag's qualified value. This means that a change in the timestamp or quality as well as changes in the value would trigger the event. You could try logging the tag in your script to check. [Scripting Object Reference | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/reference-pages/scripting-object-reference#qualifiedvalue).

If that's the problem then you could change your tag binding from `[default]myBool` to `[default]myBool.value` (rather than doing it in the script). This would avoid unnecessary script execution.

Tip 2: [system.tag.writeBlocking | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-tag/system-tag-writeBlocking) is expecting lists in its parameters. For single tags it supports single values for legacy reasons but I wouldn't rely on it. Change your script to,  
`system.tag.writeBlocking(["Tag path archive"], [numbers_of_opening])`

---

<div class="post-metadata">

**Author:** ![Filarm27](https://avatars.discourse-cdn.com/v4/letter/f/b9bd4f/32.png) [@Filarm27](https://forum.inductiveautomation.com/u/Filarm27)\
**Post date:** [May 5, 2025, 9:14am UTC](https://forum.inductiveautomation.com/t/problems-with-system-tag-querytaghistory/105866/5 "2025-05-05T09:14:01Z")

</div>

I try but nothing change.

Can i change how the script is run? Could this be the problem? Currently it is run with the property change:

```python
if event.propertyName == 'BitComandEv':

```

and after this the script runs.

But only if I enter the page where the numeric text filed is.

---

<div class="post-metadata">

**Author:** ![Filarm27](https://avatars.discourse-cdn.com/v4/letter/f/b9bd4f/32.png) [@Filarm27](https://forum.inductiveautomation.com/u/Filarm27)\
**Post date:** [May 7, 2025, 6:38am UTC](https://forum.inductiveautomation.com/t/problems-with-system-tag-querytaghistory/105866/6 "2025-05-07T06:38:24Z")

</div>

I solved the problem by putting the script directly in the tag of the bit valve open on "value changed" using:

```python
if currentValue.value == False:

```

And then the script I texted before.

Doing this I no longer have logs in the AUDIT\_EVENTS table and the opening counts of the valve are correct.

---

<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:** [May 7, 2025, 7:40am UTC](https://forum.inductiveautomation.com/t/problems-with-system-tag-querytaghistory/105866/7 "2025-05-07T07:40:44Z")

</div>

> [@Filarm27](#):
>
> I solved the problem by putting the script directly in the tag ...von "value changed"

Your script has `system.tag.writeBlocking()` in it so this doesn't look like a good idea. You should move the script into a gateway script and call that from the tag change event. The advice here is always to keep tag change event scripts down in the very low millisecond range.

> [@Filarm27](#):
>
> `if currentValue.value == False:`

This is probably cleaner:

```python
if not currentValue.value:

```
