Tag Value (Change Event)

I can’t figure out how to make this work.

I have a tag value that I don’t want to show a negative value, example -5.96.( I only want the positive!
(Now I can correct in the PLC but I just thought until I can get to location I could fix in scada.)

I have tried adding a tag event script something like this.
if Tag(Path) < 0.00:
Tag(Path) = 0.00

I get no errors for the script but it does not work.

Can someone point me in the right direction?

1 Like

You have a combination of scripting and expression language there. You need scripting.Try something like this:

if event.getValue() < 0:
	system.tag.write(event.getTagPath().toString(), 0)

EDIT: Above was untested and isn't quite what you need; see:

Ok, I now have this and it still does not function in the Tag Change Event.

“”"
if ("[.]Flow1 Rate").value < 0.00:
system.tag.write("[.]Flow1 Rate").value, 0.00
“”"
So I’m still needing some pointers

You would need to use system.tag.read() on that first line to do it that way. But the tag value change event already includes the value accessible via event.getValue() so there’s no reason to do a tag read. Have you tried the script as I posted above?

Use the button shown in dark grey near top right of this screen clipping to properly format code (you were close!):
image

Still not working.

Use

if not initialChange and currentValue.value < 0.00:
	system.tag.write("[.]Flow1 Rate",0.00)
1 Like

Also make sure you actually change the tags value to trigger the event, for example change it from -4.5 to -5.5

Yes. And if it's writing back to itself, you don't need to include a tag path, just use the tagPath included in the event like this:

if currentValue.value < 0:
	system.tag.write(tagPath, 0)

if not initialChange makes sense for most things, but it you never want this value to be zero there's no reason to exclude initialChange.

Just another perpsective.

If face with this problem, what I would do is I would rather create another tag.
Then, in that new tag just write a simple expression max(0, old_tag).

Something along that line.

Thanks to everyone for all the help, examples, Ideas.

I never could it to work like I was hoping for, so I made a trip to location and fixed the code in the PLC, I should have done this to start.

Thanks again

Hi!

If you’re using Ignition 8.0 you can do a transform with Expression like:

abs({value})

https://docs.inductiveautomation.com/display/DOC80/Expression+Transform

https://docs.inductiveautomation.com/display/DOC80/abs

The same expression function exists in 7.9 but you would implement differently, if I recall correctly.

You could also enable linear scaling on the tag to use the ‘Clamp Mode’ property. Set linear scaling with Raw = 0-100, Scaled = 0-100, and Clamp Mode = ‘Clamp_Low’. It doesn’t really matter what the high raw/scale values are if you don’t enable high clamping.

1 Like

That's because you were writing it back to itself. Every time the OPC driver supplied a new value, that would be propagated to subscribers, followed by the zero from the script. You would have to make a derived tag to hold the clamped value. The same race can occur in the PLC, too, if you are trying to overwrite the negatives in place. An OPC driver could read the negative just before the ladder replaced it. (Smaller race window in the PLC, though.)