Boolean True Counter

I have a Boolean tag CH1_Fault_Boolean and I would like to count the amount of time this tag is true in a day. I have this code that works fine, but after using it for a while it shows an error but still keeps counting which is a bit weird but the value is red. Also, I needed help with how to set it to 0. I don't know if you can write to an expression tag.


Yeah, i don't believe that expression tags can be written to because their value should be coming from the expression and the expression has no place to write to, unlike a memory tag or an OPC tag.
Instead of a expression, i would use a tag change script. Change your counter value source to memory and let the script do the job along with a daily scheduled script to reset it.

1 Like

It'll look something like this:

if newValue.getValue():
	tagPath = "[ProvedorTst]Faults/" + event.getTagPath().getItemName().replace("Boolean","Count")
	newCount = system.tag.readBlocking([tagPath])[0].value +1
	system.tag.writeBlocking([tagPath], [newCount])

I would add a couple custom props to the tag, or make a UDT.

Custom props would be something like timeHI and timeTRIG. Use a value change script. When the boolean tag changes state, if it's changed to 1 set the timeTRIG value to system.date.now(). If it's changed to 0 add the difference between system.date.now() and timeTRIG to timeHI. Repeat. (use memory tags instead of props for a UDT)

1 Like

For the tagPath would I also enter the "Provedor" I believe mine says [default]? What tag are we exactly referring to?
tagPath = "[ProvedorTst]Faults/"

UDT looks like this:

Tag is your BOOL
timeHI is a long (millis)
timeTRIG is a long (millis)

1 Like


This sis what I have so far/ I seem to get an error trying to use newValue

We're referring to the folder where the tags are located but it looks like this because i've used my tag provider that i named ProvedorTst.

Here under Tag Paths is where you'll place the tag path to the tag(or tags) you want to monitor, just right click them, select "Copy Path" then paste here.
image
As for the script, it's not pretty but you can use this so it doesn't matter the folder where they're located:

if newValue.getValue():
	tagPath = str(event.getTagPath().getParentPath()) + '/' + event.getTagPath().getItemName().replace("Boolean","Count")
	newCount = system.tag.readBlocking([tagPath])[0].value +1
	system.tag.writeBlocking([tagPath], [newCount])

You're getting an error because you're using a tag change script instead of a Gateway tag change script. Just replace your scipt with this and it should work if you just want to count:

	if currentValue.value:
		newTagPath = tagPath.replace('Boolean','Count')
		newCount = system.tag.readBlocking([newTagPath])[0].value + 1
		system.tag.writeBlocking([tagPaths], [newCount])

I thought your answer looked strange at first but just moved on to focus on my reply but you're the one on the right path. Reading the original expression i got the impression that the objective was to count how many times the tag when true instead of:

If you have the tag history module, consider using tag history.