Problems with system.tag.queryTagHistory

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:

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

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.

The binding is a simple OPC boolean tag.

Thanks for the tip of midnight

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.

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 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])

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:

if event.propertyName == 'BitComandEv':

and after this the script runs.

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

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

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.

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.

This is probably cleaner:

if not currentValue.value:
1 Like