# Changing OPC tag value based on time

**URL:** <https://forum.inductiveautomation.com/t/changing-opc-tag-value-based-on-time/98480>\
**Category:** Ignition\
**Tags:** vision, scripting\
**Created:** [November 11, 2024, 7:20pm UTC](https://forum.inductiveautomation.com/t/changing-opc-tag-value-based-on-time/98480 "2024-11-11T19:20:46Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![imdmacs](https://avatars.discourse-cdn.com/v4/letter/i/7feea3/32.png) [@imdmacs](https://forum.inductiveautomation.com/u/imdmacs)\
**Post date:** [November 11, 2024, 7:20pm UTC](https://forum.inductiveautomation.com/t/changing-opc-tag-value-based-on-time/98480/1 "2024-11-11T19:20:46Z")

</div>

Hello, I'm gonna start out by saying i have have little to no scripting experience.

The issue we have is with a sand mixing system that has editable values for the operator, the value in question is for the sand compactibility setpoint, this usually is a static value of 52 but moving into the winter months this value needs to be manually adjusted throughout the day to keep everything happy.  
The problem shows up between shifts when the operators forget to change the value back to 52 from whatever the value was at mid day, causing compactibility to be out of range for start up.  
This is a very old modicon system, i cant readily just use a move block in the program to write 52 into the holding register based on system values.

End goal is to change the value of the compactibility setpoint OPC tag based on the time of day, I've created a "time" expression tag that gives a value of 1,100 for 11am, 1,330 for 1:30pm ect.  
I just want 52 to be written to my OPC tag at 8PM, how can i accomplish this?

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [November 11, 2024, 7:25pm UTC](https://forum.inductiveautomation.com/t/changing-opc-tag-value-based-on-time/98480/2 "2024-11-11T19:25:19Z")

</div>

Use a gateway scheduled event script to write 52 to the tag.

[Gateway Event Scripts | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/platform/scripting/scripting-in-ignition/gateway-event-scripts#gateway-scheduled-scripts)

---

<div class="post-metadata">

**Author:** ![imdmacs](https://avatars.discourse-cdn.com/v4/letter/i/7feea3/32.png) [@imdmacs](https://forum.inductiveautomation.com/u/imdmacs)\
**Post date:** [November 11, 2024, 8:58pm UTC](https://forum.inductiveautomation.com/t/changing-opc-tag-value-based-on-time/98480/3 "2024-11-11T20:58:18Z")

</div>

Thank you, the gateway scheduled event is what i wanted to use with this as it will need to work across multiple open projects.  
The issue I'm having is my lack of scripting experience, I've gotten by with basic expressions over the last few months, but everything i write seems to have errors as show below now. I'm not quite sure how exactly this code would need to be written to be correct.

 ![Ignition Screenshot](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/5/15ca0390b2212cd86a03e3e493fa897438353f2c.png)  
 ![Ignition Screenshot1](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/3/f3fd70b44c117d99fd6fde577afacd5e1539ff21.png)

---

<div class="post-metadata">

**Author:** ![Eric\_Meyer](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/eric_meyer/32/72323_2.png) [@Eric\_Meyer](https://forum.inductiveautomation.com/u/Eric_Meyer)\
**Post date:** [November 11, 2024, 9:31pm UTC](https://forum.inductiveautomation.com/t/changing-opc-tag-value-based-on-time/98480/4 "2024-11-11T21:31:49Z")

</div>

Hey imdmacs,

Not sure I am overthinking this as I am newer as well, but I would probably just use the Ignition function: system.tag.writeBlocking(tagPaths, value)

It would run and write 52 to the tag.

The schedule you would define in the settings of the scheduled gateway timer.

---

<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:** [November 11, 2024, 9:39pm UTC](https://forum.inductiveautomation.com/t/changing-opc-tag-value-based-on-time/98480/5 "2024-11-11T21:39:15Z")

</div>

> **[system.tag.writeBlocking | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-tag/system-tag-writeBlocking)**
>
> Writes values to tags at the given paths.

Note the use of **[lists]** for tagnames and values being passed.

* * *

One reason that your first scheduled script couldn't have worked is that Python / Jython wouldn't have a clue what to do with `[default]UNIT3/AR40502-3`. It might look like a tag path to you but not to the interpreter.

---

<div class="post-metadata">

**Author:** ![Eric\_Meyer](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/eric_meyer/32/72323_2.png) [@Eric\_Meyer](https://forum.inductiveautomation.com/u/Eric_Meyer)\
**Post date:** [November 11, 2024, 9:47pm UTC](https://forum.inductiveautomation.com/t/changing-opc-tag-value-based-on-time/98480/6 "2024-11-11T21:47:19Z")

</div>

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

---

<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:** [November 11, 2024, 10:10pm UTC](https://forum.inductiveautomation.com/t/changing-opc-tag-value-based-on-time/98480/7 "2024-11-11T22:10:00Z")

</div>

You have an extra level of indentation on line 2. (Though probably not fatal.)

`system.tag.writeBlocking()` takes a list of tag paths, and a list of values. Even if just one tag, you need lists. (See the examples in the docs.)

---

<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:** [November 11, 2024, 11:28pm UTC](https://forum.inductiveautomation.com/t/changing-opc-tag-value-based-on-time/98480/8 "2024-11-11T23:28:01Z")

</div>

Eric, that would have to be,

```python
tagPaths = ["[default]testingscheduledscript"]
values = [52]
system.tag.writeBlocking(tagPaths, values)

```

Or, since there is only one tag and value,  
`system.tag.writeBlocking(['[default]testingscheduledscript'], [52])`

You can get away without the `[]` list brackets when there is only one element but, as far as I know, it's undocumented and could change in future. Just write them as lists.

---

<div class="post-metadata">

**Author:** ![imdmacs](https://avatars.discourse-cdn.com/v4/letter/i/7feea3/32.png) [@imdmacs](https://forum.inductiveautomation.com/u/imdmacs)\
**Post date:** [November 11, 2024, 11:58pm UTC](https://forum.inductiveautomation.com/t/changing-opc-tag-value-based-on-time/98480/9 "2024-11-11T23:58:15Z")

</div>

I appreciate everyone who's contributed to helping me with this, i can see now how i was trying to approach this earlier wasn't going to work, given the examples you've provided and what's outlined in the Docs link i feel i have something now that should work. I have to be missing some critical step somewhere? Both attempts shown below failed to change the value.

 ![LORD0](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/6/9650ffd56edcba413d20eff49e0a8773ee54f30d.png)  
 ![LORD1](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/4/e4ccc06055356d35bd4129254d727c1896bcd150.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:** [November 12, 2024, 12:16am UTC](https://forum.inductiveautomation.com/t/changing-opc-tag-value-based-on-time/98480/10 "2024-11-12T00:16:28Z")

</div>

Let's see your settings tab.

Did you wait until the scheduled event to see the change?  
Have you removed any other scripts or bindings that might be writing to that tag?

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [November 12, 2024, 1:59pm UTC](https://forum.inductiveautomation.com/t/changing-opc-tag-value-based-on-time/98480/11 "2024-11-12T13:59:08Z")

</div>

Also, if shift change is variable we may need to change where this script is executed.

---

<div class="post-metadata">

**Author:** ![imdmacs](https://avatars.discourse-cdn.com/v4/letter/i/7feea3/32.png) [@imdmacs](https://forum.inductiveautomation.com/u/imdmacs)\
**Post date:** [November 12, 2024, 3:31pm UTC](https://forum.inductiveautomation.com/t/changing-opc-tag-value-based-on-time/98480/12 "2024-11-12T15:31:19Z")

</div>

Transistior -  
I've been trying to test it by setting the time 4mins out from system time, and waiting to see a change.  
There are no other scripts/bindings or alarms with this tag, the tag is tied to a numeric text field in two separate projects, one project will be open at multiple locations in the facility.  
amarks -  
The issue with the shift change is more of a we cant count on people to adjust this value before startup and fill all our our machines with mud.

 ![LORD2](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/7/17e8a15988e2ba4de8d48bd24d3092a02746aba1.png)  
 ![LORD3](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/8/28fb1187021e78149bd44927a310f18977b77555.png)

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [November 12, 2024, 3:33pm UTC](https://forum.inductiveautomation.com/t/changing-opc-tag-value-based-on-time/98480/13 "2024-11-12T15:33:33Z")

</div>

Check your gateway and see the status of the gateway script execution.

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

---

<div class="post-metadata">

**Author:** ![imdmacs](https://avatars.discourse-cdn.com/v4/letter/i/7feea3/32.png) [@imdmacs](https://forum.inductiveautomation.com/u/imdmacs)\
**Post date:** [November 12, 2024, 4:40pm UTC](https://forum.inductiveautomation.com/t/changing-opc-tag-value-based-on-time/98480/14 "2024-11-12T16:40:51Z")

</div>

dkhayes117 -  
You're definitely on to something here, i have no scripts listed under any of the tabs on the gateway (the script in question would be the only existing script we have).  
What would cause this script to not actually make it to the gateway?

 ![LORD4](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/7/57d2af3dccf7e1ea8a24f3d503e48535202d9ed5.png)

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [November 12, 2024, 4:55pm UTC](https://forum.inductiveautomation.com/t/changing-opc-tag-value-based-on-time/98480/15 "2024-11-12T16:55:38Z")

</div>

did you save the project after creating the script?

---

<div class="post-metadata">

**Author:** ![imdmacs](https://avatars.discourse-cdn.com/v4/letter/i/7feea3/32.png) [@imdmacs](https://forum.inductiveautomation.com/u/imdmacs)\
**Post date:** [November 12, 2024, 4:57pm UTC](https://forum.inductiveautomation.com/t/changing-opc-tag-value-based-on-time/98480/16 "2024-11-12T16:57:26Z")

</div>

Turns out it was my fault it wasn't showing up or running, i was trying to test it before making it permanent and i hadn't saved the project, once i saved the project the script showed up where it should be. I have it set to try and change the value here in a few minuets.

 ![progress1](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/8/f8ee6762875f8e44cb0db3b0e42ee77a6eacbb36.png)

---

<div class="post-metadata">

**Author:** ![imdmacs](https://avatars.discourse-cdn.com/v4/letter/i/7feea3/32.png) [@imdmacs](https://forum.inductiveautomation.com/u/imdmacs)\
**Post date:** [November 12, 2024, 6:34pm UTC](https://forum.inductiveautomation.com/t/changing-opc-tag-value-based-on-time/98480/17 "2024-11-12T18:34:53Z")

</div>

Just want to report back everything worked as intended, thank you all for your help.

 ![progress2](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/8/284f364f87428b332da31be8ea75cc26e47faf86.png)

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [November 12, 2024, 6:58pm UTC](https://forum.inductiveautomation.com/t/changing-opc-tag-value-based-on-time/98480/18 "2024-11-12T18:58:53Z")

</div>

A little extra information on this. These scripts live and execute on the gateway, so they have gateway scoping. Therefore, they don't exist on the gateway until the project is saved.

Best practice is to create project scripts and call those in the gateway events due to legacy scoping weirdness. I.E. it's best not to write your logic in the event itself.
