Tag Change Event Script - find delta of weight based on tag on/off state

I have a transfer pump on/off boolean value, an active weight tag, an initial weight tag and a transferred weight tag.

I am trying to put a tag event script based on value change on the transfer pump tag.

I want to take an initial weight value when the pump turns on, take a final weight value when the pump turns off, and then take the difference between the two and write this value to a transferred weight tag.

Here is the code I have for it. It is not doing anything except clearing the transferred weight value when the transfer pump tag turns on:

def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	if currentValue.value == 1:
		initvalue=system.tag.readBlocking(["[.]Weight"])
		system.tag.writeBlocking(["[.]TransferredWeight","[.]InitialWeightOutOfDelaq"],[0,initvalue])
	if currentValue.value == 0:
		initvalue,finalvalue=system.tag.readBlocking(["[.]InitialWeightOutOfDelaq","[.]Weight"])
		deltavalue=finalvalue-initvalue
		system.tag.writeBlocking("[.]TransferredWeight", deltavalue)

Why isn't it working?

Also with tag change event script do you use the logger or can you use print statements and check the output console for feedback?

Is there a better way to do this?

I recommend you use a memory tag to hold the start weight, and an expression tag to compute current minus start. Base the rest of your logic around that. Have the event that starts the pump copy the current weight to the start weight memory tag.

Actually, I recommend you use a separate operator action to set the 'start" weight. Consider labeling that button "Tare". Probably use a separate button after the pump turns off for the operator to confirm the transferred value.

(Are you going to auto-stop the pump from Ignition? If so, why? That is a PLC task.)

So memory tag for initial weight. When transfer pump turns on read in weight and set initial weight to value read in. Have an expression tag running all the time that is constantly taking the difference between weight and initial weight. When transfer pump turns off read in expression tag and write that value to transferred weight?

Wish it was that easy. This pump is going to transfer material to a possible three different locations. Unfortunately the programmer for the plc portion isn't available and I am left with doing this on the ignition side. There is no way to tare and I can't put a button on the screen.

Oy! I hope you get to clean up when the programmer is back. But yes, copy current weight to start weight when pump turns on, then copy delta weight to transferred weight when pump turns off.

Perhaps write transferred weight and destination to a database table.

1 Like

Thanks for the solution ideas. May have to go the expression route. With a tag event script how can I write values or troubleshoot the script? I tried the print function but don't see anything on the output console.

Tag events run in the gateway. Use a logger from system.util.getLogger() to write to the gateway logs. print in gateway scope goes only to the wrapper text log file on the gateway. Handy if you have access to a terminal on the gateway.

1 Like