# Perspective run script after inactivity timer elapses

**URL:** <https://forum.inductiveautomation.com/t/perspective-run-script-after-inactivity-timer-elapses/43865>\
**Category:** Ignition\
**Tags:** ignition80, perspective\
**Created:** [February 26, 2021, 1:49pm UTC](https://forum.inductiveautomation.com/t/perspective-run-script-after-inactivity-timer-elapses/43865 "2021-02-26T13:49:30Z")\
**Posts on this page:** 20\
**Page:** 1

<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:** [February 26, 2021, 1:49pm UTC](https://forum.inductiveautomation.com/t/perspective-run-script-after-inactivity-timer-elapses/43865/1 "2021-02-26T13:49:30Z")

</div>

I need to run a script to turn off some boolean values in a few page custom properties if there are 2mins of inactivity on the client. How would I do this?

---

<div class="post-metadata">

**Author:** ![vipo](https://avatars.discourse-cdn.com/v4/letter/v/eada6e/32.png) [@vipo](https://forum.inductiveautomation.com/u/vipo)\
**Post date:** [June 9, 2021, 12:58pm UTC](https://forum.inductiveautomation.com/t/perspective-run-script-after-inactivity-timer-elapses/43865/2 "2021-06-09T12:58:50Z")

</div>

I also need this

---

<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:** [June 9, 2021, 4:36pm UTC](https://forum.inductiveautomation.com/t/perspective-run-script-after-inactivity-timer-elapses/43865/3 "2021-06-09T16:36:47Z")

</div>

In an upcoming release, we’re planning to add a `lastActivity` session prop, which would allow you to drive whatever logic you want via a property change script.

---

<div class="post-metadata">

**Author:** ![dpitzer](https://avatars.discourse-cdn.com/v4/letter/d/b5a626/32.png) [@dpitzer](https://forum.inductiveautomation.com/u/dpitzer)\
**Post date:** [June 9, 2021, 5:30pm UTC](https://forum.inductiveautomation.com/t/perspective-run-script-after-inactivity-timer-elapses/43865/4 "2021-06-09T17:30:59Z")

</div>

I haven’t experimented with this yet, but I’m wondering if you could set up your project (In Project\>Project Properties\>Inactivity) to enable an inactivity timer of 2 minutes which would generate a message to the user. Then create a Session Message Event script that would fire when this message comes through in which you can set your booleans as desired.  
Optionally, you can configure the project to have a longer grace period if you don’t want to actually log them out or close the session after 2 minutes.  
The lastActivity session prop would definitely be a preferred solution though.

---

<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 9, 2021, 9:17pm UTC](https://forum.inductiveautomation.com/t/perspective-run-script-after-inactivity-timer-elapses/43865/5 "2021-06-09T21:17:27Z")

</div>

This would all be achievable and fully customisable with the `lastActivity` session prop 🙂

---

<div class="post-metadata">

**Author:** ![kyle.bennett](https://avatars.discourse-cdn.com/v4/letter/k/71e660/32.png) [@kyle.bennett](https://forum.inductiveautomation.com/u/kyle.bennett)\
**Post date:** [August 23, 2021, 1:30pm UTC](https://forum.inductiveautomation.com/t/perspective-run-script-after-inactivity-timer-elapses/43865/6 "2021-08-23T13:30:27Z")

</div>

Just bumping this old post now that lastActivity is available in the designer post 8.1.8. Thanks guys!

What’s the most efficient use of it? I’d like to navigate to another view if no activity occurs for ‘x’ seconds. I’m thinking of creating a custom property that houses the current datetime, then put a property change script on that tag that if it’s been longer than ‘x’ seconds, then fire a navigation event. Is there a more efficient way to do this though?

Thanks again!

---

<div class="post-metadata">

**Author:** ![user66](https://avatars.discourse-cdn.com/v4/letter/u/b487fb/32.png) [@user66](https://forum.inductiveautomation.com/u/user66)\
**Post date:** [February 16, 2022, 10:00am UTC](https://forum.inductiveautomation.com/t/perspective-run-script-after-inactivity-timer-elapses/43865/7 "2022-02-16T10:00:22Z")

</div>

Is there any work around to implement this functionality in version 8.1.7?

---

<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:** [February 16, 2022, 4:15pm UTC](https://forum.inductiveautomation.com/t/perspective-run-script-after-inactivity-timer-elapses/43865/8 "2022-02-16T16:15:54Z")

</div>

No. Upgrade.

You don’t have the ability to author your own code that runs _on the browser_ in Perspective, very deliberately.

---

<div class="post-metadata">

**Author:** ![wbaker](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wbaker/32/85315_2.png) [@wbaker](https://forum.inductiveautomation.com/u/wbaker)\
**Post date:** [April 12, 2023, 10:17pm UTC](https://forum.inductiveautomation.com/t/perspective-run-script-after-inactivity-timer-elapses/43865/9 "2023-04-12T22:17:05Z")

</div>

@paul-griffith , is lastActivity supposed to update when Perspective Automatically Detects Inactivity?

Using these settings, my lastActivity timestamp doesn't change at Logout.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/e/ce2b692ea0309e58892e55ef4f716e0013a097f3.png)

Goal is to close a dock when user is auto logged out.

---

<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:** [April 12, 2023, 10:21pm UTC](https://forum.inductiveautomation.com/t/perspective-run-script-after-inactivity-timer-elapses/43865/10 "2023-04-12T22:21:41Z")

</div>

That's not how it works.  
`lastActivity` is a datetime that's automatically updated the last time the user _does_ do something. If you want to react to that change, it's up to you. So, if you want to close a dock, then start a thread after the property changes that waits 60 seconds, checks if the last activity still hasn't changed from the value it was last set to, and then kicks off whatever action you want.

---

<div class="post-metadata">

**Author:** ![Manojkumar\_Palanisam](https://avatars.discourse-cdn.com/v4/letter/m/8edcca/32.png) [@Manojkumar\_Palanisam](https://forum.inductiveautomation.com/u/Manojkumar_Palanisam)\
**Post date:** [March 22, 2024, 8:59am UTC](https://forum.inductiveautomation.com/t/perspective-run-script-after-inactivity-timer-elapses/43865/11 "2024-03-22T08:59:10Z")

</div>

I am facing an issue with **lastActivity** status tag when using it inside the script. The purpose of the script is to do auto-logout after an ideal time. The issue is **lastActivity** value is not updating in script when client is running. I have written a script in **Session Probs \> SESSION CUSTOM \> Expression Structure** and below is my script. Kindly support me to resolve this issue.

&nbsp;

```
_def transform(self, value, quality, timestamp):_
 _lt01 = value['LA']_
 _ct01 = value['CT']_
 _idTime = value['IdealTime']_
 _if lt01:_
 _diff = system.date.minutesBetween(ct01, lt01)_
 _if diff \>= idTime:_
 _system.perspective.logout()_
 _return value_
```

&nbsp;

&nbsp;

---

<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:** [March 22, 2024, 9:14am UTC](https://forum.inductiveautomation.com/t/perspective-run-script-after-inactivity-timer-elapses/43865/12 "2024-03-22T09:14:21Z")

</div>

Let's clarify some terminology. It may help solve your issue.

> [@Manojkumar\_Palanisam](#):
>
> I am facing an issue with **lastActivity** status tag ...

`lastActivity` isn't a tag - it's a session property.

> [@Manojkumar\_Palanisam](#):
>
> I have written a script in **Session Probs \> SESSION CUSTOM \> Expression Structure**

1. Typo. It's SESSION PROPS, as in "properties".
2. SESSION CUSTOM isn't a child of SESSION PROPS. It sits alongside them.

What is "Expression Structure"? It can't be a custom prop if it has a space in the name.  
How is the script you provided related to lastActivity?

---

<div class="post-metadata">

**Author:** ![Manojkumar\_Palanisam](https://avatars.discourse-cdn.com/v4/letter/m/8edcca/32.png) [@Manojkumar\_Palanisam](https://forum.inductiveautomation.com/u/Manojkumar_Palanisam)\
**Post date:** [March 22, 2024, 2:48pm UTC](https://forum.inductiveautomation.com/t/perspective-run-script-after-inactivity-timer-elapses/43865/13 "2024-03-22T14:48:28Z")

</div>

![Screenshot 2024-03-22 200548](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/d/bd364536d3fa9abdc88b5425fcf966de6cd65f1d.png)

---

<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:** [March 22, 2024, 2:52pm UTC](https://forum.inductiveautomation.com/t/perspective-run-script-after-inactivity-timer-elapses/43865/14 "2024-03-22T14:52:22Z")

</div>

1. This script should _not_ be in a script transform - a script transform is for driving the value returned for a prop, not for executing arbitrary code. This belongs inside of a value change event (right click on the "AutoLogoff" prop and click "Add Change Script...").

2. That's a strange way, to me, to derive the values of the keys under AutoLogoff, by using an expression structure. I normally use an expression structure to monitor the values of multiple things which should all trigger the binding to change. I would be moving those expressions into individual bindings on the keys instead.

---

<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:** [March 22, 2024, 3:44pm UTC](https://forum.inductiveautomation.com/t/perspective-run-script-after-inactivity-timer-elapses/43865/15 "2024-03-22T15:44:05Z")

</div>

1. In SESSION CUSTOM create a value property "idleTimeout".
2. On `idleTimeout` create an expression binding.  
`(now() - {this.props.lastActivity}) > 5000 // 5 s`  
The 5 s timeout is just to enable you to test it quickly. Point this at some other variable if you want. You seem to be using a tag in your example but tags are common to all projects so it doesn't seem appropriate.
3. Right-click on `custom.idleTimeout` and select Add Change Script ...  
Put one line in there:  
`system.perspective.logout()`

Be aware that Perspective has [Perspective Project Properties | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/ignition-modules/perspective/perspective-project-properties#perspective-inactivity-properties) which includes the session timeout. If you're going to do your own custom auto-logout then you need to set the project's auto-timeout to a value longer than your timout.

---

<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:** [March 23, 2024, 10:29pm UTC](https://forum.inductiveautomation.com/t/perspective-run-script-after-inactivity-timer-elapses/43865/16 "2024-03-23T22:29:52Z")

</div>

> [@Transistor](#):
>
> system.perspective.logout()

I actually don't know if this works from the session prop change script. I looked at what I did and I sent a message from my session prop and had a message handler in my navigation dock which did the logout... it was a long time ago I did it, but I wouldn't have gone such a bizarre route if I could get it working from the session on change script... there are definitely some peculiarities that I haven't yet worked out with targetting sessions from different places.  
I was about to test this all out, but it was very late at night...

---

<div class="post-metadata">

**Author:** ![Manojkumar\_Palanisam](https://avatars.discourse-cdn.com/v4/letter/m/8edcca/32.png) [@Manojkumar\_Palanisam](https://forum.inductiveautomation.com/u/Manojkumar_Palanisam)\
**Post date:** [March 25, 2024, 5:28am UTC](https://forum.inductiveautomation.com/t/perspective-run-script-after-inactivity-timer-elapses/43865/17 "2024-03-25T05:28:10Z")

</div>

After I implement the script in "Add Change Script". I am getting below error message.  
com.inductiveautomation.ignition.common.script.JythonExecException  
Traceback (most recent call last):  
File "function:valueChanged", line 2, in valueChanged  
NameError: global name 'now' is not defined

```
at org.python.core.Py.NameError(Py.java:259)
at org.python.core.PyFrame.getglobal(PyFrame.java:265)
at org.python.pycode._pyx524.valueChanged$1(<function:valueChanged>:3)
at org.python.pycode._pyx524.call_function(<function:valueChanged>)
at org.python.core.PyTableCode.call(PyTableCode.java:173)
at org.python.core.PyBaseCode.call(PyBaseCode.java:306)
at org.python.core.PyFunction.function ___call__ (PyFunction.java:474)
at org.python.core.PyFunction. __call__ (PyFunction.java:469)
at org.python.core.PyFunction. __call__ (PyFunction.java:464)
at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:847)
at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:829)
at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runFunction(ProjectScriptLifecycle.java:868)
at com.inductiveautomation.ignition.common.script.ScriptManager$ScriptFunctionImpl.invoke(ScriptManager.java:1010)
at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$AutoRecompilingScriptFunction.invoke(ProjectScriptLifecycle.java:950)
at com.inductiveautomation.perspective.gateway.script.ScriptFunctionHelper.invoke(ScriptFunctionHelper.java:161)
at com.inductiveautomation.perspective.gateway.script.ScriptFunctionHelper.invoke(ScriptFunctionHelper.java:98)
at com.inductiveautomation.perspective.gateway.model.PropertyChangeScript.access$001(PropertyChangeScript.java:31)
at com.inductiveautomation.perspective.gateway.model.PropertyChangeScript$ScriptSequencer.run(PropertyChangeScript.java:162)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at com.inductiveautomation.perspective.gateway.threading.BlockingWork$BlockingWorkRunnable.run(BlockingWork.java:58)
at java.base/java.lang.Thread.run(Unknown Source)

```

Caused by: org.python.core.PyException  
Traceback (most recent call last):  
File "function:valueChanged", line 2, in valueChanged  
NameError: global name 'now' is not defined

```
... 24 more

```

Ignition v8.1.35 (b2023120517)  
Java: Azul Systems, Inc. 17.0.8

---

<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:** [March 25, 2024, 6:25am UTC](https://forum.inductiveautomation.com/t/perspective-run-script-after-inactivity-timer-elapses/43865/18 "2024-03-25T06:25:51Z")

</div>

> [@Manojkumar\_Palanisam](#):
>
> the script

We can't possibly help you without seeing the script as your original script doesn't have `now` in it I can only deduce that you're not using it. If you're looking at Transistor's reply, he mentions using `now()` inside of an _expression_ binding. The scripting environment doesn't have a `now()` function, it has a `system.date.now()`, but you most likely don't want to be using that inside of your script in this instance

---

<div class="post-metadata">

**Author:** ![Manojkumar\_Palanisam](https://avatars.discourse-cdn.com/v4/letter/m/8edcca/32.png) [@Manojkumar\_Palanisam](https://forum.inductiveautomation.com/u/Manojkumar_Palanisam)\
**Post date:** [March 25, 2024, 8:51am UTC](https://forum.inductiveautomation.com/t/perspective-run-script-after-inactivity-timer-elapses/43865/19 "2024-03-25T08:51:31Z")

</div>

The above error log is for Transistor Script wrote in Chage Script property. I also wrote my own script in Chage Script property. For this code error is not raising but Auto log-out function not happening after ideal time. Below is original code.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/c/bc249e05dcade82de90540c4abd2b17518f046cb.png)

---

<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:** [March 25, 2024, 9:36am UTC](https://forum.inductiveautomation.com/t/perspective-run-script-after-inactivity-timer-elapses/43865/20 "2024-03-25T09:36:12Z")

</div>

Please explain why you can't use Project | Project Properties | Perspective | General | Session Timeout.

What is special about your application?

[Next page](https://forum.inductiveautomation.com/t/perspective-run-script-after-inactivity-timer-elapses/43865.md?page=2)
