# Momentary button script on Perspective

**URL:** <https://forum.inductiveautomation.com/t/momentary-button-script-on-perspective/46360>\
**Category:** Ignition\
**Tags:** ignition80, perspective\
**Created:** [May 5, 2021, 8:31am UTC](https://forum.inductiveautomation.com/t/momentary-button-script-on-perspective/46360 "2021-05-05T08:31:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniele1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/daniele1/32/31531_2.png) [@daniele1](https://forum.inductiveautomation.com/u/daniele1)\
**Post date:** [May 5, 2021, 8:31am UTC](https://forum.inductiveautomation.com/t/momentary-button-script-on-perspective/46360/1 "2021-05-05T08:31:18Z")

</div>

Hi,  
a few days ago I posted a question about how to handle a bool type tag that needs to go back to 0 after few seconds after it goes to 1.

> [@Sending and receiving a data request from a device through MQTT from Ewon](http://forum.inductiveautomation.com/t/sending-and-receiving-a-data-request-from-a-device-through-mqtt-from-ewon/46101):
>
> Hi, I have a system made up of different station (made up of a PLC and and Ewon router) connected to an Ignition gateway through MQTT that send data when those values changes. Sometimes it happens that the gateway looses the tags stored in the MQTT Engine provider: that sometimes happens because the server goes down or other mysterious reasons. The simpler way to get back those tags is to simply sending them back from the stations. To be able to send all the tags back from the SCADA I create…

In Vision I just used a Momentary Button but in Perspective such a component doesn't exist.  
Searching through the forum I found out about a possible solution that uses the Timer function (I believe from Java) used in a tag script to comands the value back to 0 after a few seconds

> [@Perspective: Scripting a Momentary Button](http://forum.inductiveautomation.com/t/perspective-scripting-a-momentary-button/35684/5):
>
> I had the same application where I could not alter the PLC code and needed a momentary script. This would write a tag on, delay 1.5 seconds, and then write it off again. from threading import Timer def writeOn(): system.tag.write("[default]Testing Tags/Bool Test", 1) def delayOff(): system.tag.write("[default]Testing Tags/Bool Test", 0) writeOn() t = Timer(1.5, delayOff) t.start() Again, as stated - it is not recommended as there is no way to guarantee it gets written off again. How…

The code that @aspatchek2013 is using doens't seem the "standard Ignition Script language" (Jython?), will the function Timer work in a "standard script"? Should I import any library?  
I'm not familiar with Java so any help would be great, thanks.

---

<div class="post-metadata">

**Author:** ![Federick732000](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/federick732000/32/23954_2.png) [@Federick732000](https://forum.inductiveautomation.com/u/Federick732000)\
**Post date:** [May 5, 2021, 8:41am UTC](https://forum.inductiveautomation.com/t/momentary-button-script-on-perspective/46360/2 "2021-05-05T08:41:50Z")

</div>

Hi @daniele1,

maybe using the time library may help you.

I usually use this script to keep a tag to 1 for few seconds:

```auto
import time

system.tag.write("[default]Test/Tag", 1)
time.sleep(1) 
system.tag.write("[default]Test/Tag", 0)

```

the parameter of time.sleep is the amount of seconds the script will wait before continuing the script.  
So if you want for example 100ms, you can write:

```auto
time.sleep(0.1)

```

Hope it helps 🙂

---

<div class="post-metadata">

**Author:** ![matthew.ayre](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matthew.ayre/32/19499_2.png) [@matthew.ayre](https://forum.inductiveautomation.com/u/matthew.ayre)\
**Post date:** [May 5, 2021, 9:04am UTC](https://forum.inductiveautomation.com/t/momentary-button-script-on-perspective/46360/3 "2021-05-05T09:04:06Z")

</div>

Just to add to this, sleep is not recommended for some applications as it locks the thread. There are [other methods for adding delay](https://docs.inductiveautomation.com/display/DOC81/Adding+a+Delay+to+a+Script).

@Federick732000’s answer is perfectly acceptable as long as you are aware of the caveats/risks associated with it 🙂

---

<div class="post-metadata">

**Author:** ![daniele1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/daniele1/32/31531_2.png) [@daniele1](https://forum.inductiveautomation.com/u/daniele1)\
**Post date:** [May 5, 2021, 10:05am UTC](https://forum.inductiveautomation.com/t/momentary-button-script-on-perspective/46360/4 "2021-05-05T10:05:02Z")

</div>

Ok, thanks.  
I just saw this thread [How to implement "Momentary Button" function in Perspective View](http://forum.inductiveautomation.com/t/how-to-implement-momentary-button-function-in-perspective-view/39103) that mentions that function and I was wondering what was the difference between the two solutions.  
When writing/reading tags I always use writeBlocking and readBlocking (mostly because I didin’t understood how the Async method works 😅 ), so using time.sleep() shouldn’t make a difference I believe.

---

<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:** [May 5, 2021, 10:21am UTC](https://forum.inductiveautomation.com/t/momentary-button-script-on-perspective/46360/5 "2021-05-05T10:21:13Z")

</div>

Using sleep in Perspective isn’t much of an issue since the gateway runs all scripts and it has no effect on the gui like it does in Vision. A Vision client would be locked up for the time that it takes to run the script. That being said, I wouldn’t make a habit of using sleep as there are better solutions most of the time. Also, if you have lots of scripts with sleeps, then the gateway threads might start to build up which could cause stability issues

The `threading.Timer` library is a standard Python library. It’s technically fine to use in any python script.  
The difference between using `time.sleep` and `threading.Timer` is that `time.sleep` will delay the progression of your current script, whereas `threading.Timer` will spawn another thread and execute the given function after a delay, leaving your calling script free to continue on executing the rest of the code. For Perspective, `time.sleep` is simpler if you don’t need the function to execute asynchronously to your script

Also, `tag.write` and `tag.read` functions are from Ignition versions below v8.0 and shouldn’t be used in v8+ projects. You’re correct in using `read/writeBlocking/Async` etc.

---

<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 5, 2021, 4:44pm UTC](https://forum.inductiveautomation.com/t/momentary-button-script-on-perspective/46360/6 "2021-05-05T16:44:41Z")

</div>

> [@nminchin](#):
>
> Using sleep in Perspective isn’t much of an issue since the gateway runs all scripts and it has no effect on the gui like it does in Vision.

That's not entirely true - while scripting in Perspective isn't _directly_ tried to an 'event dispatch thread' as it is in Vision, it's still bad practice to "hold" a Perspective worker thread for longer than strictly necessary to perform your script. Using asynchronous threads or workers is definitely still best practice.

---

<div class="post-metadata">

**Author:** ![aspatchek2013](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/aspatchek2013/32/85339_2.png) [@aspatchek2013](https://forum.inductiveautomation.com/u/aspatchek2013)\
**Post date:** [May 5, 2021, 6:35pm UTC](https://forum.inductiveautomation.com/t/momentary-button-script-on-perspective/46360/7 "2021-05-05T18:35:16Z")

</div>

@daniele1

I tend to use the threading library whenever this situation arises. The timer.sleep function may have applications where it works, but it will always put more limitations on system functionality than the threading option.

On top of that, if I need to run another function during the ‘waiting’ portion of either of these options, the threading option will be only solution. The timer.sleep function will not run the rest of the script until after the sleep is complete whereas the threading timer works well for this.

Due to the differences and my desire to standardize whenever possible, I stick with the threading library.
