Changing a Tag Value using an onClick Mouse Event

Hello,

I am trying to develop some templates to use on views while developing in Perspective. Last time I have used Ignition was 7.9 about 4 years ago. So trying to use some knowledge that I have retained from there to use here.

Anyhow, I am trying to script an event when the background of template is pressed. I am unsure what iteration I am on with trying to get this to work but this is what I have:

path = self.view.params.ElementSelected
value = [self.view.params.ElementSelected]
	
if value == 0:
	system.tag.write(path,1)
	
else:
	system.tag.write(path,0)

Using that script gives me no errors, but it doesn't write to the parameter on the view. I was getting some errors before, but this has seemed to have cleared them up, but the script doesn't do what I want it to do. I really don't remember having to script something like this, but it was a built in function.

In short, if the background is pressed, I want to change the state of the tag from a 1 to a 0 or a 0 to a 1.

Is it a boolean tag, or an integer tag?

# boolean
system.tag.writeBlocking([path], not system.tag.readBlocking([path]).value)

# integer
original = system.tag.readBlocking([path]).value  # 0 or 1
system.tag.writeBlocking([path], abs(original - 1))  # abs(0-1) == abs(-1) == 1, or abs(1-1) == abs(0) == 0

The script as you've provided it would always execute the else block, because you have set value to be a list which contains what I'm assuming is a string, and so value will never equate to 0.

path = self.view.params.ElementSelected
value = [self.view.params.ElementSelected]  # value is now a list which contains a string
	
if value == 0:  # so value will never be 0
	system.tag.write(path,1)
	
else:
	system.tag.write(path,0)
1 Like

Thanks for the response.

The tag I am ultimately writing to is an integer. Person whom I learned Ignition from was adamite to not use Boolean tag types in Ignition, even if the tag in the PLC was a Boolean. From the template view, it is going to a value parameter, then once on the main view, it is bound to the tag. Forcing the parameter value does change the tag value in the PLC when I manually change the number, so I would think the binding is fine and working.

So anyhow, this is the code

path = self.view.params.ElementSelected

original = self.tag.readBlocking([path])
system.tag.writeBlocking([path], abs(original - 1))

This is the error I get.

com.inductiveautomation.ignition.common.script.JythonExecException
Traceback (most recent call last):
File "function:runAction", line 4, in runAction
AttributeError: 'com.inductiveautomation.perspective.gateway.script' object has no attribute 'tag'

caused by org.python.core.PyException
Traceback (most recent call last):
File "function:runAction", line 4, in runAction
AttributeError: 'com.inductiveautomation.perspective.gateway.script' object has no attribute 'tag'

Ignition v8.1.18 (b2022061518)
Java: Azul Systems, Inc. 11.0.15

I am rather lost. I have gotten this same error before and fumbled my way out it somehow.

I'm going to guess somewhere you've used system as a variable name.

That sounds like crazy talk. Ask him/her, "Why?"

3 Likes

Sorry, I hadn't actually tested that code, and had accidentally supplied self. instead of system.. I've updated the snippet.

1 Like

Good news: Either way, the tag system and Jython will coerce, so the not approach works:
Kapture 2023-05-31 at 15.52.23

1 Like

Um, why not? These are perfectly fine data types. Im interested now what other things this person was adamant about?

Theres a mistake in the read locking as well, on phone so bit hard to correct... Need to add [0].value

This did work for writing to a tag in the tag directory. The tag (rather parameter) I am trying to write to is a parameter on the view, which is then bound to a tag in the tag directory.

image

I mean, it works, repeatability is going to be a little more work. But it works, so thanks! All the input is much appreciated!

For those that are wondering why this guy was adamite about using ints for booleans is because his boss didn't like how the boolean value in the tag tree was a check mark or a square, not a 1 or a 0. I am trying to break this habit on this project I am currently doing.

Ah, so the boss is the quack, that makes more sense :duck::grin:

1 Like