Last Time Stamp Based on Boolean

I have a Boolean tag (via OPC UA), and I am looking to capture the date and time stamp the last time the Boolean was true. Would someone please be able to help me with this?

It is easy if you capture both transitions. I would use two datetime memory tags alongside the boolean, and a gateway tag change script. Like so.

name = event.tag.name
pp = event.tagPath.parentPath
risingPath, fallingPath = [pp.getChildPath(name+x) for x in ['RisingTS', 'FallingTS']]
rising, falling = [x.value for x in system.tag.readBlocking([risingPath, fallingPath])]
# Now decide if either timestamp needs to be updated
if newValue.value:
	if not rising.after(falling):
		system.tag.writeBlocking([risingPath], [system.datetime.now()])
else:
	if not falling.after(rising):
		system.tag.writeBlocking([fallingPath], [system.datetime.now()])

Note that this script assumes the datetime memory tags have the same name as the boolean, plus “RisingTS” or “FallingTS”.

If you have many such booleans, you can create the corresponding edge timestamp tags and list all of the boolean tags in the single gateway scripts subscription list.

Edit: realized there was a pathological case if the timestamps happened to end up equal.

1 Like

Thanks for the help and quick response, pturmel.

I’m fairly new to Ignition, so I do not know what you mean about a list and its gateway scripts subscription list. Would you happen to have an example or material showing this? Edit: I have about 100 or so tags I want to do this with.

This is what I was referring to:

https://docs.inductiveautomation.com/display/DOC81/Gateway+Event+Scripts#GatewayEventScripts-TagChangeScripts

It’ll handle a hundred booleans fine, but with such a regular pattern, you might also consider a UDT that defines a value change script. As you’ll find throughout Ignition, there are often multiple workable answers.

If you haven’t yet, you should consider going through the free online lessons at Inductive University. While somewhat dry, they really are the fastest way to get up to speed on the breadth of Ignition.

Where do you want to store this captured time stamp?
If you want to to store in another tag then you just need to add a tag script / value change. Like this:
image

add this script on opcTag and make sure to indent:

if currentValue.quality.isGood():
		if currentValue.value == True:
			timestamp = currentValue.timestamp
			system.tag.writeBlocking(["[~]lastTrueTimestamp"],[timestamp])