Counting tag

I have 2 tags.
1 is a bool and one is an expression.
I want the expression tag to keep track how many times the bool “becomes true”

Counter is something like:

if(hasChanged({[.]bool}) && {[.]bool} = 1, {[.]Counter} + 1, {[.]Counter} + 0)

This does not work and I really want to be able to track this over a thousand tags and don’t see the point of using a gateway script for this.
Any suggestions?

Try modifying your expression to look like the following. This is assuming your expression tag is called Counter:

if(hasChanged({[.]bool}) && {[.]bool} = 1,coalesce({[.]Counter}+1,0),coalesce({[.]Counter},0))

The only issue I see with that solution is the tag “Counter” value does not change to zero when reset.
I have tried 2 ways:

  1. Trying to write the value to zero from the designer.
    2)system.tag.writeToTag(“Counter”,0)

It does reset thou. You can refresh the tag database to show 0 or wait for the conditions to be true and it starts at 1 again.

If that is not an issue, then take solution should work. Otherwise, the Gateway Tag Change Script will work also.

Something like this:

def Count(b,c):
	import system
	bool = system.tag.getTagValue(b)
	Count = system.tag.getTagValue(c)
	if bool == 1:
		Count += 1
	else:
		pass
	system.tag.writeToTag(c,Count)

def ResetCount(c):
	import system
	system.tag.writeToTag(c,0)

Then just add the bool tag to the Gateway Tag Change Scripts to call the script.
app.Test.Count(“bool”,“Counter”)

Hope that helps.
Chris

[quote=“dave.fogle”]Try modifying your expression to look like the following. This is assuming your expression tag is called Counter:

[code]
if(hasChanged({[.]bool}) && {[.]bool} = 1,coalesce({[.]Counter}+1,0),coalesce({[.]Counter},0))

[/code][/quote]

I fixed this in the UDT but it looks like a question marked tag in each instance, I can write to it sometimes… it seems like it doesn’t like to equal itself in the else segment…

What version are you running because I’m not having a problem doing this in a UDT?

7.5.5
It is still giving me issues.
The tag has a question mark by it.

Thoughout my tag instances I have some Counters work. some have bad quality, some unknown quality.

One thing is that expressions propagate up the worst of the qualities contained in them. So it sounds like the counter is passing along its initial unknown quality. Try this:

forceQuality(if(hasChanged({[.]bool}) && {[.]bool} = 1,coalesce({[.]Counter}+1,0),coalesce({[.]Counter},0)),192)

You could do it around each reference to Counter, but this should be sufficient.

Regards,