Tag counter

I have a digital opc tag which connect to a photo sensor, and have true value for several second and then go to false value. And I want to have a tag which count how many that digital tag has true value. What is the good solution to accomplish that?

Thank you, sorry for my bad English.

The best way would be with a transaction group as an Event Counter.

If you donā€™t have access to transaction groups (i.e you have Panel Edition), it getā€™s a little more complicated.

Iā€™ll post an example in a little bitā€¦

Actually, itā€™s going to be longer than a bit. Have a meeting to go to. :laughing:

Assumed, I have an access to Transaction group.
Ok, I will wait for the sample.

Best Regards

Update ā€¦
Actually i do not have a transaction group.
But i am really appreciate if you provide a sample for both case studies.
With and without transaction group.

Best Regards

Hello Mr. Jordan,
I am waiting for your help, because I will perform demo to potential customer about Ignition.

Best Regards

Lets assume your OPC tag is called ā€œMagic Tagā€

Create a DB Tag Called ā€œCounterā€

Create a Tag Change Gateway Event Script. You can call it whatever you want. In ā€œTag Pathsā€, enter the path to Magic Tag

For the script, enter the following

counter = system.tag.getTagValue("Counter") #Gets the current value of the counter
if newValue.value == 1: #If this event was caused because the magic tag was set true
	counter += 1 #Increment the counter value
system.tag.writeToTag("counter",counter) #write the new counter value to the "Counter" tag

Save the project and count away

2 Likes

If you want a cumulative true time,. try the following in addition to the previous code.

Create 2 more tags, "LastChangeTimestamp (DateTime) and CumulativeTrueTimeInSeconds(Int8)

counter = system.tag.getTagValue("Counter")
if newValue.getValue() == 1:
	counter += 1
	system.tag.writeToTag("LastChangeTimestamp",newValue.getTimestamp())
else:
	time = system.tag.getTagValue("CumulativeTrueTimeInMilliseconds")
	lastFallingTransition = system.tag.getTagValue("LastChangeTimestamp")
	time += newValue.getTimestamp().getTime() - lastFallingTransition.getTime()
	system.tag.writeToTag("CumulativeTrueTimeInMilliseconds",time)
system.tag.writeToTag("counter",counter)

Sorry, Yopie, Iā€™ve been away for a bit.

Kyle has is exactly right. This is the best way to go about it without a transaction group.

Using a transaction group, you can set any item in the group to be a counter by using the ā€œEvent meter optionā€


Superbb, i am just searching for tag change event.
I think that is the best way to perform counter calculation.

Iā€™ve also have try using Transaction Group.

Thank you Kyle and Jordan.
I am really appreciate it.

I have successfully create the counter, but
i have trouble displaying LastChangeTimestamp on the label control.
The label did not updated when ā€œMagic Tagā€ changed.

[quote=ā€œKyle Chaseā€]If you want a cumulative true time,. try the following in addition to the previous code.

Create 2 more tags, "LastChangeTimestamp (DateTime) and CumulativeTrueTimeInSeconds(Int8)

counter = system.tag.getTagValue("Counter") if newValue.getValue() == 1: counter += 1 system.tag.writeToTag("LastChangeTimestamp",newValue.getTimestamp()) else: time = system.tag.getTagValue("CumulativeTrueTimeInMilliseconds") lastFallingTransition = system.tag.getTagValue("LastChangeTimestamp") time += newValue.getTimestamp().getTime() - lastFallingTransition.getTime() system.tag.writeToTag("CumulativeTrueTimeInMilliseconds",time) system.tag.writeToTag("counter",counter) [/quote]

Dear Kyle,
I have successfully create the counter, but
i have trouble displaying LastChangeTimestamp on the label control.
The label did not updated when ā€œMagic Tagā€ changed.

Is there something Iā€™ve missed?

Regards

One more thing :

Iā€™ve created two counter tag, InputCounter and OutputCounter.
All the counter are updated through Gateway Script Tag Change Event.
Here are the script :

InputCounter (updated when AInput tag changed)

[color=#0000FF]counter = system.tag.getTagValue(ā€œInputCounterā€)
if newValue.value == 1:
counter += 1

system.tag.writeToTag (ā€œInputCounterā€, counter) [/color]

OutputCounter (updated when NInput tag changed)
[color=#0000FF]
counter2 = system.tag.getTagValue(ā€œOutputCounterā€)
if newValue.value == 1:
counter2 += 1

system.tag.writeToTag (ā€œOutputCounterā€, counter2)[/color]

The problem is only InputCounter are updated, the OutputCounter always have zero value although the NInput tag value changed.

Also i try to get timestamp value when AInput value changed through the following script :

[color=#0000FF]
system.tag.writeToTag (ā€˜GlassDigitalStatus\tAInputā€™, newValue.getTimestamp())[/color]

Tag ā€˜GlassDigitalStatus\tAInputā€™ has TimeStamp data type.

But, the tAInput tag always got null value.
My goal is getting time difference between NInput and AInput.

Is there any idea how to solve this?

Best Regards

My guess is there is a problem around:if newValue.value == 1: counter2 += 1I would put print statements or add an else to see what is going on:if newValue.value == 1: counter2 += 1 else: counter2 = 10You have to make sure the script is firing and the value is what you expect. If you do print to the console you can check the print out in the wrapper.log file located in:

c:\program files\inductive automation\ignition\logs