Using a Timer script

I swear i am going to get banned asking so many questions…

I am wanting to create a time based off of a tags value. Example…

When TagA = TagValueA then start timer, and if at all possible write an entry to a sql table with the start datetime. Continue running the timer until it reaches 5 minutes, then open a docked window. Continue to count until TagA = TagValueB, and then update sql table with end datetime. Once it writes both start and end datetimes then reset counter back to zero.

I am completely open to suggestions, it does not have to be a timer script if there is another way. If this is also not possible, then that is fine as well.

Could you explain what you are trying to make, instead of how?

It seems like you’re mixing responsibilities of the code. Like you have a part about opening a popup, which is clearly a GUI thing, and you have a part about registering it in SQL, which is a gateway thing (otherwise with multiple clients you get multiple records).

To me, it sounds like you’re trying to implement an alarm with a delayed execution. If that’s the case, most of the things are prepared for you in Ignition.

1 Like

What you’re trying to accomplish is definitely possible.

There are a few ways to approach this. You need to first decide if this is going to run on the Client (in a window, using a timer component) or on the Gateway (using tags/alarms, or SFCs).

I recommend using a tag or set of tags and alarms. The active delay on the alarm can be your timer, and you get a script that fires when the alarm goes active. You can also clear the alarm when you hit your “TagValueB”. You can use these scripts to write to the database.

If you needed a window to open, your client’s can watch the tag via a tag change script.

I guess the best way to describe it would be almost like a HMI application. It won’t necessarily control the PLC, but management wants the ability to track uptime and downtime on the machine. The previous person before me created a webpage with an arduino board, and we are trying to move away from that and tie directly into the PLC on the machine itself. I don’t have enough room on the main window to display all the downtime reason codes that is why i was wanting to open another docked window. Sorry if this isn’t detailed enough. I would be happy to explain more if needed. Thanks for your response.

That’s indeed a job for alarms.

On the alarm, you can check if the tag value is between two setpoints (which can be linked to tags on their own). You can add an active delay (time before the alarm comes active).


Then, regarding the registration, by default, the alarm log will create entries on alarm activation and deactivation in an SQL table. So you could use those for analysis. Or you could write a tag script that triggers on alarm active and alarm cleared to write your own data to a table.


Then for displaying, you can bundle it with other alarms and display them in an alarm table component.

Generally we have a button that shows the number of alarms, and shows the alarm details (in that table component) when users click on it.

The alarm status can be queried with the system.alarm.queryStatus method on the clients.

This will integrate a lot better with eventual other alarms for the operators.

Thank you for the information. I have started with the alarm. My first question is for when i create the tag event script for adding data to sql. I do not know the correct syntax for what i am trying to accomplish. Should it be

and that would write the start datetime to my table, and then for the alarmCleared write QueryB to write the end datetime?

I was also using Outside Setpoints for my alarm as any value besides 1 or 2 needs to start the alarm. Would this be the correct way to do so?

To run a query, you can either create a named query, and call it like you do. Or you can run the SQL directly with system.db.runPrepUpdate('SQL HERE', [parameters], "Database name").

And if your alarm needs to be active between 1 and 2, you need to choose the “between setpoints” option.

This is what i had for my alarm settings…

But yet, when my tag reaches 3 the alarm is not getting triggered. Am i missing something?

So to clarify, i was not looking in the right place. It would appear that the alarm is getting triggered, but it is not running the queries as the alarm is raised and cleared.

If its a server side script, then you could have two 1 second timers running continuously in startup script. Inside the first timer you could check condition "TagA = TagValueA " to set a memory tag “timer2on” to True. In timer two, check if “timer2on” flag and skip the loop is its off. If its ON then count upto 5 mins and reset this memory tag to false. Write in data base the time stamps at appropriate events. Something like that!