[help] Scripting a counting function

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.

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 ?

1 Like

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

2 Likes

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

Hey ive been given this project to do in ignition specifically

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

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):

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

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

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.

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

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

Not much.

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

1 Like

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.

3 Likes

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

Where on the button did you put the script ?

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.

3 Likes

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.

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

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.

4 Likes