# No perspective session attached to this thread" error for system.perspective functions

**URL:** <https://forum.inductiveautomation.com/t/no-perspective-session-attached-to-this-thread-error-for-system-perspective-functions/32779>\
**Category:** Ignition Early Access\
**Tags:** perspective, scripting\
**Created:** [March 3, 2020, 6:40pm UTC](https://forum.inductiveautomation.com/t/no-perspective-session-attached-to-this-thread-error-for-system-perspective-functions/32779 "2020-03-03T18:40:11Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![fstamm](https://avatars.discourse-cdn.com/v4/letter/f/87869e/32.png) [@fstamm](https://forum.inductiveautomation.com/u/fstamm)\
**Post date:** [March 3, 2020, 6:40pm UTC](https://forum.inductiveautomation.com/t/no-perspective-session-attached-to-this-thread-error-for-system-perspective-functions/32779/1 "2020-03-03T18:40:11Z")

</div>

Hi!

I’ve been trying to use system.perspective.sendMessage in a gateway timer script but I keep getting the “No perspective session attached to this thread.” error. For a test, I made a tag change script with only:

```auto
sessionInfo = system.perspective.getSessionInfo()
system.perspective.print(sessionInfo)

```

in the script and received the same error.

However, this works:

```auto
sessionInfo = system.perspective.getSessionInfo()
print sessionInfo

```

Am I not allowed to use certain system.perspective functions in gateway scripts? What am I missing?

Thank you!

---

<div class="post-metadata">

**Author:** ![jspecht](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jspecht/32/6465_2.png) [@jspecht](https://forum.inductiveautomation.com/u/jspecht)\
**Post date:** [March 3, 2020, 6:55pm UTC](https://forum.inductiveautomation.com/t/no-perspective-session-attached-to-this-thread-error-for-system-perspective-functions/32779/2 "2020-03-03T18:55:17Z")

</div>

To which console are you trying to print session information?

`system.perspective.print` will try to print the given message to the browser console (if the target page belongs to a client session) or the designer console (if the target page belongs to a designer session), but in order to do this, it needs to know _which_ page’s console to target. If you do not provide the target session and page, Perspective will default to the session and page in the context of the script which calls `system.perspective.print`, but since Gateway timer scripts do not run in the context of a Perspective Session, it does not know where to print the message, and will throw an error.

If you want to print session information to the Gateway’s output console, then using the `print` command will work. You could also target a Gateway logger using `system.util.getLogger`

---

<div class="post-metadata">

**Author:** ![fstamm](https://avatars.discourse-cdn.com/v4/letter/f/87869e/32.png) [@fstamm](https://forum.inductiveautomation.com/u/fstamm)\
**Post date:** [March 3, 2020, 7:22pm UTC](https://forum.inductiveautomation.com/t/no-perspective-session-attached-to-this-thread-error-for-system-perspective-functions/32779/4 "2020-03-03T19:22:10Z")

</div>

I was calling perspective.print with a tag change script to test any system.perspective function within a gateway script.

What I really need is to run the system.perspective.sendMessage function from a gateway timer script. This is the full code which does work when run from a button press but not from the gateway timer script:

```auto
# state of alarms we care about
state = ["ActiveUnacked", "ActiveAcked", "ClearUnacked"]

numAlarmsShelved = 0
sources = []

# get alarms objects
alarms = system.alarm.queryStatus(state = state)
alarmsShelved = system.alarm.queryStatus(includeShelved=True)
 
# get number of active alarms
numAlarms = len(alarms)

# extract from the alarm object the number of
# shelved alarms with *unique* sources
for i in alarmsShelved:
	if not (i.getSource() in sources):
		if i.isShelved():
			sources.append(i.getSource())
			numAlarmsShelved += 1

# cast to string
numAlarms = str(numAlarms)
numShelved = str(numAlarmsShelved)

# construct message to be sent to alarms label in the upper navigation dock
messageType = 'alarmAlert'
payload = {'alarms': numAlarms, 'shelved': numShelved}
scope = 'session'

# send message
system.perspective.sendMessage(messageType, payload, scope)

```

This always throws the “no perspective session attached to this thread” error. Should we be able to use sendMessage() from a timer script?

---

<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:** [March 3, 2020, 7:36pm UTC](https://forum.inductiveautomation.com/t/no-perspective-session-attached-to-this-thread-error-for-system-perspective-functions/32779/5 "2020-03-03T19:36:47Z")

</div>

As Joel alluded to, calling (most) `system.perspective` scripting functions _requires_ contextual information about which session the action should be applied to - because unlike Vision, where clients are each an independent Java Virtual Machine, Perspective sessions all execute on the gateway, and share script execution threads. When calling scripts directly from Perspective, we do some automatic bookkeeping for you, so that the functions ‘know’ the context of where they’re being called.

But when you’re in the context of a gateway timer script, or tag change script, or anything else that’s running on the gateway, you’re totally separate from Perspective. When you call `system.perspective.sendMessage` from a timer script - how would it “know” what session to send the message to? So you _have to_ specify a session ID, which is what the error message is indicating. You can do that, now, using `system.perspective.getSessionInfo()`. If you want to send a message to _all_ sessions, then you would iterate through the object you get from `getSessionInfo()`, and call `system.perspective.sendMessage`, specifying a `sessionId` in each call:  
`system.perspective.sendMessage(messageType, payload, scope, sessionId=session)`

---

<div class="post-metadata">

**Author:** ![fstamm](https://avatars.discourse-cdn.com/v4/letter/f/87869e/32.png) [@fstamm](https://forum.inductiveautomation.com/u/fstamm)\
**Post date:** [March 3, 2020, 9:50pm UTC](https://forum.inductiveautomation.com/t/no-perspective-session-attached-to-this-thread-error-for-system-perspective-functions/32779/6 "2020-03-03T21:50:25Z")

</div>

Thanks for the clarification! I added:

```auto
sessionInfo = system.perspective.getSessionInfo()

for session in sessionInfo:
	thisSessionID = session["id"]
	# send message
	system.perspective.sendMessage(messageType, payload, scope, sessionId = thisSessionID)

```

Unfortunately now I’m getting and error that reads: “Invalid UUID string:” and then the session id. I also tried using parentSessionId instead which gives me “Perspective session “1c62b5a0-acb0-45a…” not found”.

Am I iterating through the getSessionInfo object incorrectly?

---

<div class="post-metadata">

**Author:** ![fstamm](https://avatars.discourse-cdn.com/v4/letter/f/87869e/32.png) [@fstamm](https://forum.inductiveautomation.com/u/fstamm)\
**Post date:** [March 3, 2020, 10:42pm UTC](https://forum.inductiveautomation.com/t/no-perspective-session-attached-to-this-thread-error-for-system-perspective-functions/32779/7 "2020-03-03T22:42:10Z")

</div>

Cancel that, I came back from lunch and now it works. I’ll go with it. Thank you so much for your help!

---

<div class="post-metadata">

**Author:** ![ken.maze](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ken.maze/32/85876_2.png) [@ken.maze](https://forum.inductiveautomation.com/u/ken.maze)\
**Post date:** [August 28, 2021, 6:27am UTC](https://forum.inductiveautomation.com/t/no-perspective-session-attached-to-this-thread-error-for-system-perspective-functions/32779/8 "2021-08-28T06:27:47Z")

</div>

I had the same problem as OP. Got that fixed up with sessionid as suggested. I am getting the UUID too large.

system.perspective.sendMessage(“pumpUnlock”, payload = {}, scope = “session”, sessionID = sessionInfo)

Perhaps I need to pass the pageId parameter as well as mentioned in the system.perspective.sendMessage documentation? I tried that, but not sure if I am getting it right, I assume it;'s the name of the page as lasted in the Perspective Page Configuration?

I’m going to bed, I’m hoping it just starts working tomorrow like OP.

Thanks

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [August 28, 2021, 1:57pm UTC](https://forum.inductiveautomation.com/t/no-perspective-session-attached-to-this-thread-error-for-system-perspective-functions/32779/9 "2021-08-28T13:57:46Z")

</div>

> [@ken.maze](#):
>
> I’m hoping it just starts working tomorrow like OP.

It's not going to.

You need to look at what you're passing as `sessionInfo`. If you're using the code supplied in a previous thread, then you're passing a list of current sessions and all their available information. You need to supply the `id` of a singular session from that list. How you make that determination is up to you.

---

<div class="post-metadata">

**Author:** ![ken.maze](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ken.maze/32/85876_2.png) [@ken.maze](https://forum.inductiveautomation.com/u/ken.maze)\
**Post date:** [August 29, 2021, 2:07pm UTC](https://forum.inductiveautomation.com/t/no-perspective-session-attached-to-this-thread-error-for-system-perspective-functions/32779/10 "2021-08-29T14:07:10Z")

</div>

Indeed it did not lol. Thanks for the tip. I realized there was not any Perspective context within it anyway, so moved it into the project library. Thanks!

---

<div class="post-metadata">

**Author:** ![david.gillbee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/david.gillbee/32/27863_2.png) [@david.gillbee](https://forum.inductiveautomation.com/u/david.gillbee)\
**Post date:** [October 12, 2022, 11:58pm UTC](https://forum.inductiveautomation.com/t/no-perspective-session-attached-to-this-thread-error-for-system-perspective-functions/32779/11 "2022-10-12T23:58:57Z")

</div>

> [@ken.maze](#):
>
> UUID too

In the project script you trigger a project wide message:  
system.util.sendMessage("myProject", "GWMSG",{'stuff':'things'},'S')

Then in the session events create a handler for this message.  
The name needs to match the message above ie GWMSG.  
In this message handler you send a new message at the session level. Make sure to add the scope argument = session.  
system.perspective.sendMessage("SMSG",payload,'session')

Then on the page you can add a message handler script with the message type matching the session message name ie SMSG

You do not necessarily need to iterate over the sessions manually as long as you set the scope to session.

The error with the UUID is because the session iterator also includes designer sessions, if you manually iterate you would need to add a line to check the if(session['userAgent']!='designer')

Hope this help clear it up for anyone still struggling.

---

<div class="post-metadata">

**Author:** ![cdunn.home](https://avatars.discourse-cdn.com/v4/letter/c/45deac/32.png) [@cdunn.home](https://forum.inductiveautomation.com/u/cdunn.home)\
**Post date:** [June 17, 2025, 2:22pm UTC](https://forum.inductiveautomation.com/t/no-perspective-session-attached-to-this-thread-error-for-system-perspective-functions/32779/12 "2025-06-17T14:22:30Z")

</div>

The userAgent is what got me, too! Thanks for the tip!
