# Changing a Tag Value using an onClick Mouse Event

**URL:** <https://forum.inductiveautomation.com/t/changing-a-tag-value-using-an-onclick-mouse-event/74396>\
**Category:** Ignition\
**Tags:** ignition81, perspective, scripting\
**Created:** [May 31, 2023, 9:00pm UTC](https://forum.inductiveautomation.com/t/changing-a-tag-value-using-an-onclick-mouse-event/74396 "2023-05-31T21:00:42Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![T\_Babcock](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@T\_Babcock](https://forum.inductiveautomation.com/u/T_Babcock)\
**Post date:** [May 31, 2023, 9:00pm UTC](https://forum.inductiveautomation.com/t/changing-a-tag-value-using-an-onclick-mouse-event/74396/1 "2023-05-31T21:00:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [May 31, 2023, 9:11pm UTC](https://forum.inductiveautomation.com/t/changing-a-tag-value-using-an-onclick-mouse-event/74396/2 "2023-05-31T21:11:25Z")

</div>

Is it a boolean tag, or an integer tag?

```auto
# 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`.

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![T\_Babcock](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@T\_Babcock](https://forum.inductiveautomation.com/u/T_Babcock)\
**Post date:** [May 31, 2023, 9:43pm UTC](https://forum.inductiveautomation.com/t/changing-a-tag-value-using-an-onclick-mouse-event/74396/4 "2023-05-31T21:43:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [May 31, 2023, 9:49pm UTC](https://forum.inductiveautomation.com/t/changing-a-tag-value-using-an-onclick-mouse-event/74396/5 "2023-05-31T21:49:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [May 31, 2023, 9:49pm UTC](https://forum.inductiveautomation.com/t/changing-a-tag-value-using-an-onclick-mouse-event/74396/6 "2023-05-31T21:49:31Z")

</div>

> [@T\_Babcock](#):
>
> [The] person whom I learned Ignition from was adamant to not use Boolean tag types in Ignition, even if the tag in the PLC was a Boolean.

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

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [May 31, 2023, 10:11pm UTC](https://forum.inductiveautomation.com/t/changing-a-tag-value-using-an-onclick-mouse-event/74396/7 "2023-05-31T22:11:23Z")

</div>

> [@T\_Babcock](#):
>
> This is the error I get.

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

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 31, 2023, 10:53pm UTC](https://forum.inductiveautomation.com/t/changing-a-tag-value-using-an-onclick-mouse-event/74396/8 "2023-05-31T22:53:38Z")

</div>

> [@cmallonee](#):
>
> Is it a boolean tag, or an integer tag?

Good news: Either way, the tag system and Jython will coerce, so the `not` approach works:  
 ![Kapture 2023-05-31 at 15.52.23](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/9/b93725ddf907de9079112b132df39fe6c72c133b.gif)

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [June 1, 2023, 10:42am UTC](https://forum.inductiveautomation.com/t/changing-a-tag-value-using-an-onclick-mouse-event/74396/9 "2023-06-01T10:42:10Z")

</div>

> [@T\_Babcock](#):
>
> Person whom I learned Ignition from was adamite to not use Boolean tag types in Ignition

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

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [June 1, 2023, 10:43am UTC](https://forum.inductiveautomation.com/t/changing-a-tag-value-using-an-onclick-mouse-event/74396/10 "2023-06-01T10:43:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![T\_Babcock](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@T\_Babcock](https://forum.inductiveautomation.com/u/T_Babcock)\
**Post date:** [June 1, 2023, 1:38pm UTC](https://forum.inductiveautomation.com/t/changing-a-tag-value-using-an-onclick-mouse-event/74396/11 "2023-06-01T13:38:02Z")

</div>

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](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/a/2ad13b58e847837963439682cf56a6096ff1ba5f.png)

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.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [June 1, 2023, 9:29pm UTC](https://forum.inductiveautomation.com/t/changing-a-tag-value-using-an-onclick-mouse-event/74396/13 "2023-06-01T21:29:56Z")

</div>

> [@T\_Babcock](#):
>
> 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

Ah, so the boss is the quack, that makes more sense 🦆😁
