# \[help\] Scripting a counting function

**URL:** <https://forum.inductiveautomation.com/t/help-scripting-a-counting-function/56909>\
**Category:** Ignition\
**Created:** [March 7, 2022, 2:57pm UTC](https://forum.inductiveautomation.com/t/help-scripting-a-counting-function/56909 "2022-03-07T14:57:27Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dean\_Walsh](https://avatars.discourse-cdn.com/v4/letter/d/8797f3/32.png) [@Dean\_Walsh](https://forum.inductiveautomation.com/u/Dean_Walsh)\
**Post date:** [March 7, 2022, 2:57pm UTC](https://forum.inductiveautomation.com/t/help-scripting-a-counting-function/56909/1 "2022-03-07T14:57:27Z")

</div>

I am trying to make create a count for a given input and to display the number of times it has been activated onto a numeric label.

From my searching the problem online i have tried to understand using memory bits and the gateway tag scripting but i have not been successful and would greatly appreciate advice.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 7, 2022, 3:01pm UTC](https://forum.inductiveautomation.com/t/help-scripting-a-counting-function/56909/2 "2022-03-07T15:01:25Z")

</div>

What type of input is it, and how do you know it’s been activated ? Do you need to keep the count on a per-session basis, or does it need to be global ?

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [March 7, 2022, 3:03pm UTC](https://forum.inductiveautomation.com/t/help-scripting-a-counting-function/56909/3 "2022-03-07T15:03:25Z")

</div>

My first question would be, why do you need to do it in Ignition rather than in the device?

---

<div class="post-metadata">

**Author:** ![Dean\_Walsh](https://avatars.discourse-cdn.com/v4/letter/d/8797f3/32.png) [@Dean\_Walsh](https://forum.inductiveautomation.com/u/Dean_Walsh)\
**Post date:** [March 7, 2022, 3:04pm UTC](https://forum.inductiveautomation.com/t/help-scripting-a-counting-function/56909/4 "2022-03-07T15:04:16Z")

</div>

To keep it simple for now i just want a button to add 1 to the count and keeping it per session is fine

---

<div class="post-metadata">

**Author:** ![Dean\_Walsh](https://avatars.discourse-cdn.com/v4/letter/d/8797f3/32.png) [@Dean\_Walsh](https://forum.inductiveautomation.com/u/Dean_Walsh)\
**Post date:** [March 7, 2022, 3:05pm UTC](https://forum.inductiveautomation.com/t/help-scripting-a-counting-function/56909/5 "2022-03-07T15:05:32Z")

</div>

Hey ive been given this project to do in ignition specifically

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 7, 2022, 3:10pm UTC](https://forum.inductiveautomation.com/t/help-scripting-a-counting-function/56909/6 "2022-03-07T15:10:57Z")

</div>

Well then, make a `session.custom` property, let’s call it `click_counter`, set it to 0.  
Then, in your button’s event, add an `onActionPerformed` script, and make it

```python
self.session.custom.click_counter += 1

```

If you need it globally instead, use a memory integer tag instead of a property, and make your script be something like this (ignoring error handling for simplicity sake):

```python
count = system.tag.readBlocking(["[provider]tag_name"])[0].getValue()
system.tag.writeBlocking(["[provider]tag_name"], [count + 1])

```

---

<div class="post-metadata">

**Author:** ![Conn\_Mccurdy](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@Conn\_Mccurdy](https://forum.inductiveautomation.com/u/Conn_Mccurdy)\
**Post date:** [March 7, 2022, 3:47pm UTC](https://forum.inductiveautomation.com/t/help-scripting-a-counting-function/56909/7 "2022-03-07T15:47:04Z")

</div>

i tried to do this as i was having the same problem but i recieved an error telling me there was a problem with the scripting. I just want to display how many times a button has been pressed on an LED display

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [March 7, 2022, 3:48pm UTC](https://forum.inductiveautomation.com/t/help-scripting-a-counting-function/56909/8 "2022-03-07T15:48:53Z")

</div>

The code provided was for perspective, it will not work as is for vision.

Are you working in vision or perspective?

Provide some code (in Preformatted text (use the `</>` button) and we can help.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 7, 2022, 3:49pm UTC](https://forum.inductiveautomation.com/t/help-scripting-a-counting-function/56909/9 "2022-03-07T15:49:02Z")

</div>

We can’t help with your error if you don’t tell us what the error is…

---

<div class="post-metadata">

**Author:** ![Conn\_Mccurdy](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@Conn\_Mccurdy](https://forum.inductiveautomation.com/u/Conn_Mccurdy)\
**Post date:** [March 7, 2022, 3:52pm UTC](https://forum.inductiveautomation.com/t/help-scripting-a-counting-function/56909/10 "2022-03-07T15:52:53Z")

</div>

Traceback (most recent call last):  
File “event:actionPerformed”, line 1, in   
NameError: name ‘self’ is not defined

Ignition v8.1.15 (b2022030114)  
Java: Azul Systems, Inc. 11.0.14.1

this is the error I received. I dont know if this helps

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [March 7, 2022, 3:59pm UTC](https://forum.inductiveautomation.com/t/help-scripting-a-counting-function/56909/11 "2022-03-07T15:59:39Z")

</div>

Not much.

Looks like your trying to use this code in Vision. The code in vision will be different.

---

<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:** [March 7, 2022, 4:00pm UTC](https://forum.inductiveautomation.com/t/help-scripting-a-counting-function/56909/12 "2022-03-07T16:00:58Z")

</div>

It seems you are copy/pasting code you don’t understand. `self` doesn’t exist everywhere in Ignition. Please show more of what you are trying. When pasting code here on the forum, be sure to the use the “pre-formatted text” button as others have mentioned.

---

<div class="post-metadata">

**Author:** ![Conn\_Mccurdy](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@Conn\_Mccurdy](https://forum.inductiveautomation.com/u/Conn_Mccurdy)\
**Post date:** [March 7, 2022, 4:16pm UTC](https://forum.inductiveautomation.com/t/help-scripting-a-counting-function/56909/13 "2022-03-07T16:16:33Z")

</div>

I am in perspective and have applied the code to the button but am now confused as to how this data can be displayed. I apologize for my lack of understanding as this is a learning curve for me

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 7, 2022, 4:21pm UTC](https://forum.inductiveautomation.com/t/help-scripting-a-counting-function/56909/14 "2022-03-07T16:21:13Z")

</div>

Where on the button did you put the script ?

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [March 7, 2022, 4:25pm UTC](https://forum.inductiveautomation.com/t/help-scripting-a-counting-function/56909/15 "2022-03-07T16:25:28Z")

</div>

Do you mean you moved to perspective? Perspective buttons have an ‘onActionPerformed’ event, not actionPerformed.

I’m not trying to evade your questions, but honestly in the long run you’re going to be better off struggling through this on your own. Learning a new system is as much about learning what doesn’t work as learning what does work.

I know you’ve been referred to Inductive University already, I can’t stress enough to start there. These things will be come clear.

Here is a link to the Perspective Component Overview video, watch it and the others in the lesson, I think it will clear things up for you.

> **[Component Overview - Understanding Perspective Components](https://inductiveuniversity.com/videos/perspective-component-overview/8.1)**
>
> Component Overview: In this lesson, we'll take a brief look at using components in the Perspective module. Learn more about Understanding Perspective Components

---

<div class="post-metadata">

**Author:** ![Conn\_Mccurdy](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@Conn\_Mccurdy](https://forum.inductiveautomation.com/u/Conn_Mccurdy)\
**Post date:** [March 7, 2022, 4:29pm UTC](https://forum.inductiveautomation.com/t/help-scripting-a-counting-function/56909/16 "2022-03-07T16:29:54Z")

</div>

I clicked on the button and went to configure events. I then went to component events and then to ‘onActionPerformed’

I used the code that was given to me.

the following error popped up when the button was pressed:

Unable to run action ‘component.onActionPerformed’ on counter\_view@D/root/Button: code could not be compiled.

---

<div class="post-metadata">

**Author:** ![Conn\_Mccurdy](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@Conn\_Mccurdy](https://forum.inductiveautomation.com/u/Conn_Mccurdy)\
**Post date:** [March 7, 2022, 4:32pm UTC](https://forum.inductiveautomation.com/t/help-scripting-a-counting-function/56909/17 "2022-03-07T16:32:06Z")

</div>

Thanks for the link Lrose, I will keep watching the videos and hopefully pick something up along the way. I think trial and error might be the way to go

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [March 7, 2022, 4:33pm UTC](https://forum.inductiveautomation.com/t/help-scripting-a-counting-function/56909/18 "2022-03-07T16:33:31Z")

</div>

> [@Conn\_Mccurdy](#):
>
> Unable to run action ‘component.onActionPerformed’ on counter\_view@D/root/Button: code could not be compiled.

Probably an Indentation error. Make sure that the pasted code has the same indentation and is at least tabbed or spaced over. If you haven't looked into some basic Python tutorials, those will probably do you good as well.
