# Set/Configure tag binding with script in Perspective

**URL:** <https://forum.inductiveautomation.com/t/set-configure-tag-binding-with-script-in-perspective/68906>\
**Category:** Ignition\
**Tags:** vision\
**Created:** [January 23, 2023, 12:34pm UTC](https://forum.inductiveautomation.com/t/set-configure-tag-binding-with-script-in-perspective/68906 "2023-01-23T12:34:10Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Theodorska](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Theodorska](https://forum.inductiveautomation.com/u/Theodorska)\
**Post date:** [January 23, 2023, 12:34pm UTC](https://forum.inductiveautomation.com/t/set-configure-tag-binding-with-script-in-perspective/68906/1 "2023-01-23T12:34:10Z")

</div>

Hello,  
From my research online it might not be possible (yet). However, I have seen some people claiming it has been possible in Vision.

I want to configure tag bindings via script in Perspective.  
Let's say I have a onStartup script that populates a map with multiple markers where each marker represents a vehicle in a fleet of vehicles. The markers latitude and longitude are dynamic and the data comes from vehicle specific tags. Is there a way to configure the tag binding with a script.

I can set the value for Lat and Lng manually by "self.props.layers.ui.marker[0].lat = Value" however, this does not configure a binding to a tag and would require me to run a while loop continously to update the value dynamically like a tag binding would do.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 23, 2023, 1:02pm UTC](https://forum.inductiveautomation.com/t/set-configure-tag-binding-with-script-in-perspective/68906/2 "2023-01-23T13:02:22Z")

</div>

You cannot. You **can** directly edit the view definition in the gateway filesystem and wait five minutes for Ignition to notice. That would be a project-wide change. There is no solution for per-session binding creation.

There is limited ability to programmatically create bindings for Vision in the designer. All undocumented, unsupported, and cryptically intricate. The options for creating bindings in runtime in a Vision client are more limited than that.

---

<div class="post-metadata">

**Author:** ![Theodorska](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Theodorska](https://forum.inductiveautomation.com/u/Theodorska)\
**Post date:** [January 23, 2023, 1:53pm UTC](https://forum.inductiveautomation.com/t/set-configure-tag-binding-with-script-in-perspective/68906/3 "2023-01-23T13:53:46Z")

</div>

So there is no way to automate configuring tag bindings?

Is it then acceptable to update values that would normally be read directly off a tag by having a script read the tag value and update the value in a continous while loop?

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [January 23, 2023, 2:12pm UTC](https://forum.inductiveautomation.com/t/set-configure-tag-binding-with-script-in-perspective/68906/4 "2023-01-23T14:12:55Z")

</div>

> [@Theodorska](#):
>
> So there is no way to automate configuring tag bindings?

No, there is no way to do it in perspective.

> [@Theodorska](#):
>
> Is it then acceptable to update values that would normally be read directly off a tag by having a script read the tag value and update the value in a continous while loop?

What exactly are you trying to accomplish? I would always recommend against a continuous while loop. What is preventing you from using embedded views and indirect tag bindings? There are other mechanisms for periodically updating if needed. Look into Gateway Events, specifically Scheduled, Timer, and Tag Change.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 23, 2023, 2:19pm UTC](https://forum.inductiveautomation.com/t/set-configure-tag-binding-with-script-in-perspective/68906/5 "2023-01-23T14:19:21Z")

</div>

Never use a continuous while loop. You probably want a polling binding that will call a script that does all of the tag reads and property writes at once. Consider `runScript()` with a non-zero poll time. (A view custom prop would be most appropriate.) Presumably the script can dynamically determine the tag/property relationship from the rest of the view's properties.

---

<div class="post-metadata">

**Author:** ![Theodorska](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Theodorska](https://forum.inductiveautomation.com/u/Theodorska)\
**Post date:** [January 23, 2023, 2:22pm UTC](https://forum.inductiveautomation.com/t/set-configure-tag-binding-with-script-in-perspective/68906/6 "2023-01-23T14:22:56Z")

</div>

What I am trying to achieve is to update the position of units on our fleet on a map component with the Latitude and Longitude values for a marker. Our units move around and therefore the locations change at random times. Therefore I would be interested in updating these values on tag change.

However, since we have so many units i am trying to automate adding them to the map with a script that populates markers on the map, and this is where I wanted to automate the configuration of tag bindings to avoid manually configuring them.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 23, 2023, 2:30pm UTC](https://forum.inductiveautomation.com/t/set-configure-tag-binding-with-script-in-perspective/68906/7 "2023-01-23T14:30:02Z")

</div>

Consider using a gateway tag change script subscribed to all of the relevant tags. That event script can then re-package the important parts of the event into a payload for `system.perspective.sendMessage()` at session scope. That relays these events into your GUI. The map would have a message handler listening for these broadcasts and checking if it contains a marker matching the incoming event, and updating the appropriate property. Probably worth only writing to the prop if it is actually different, minimizing browser churn.

The only manual part would be adding each vehicle's tags to the gateway tag change event's list. You can use a designer script that browses for relevant tags and spits out a list of tag paths. That minimizes the chances for mistakes.

---

<div class="post-metadata">

**Author:** ![Theodorska](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Theodorska](https://forum.inductiveautomation.com/u/Theodorska)\
**Post date:** [January 26, 2023, 10:29am UTC](https://forum.inductiveautomation.com/t/set-configure-tag-binding-with-script-in-perspective/68906/8 "2023-01-26T10:29:41Z")

</div>

Thank you for this! You are truly an expert user.  
Could you provide a simple example of configuring one tag on a component using this approach?

---

<div class="post-metadata">

**Author:** ![Theodorska](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Theodorska](https://forum.inductiveautomation.com/u/Theodorska)\
**Post date:** [January 26, 2023, 12:36pm UTC](https://forum.inductiveautomation.com/t/set-configure-tag-binding-with-script-in-perspective/68906/9 "2023-01-26T12:36:32Z")

</div>

> [@pturmel](#):
>
> onsider using a gateway tag change script subscribed to all of the relevant tags. That event script can then re-package the important parts of the event into a payload for `system.perspective.sendMessage()` at session scope. That relays these events into your GUI. The map would have a message handler listening for these broadcasts and checking if it contains a marker matching the incoming event, and updating the appropriate property. Probably worth only writing to the prop if it is actually different, minimizing browser churn.

I am getting "No perspective session attached to this thread." when trying to use `system.perspective.sendMessage(messageType, payload)` in a gateway event timer script.

Trying to use `system.util.sendMessage(project ='name of project',messageHandler,myDict ='payload',scope ='CG')` but no component seems to be listening for these messages

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [January 26, 2023, 1:12pm UTC](https://forum.inductiveautomation.com/t/set-configure-tag-binding-with-script-in-perspective/68906/10 "2023-01-26T13:12:17Z")

</div>

Have you configured the message handler on the component? Is it set to listen on the correct scope?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 26, 2023, 1:35pm UTC](https://forum.inductiveautomation.com/t/set-configure-tag-binding-with-script-in-perspective/68906/11 "2023-01-26T13:35:15Z")

</div>

> [@Theodorska](#):
>
> No perspective session attached to this thread.

Ah, right. You need to use `system.util.sendMessage()` to cross from plain gateway scope to Perspective sessions. Use `scope='S'`.

---

<div class="post-metadata">

**Author:** ![Theodorska](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Theodorska](https://forum.inductiveautomation.com/u/Theodorska)\
**Post date:** [January 26, 2023, 2:49pm UTC](https://forum.inductiveautomation.com/t/set-configure-tag-binding-with-script-in-perspective/68906/12 "2023-01-26T14:49:28Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/6/4625f8c11c160593a3abb0a718128cd64269a9aa.png)

I am still not able to communicate to a component within a view in perspective with this approach.  
Do you see anything obvious that I am doing wrong?

The gateway script is has success status in the gateway, but the message handler of the component is for sure listning for a system.perspective.sendMessage(messageType, payload) and not a util as I can see it.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 26, 2023, 3:52pm UTC](https://forum.inductiveautomation.com/t/set-configure-tag-binding-with-script-in-perspective/68906/13 "2023-01-26T15:52:37Z")

</div>

So, to get this to work I had to make a handler in the project's "Session Events" to receive from `system.util.sendMessage(..., scope='S')`. It then just calls `system.perspective.sendMessage(..., scope='session')` with the same payload. The component can receive this latter broadcast.

This is a nuance that should be put in the documentation. (That messages broadcast to Perspective via `system.util.sendMessage(..., scope='S')` can only be received in "Session Events".)

---

<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:** [January 26, 2023, 4:29pm UTC](https://forum.inductiveautomation.com/t/set-configure-tag-binding-with-script-in-perspective/68906/14 "2023-01-26T16:29:08Z")

</div>

You could probably use `system.perspective.getSessionInfo()` to get a list of session IDs to broadcast out with `system.perspective.sendMessage` from a 'regular' gateway thread. Might or might not be worth the effort compared to the indirect solution.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 26, 2023, 4:30pm UTC](https://forum.inductiveautomation.com/t/set-configure-tag-binding-with-script-in-perspective/68906/15 "2023-01-26T16:30:26Z")

</div>

> [@paul-griffith](#):
>
> Might or might not be worth the effort compared to the indirect solution.

Meh. Not. The intermediate handler is trivial and only needs to be in one spot.
