# addSeconds in Expression scripting not working with Tag value

**URL:** <https://forum.inductiveautomation.com/t/addseconds-in-expression-scripting-not-working-with-tag-value/90152>\
**Category:** Ignition\
**Tags:** scripting, ignition81, perspective\
**Created:** [May 9, 2024, 12:10pm UTC](https://forum.inductiveautomation.com/t/addseconds-in-expression-scripting-not-working-with-tag-value/90152 "2024-05-09T12:10:11Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luke\_Smith1](https://avatars.discourse-cdn.com/v4/letter/l/c89c15/32.png) [@Luke\_Smith1](https://forum.inductiveautomation.com/u/Luke_Smith1)\
**Post date:** [May 9, 2024, 12:10pm UTC](https://forum.inductiveautomation.com/t/addseconds-in-expression-scripting-not-working-with-tag-value/90152/1 "2024-05-09T12:10:11Z")

</div>

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

Hi everyone, hopefully this question will be easy for you guys!

In a tag script I am trying to add an amount of time(as an integer) to the current time, when a process starts. See my code above. This code works as intended when the highlighted code is replaced with a hardcoded integer. It does not work when I try to use a tag value.

To recap, When the "lift raised" tag goes from 0 to 1, I want to write the current system time to CycleStartTime and I also want to add StationCycleTime to current system time and write the sum to CycleEndTime.

I have been stuck on this for a while and am wondering if it is even possible. maybe I can't reference other tags inside of a tag script?

Any help would be greatly appreciated!  
Luke M. Smith

---

<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:** [May 9, 2024, 12:17pm UTC](https://forum.inductiveautomation.com/t/addseconds-in-expression-scripting-not-working-with-tag-value/90152/2 "2024-05-09T12:17:50Z")

</div>

![Not a tag](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/b/8b0ccad1e3387f93d9d9f3298ce4bbd52ab31a04.png)

That's not a tag value. That's just a string.

---

<div class="post-metadata">

**Author:** ![Luke\_Smith1](https://avatars.discourse-cdn.com/v4/letter/l/c89c15/32.png) [@Luke\_Smith1](https://forum.inductiveautomation.com/u/Luke_Smith1)\
**Post date:** [May 9, 2024, 12:21pm UTC](https://forum.inductiveautomation.com/t/addseconds-in-expression-scripting-not-working-with-tag-value/90152/3 "2024-05-09T12:21:54Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/0/303970c1da04ba327cfe6767a873fd3b66f7aede.png)  
This is how it shows up when I click the "insert tag" icon. Also it seems to work with that syntax for the other tags in the script. How would I do it then? {} ?

---

<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:** [May 9, 2024, 12:35pm UTC](https://forum.inductiveautomation.com/t/addseconds-in-expression-scripting-not-working-with-tag-value/90152/4 "2024-05-09T12:35:39Z")

</div>

Post your code (rather than a picture of your code) so we don't have to type it out. See [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831).

system.tag.writeBlocking's syntax is  
`system.tag.writeBlocking([list-of-tag-paths], [list-of-values])`

It runs through the list of tag paths and does a write to each of those using the corresponding value from the list of values. You need to get that value using `system.tag.readBlocking([list-of-tag-paths])` first.

---

<div class="post-metadata">

**Author:** ![Luke\_Smith1](https://avatars.discourse-cdn.com/v4/letter/l/c89c15/32.png) [@Luke\_Smith1](https://forum.inductiveautomation.com/u/Luke_Smith1)\
**Post date:** [May 9, 2024, 12:49pm UTC](https://forum.inductiveautomation.com/t/addseconds-in-expression-scripting-not-working-with-tag-value/90152/5 "2024-05-09T12:49:36Z")

</div>

Good Call. Thanks for the link!

```python
if previousValue.value != currentValue.value and initialChange == False and currentValue.value == 1:
		system.tag.writeBlocking(["[default]Station/ACell_15/Stations/Unload/CycleStartTime"], [system.date.now()])
		system.tag.writeBlocking(["[default]Station/ACell_15/Stations/Unload/CycleEndTime"], [system.date.addSeconds(system.date.now(), '[.]../StationCycleTime.value')])

```

So you're saying to do `system.tag.writeBlocking(["[default]Station/ACell_15/Stations/Unload/CycleEndTime"], [system.tag.readBlocking(system.date.addSeconds(system.date.now(), '[.]../StationCycleTime.value'))])`

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [May 9, 2024, 12:53pm UTC](https://forum.inductiveautomation.com/t/addseconds-in-expression-scripting-not-working-with-tag-value/90152/6 "2024-05-09T12:53:56Z")

</div>

I would suggest it like this:

```python
	if current.value and not initialChange:
		cycleTime = system.tag.readBlocking(['[default]Station/ACell_15/Stations/Unload/StationCycleTime'])[0].value
		start = system.date.now()
		end = system.date.addSeconds(start, cycleTime)
		system.tag.writeBlocking(['[default]Station/ACell_15/Stations/Unload/CycleStartTime', '[default]Station/ACell_15/Stations/Unload/CycleEndTime'], [start, end])		

```

---

<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:** [May 9, 2024, 12:59pm UTC](https://forum.inductiveautomation.com/t/addseconds-in-expression-scripting-not-working-with-tag-value/90152/7 "2024-05-09T12:59:07Z")

</div>

Jordan beat me to it (and did a better job). Note that there is only one writeBlocking call for both tags. This will be more efficient.

Yours wouldn't work because you are trying to read a tag called  
` system.date.addSeconds(system.date.now(), '[.]../StationCycleTime.value')`  
which is not a valid tag path.

---

<div class="post-metadata">

**Author:** ![Luke\_Smith1](https://avatars.discourse-cdn.com/v4/letter/l/c89c15/32.png) [@Luke\_Smith1](https://forum.inductiveautomation.com/u/Luke_Smith1)\
**Post date:** [May 9, 2024, 1:06pm UTC](https://forum.inductiveautomation.com/t/addseconds-in-expression-scripting-not-working-with-tag-value/90152/8 "2024-05-09T13:06:19Z")

</div>

@JordanCClark , seeing it written that way brought me joy. so much more beautiful than what I had. @Transistor , that makes sense. Thank you! I put Jordan's code in and it is not working. Neither CycleStartTime or CycleEndTime are being changed. Can you explain the [0] portion of this line?  
`cycleTime = system.tag.readBlocking(['[default]Station/ACell_15/Stations/Unload/StationCycleTime'])[0].value`

---

<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:** [May 9, 2024, 1:13pm UTC](https://forum.inductiveautomation.com/t/addseconds-in-expression-scripting-not-working-with-tag-value/90152/9 "2024-05-09T13:13:01Z")

</div>

[system.tag.readBlocking | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-tag/system-tag-readBlocking) returns a list of qualified values ([Scripting Object Reference | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/appendix/reference-pages/scripting-object-reference#qualifiedvalue)). These will be **value** , **timestamp** and **quality**. You are generally going to be interested in the value.

You've only read one tag but the function still returns a list so you need the first tag qualified value. So,  
`system.tag.readBlocking(['.../StationCycleTime'])[0]`  
will give you that.

Inside that you want the **value** so,  
`system.tag.readBlocking(['.../StationCycleTime'])[0].value`

You could also retrieve  
`system.tag.readBlocking(['.../StationCycleTime'])[0].timestamp`  
`system.tag.readBlocking(['.../StationCycleTime'])[0].quality`

Cool!

---

<div class="post-metadata">

**Author:** ![Luke\_Smith1](https://avatars.discourse-cdn.com/v4/letter/l/c89c15/32.png) [@Luke\_Smith1](https://forum.inductiveautomation.com/u/Luke_Smith1)\
**Post date:** [May 9, 2024, 1:13pm UTC](https://forum.inductiveautomation.com/t/addseconds-in-expression-scripting-not-working-with-tag-value/90152/10 "2024-05-09T13:13:42Z")

</div>

That makes sense, thank you!

Error I am getting  
Error executing script.  
Traceback (most recent call last):  
File "tagevent:valueChanged", line 6, in valueChanged  
AttributeError: 'com.inductiveautomation.ignition.common.script.Imm' object has no attribute 'WriteBlocking'

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [May 9, 2024, 1:19pm UTC](https://forum.inductiveautomation.com/t/addseconds-in-expression-scripting-not-working-with-tag-value/90152/11 "2024-05-09T13:19:03Z")

</div>

Sorry, make that a lower case 'w' on writeBlocking().

EDIT: updated above.

---

<div class="post-metadata">

**Author:** ![Luke\_Smith1](https://avatars.discourse-cdn.com/v4/letter/l/c89c15/32.png) [@Luke\_Smith1](https://forum.inductiveautomation.com/u/Luke_Smith1)\
**Post date:** [May 9, 2024, 1:19pm UTC](https://forum.inductiveautomation.com/t/addseconds-in-expression-scripting-not-working-with-tag-value/90152/12 "2024-05-09T13:19:57Z")

</div>

Works. I love you both and owe you both a coffee. Cheers!

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [May 9, 2024, 6:41pm UTC](https://forum.inductiveautomation.com/t/addseconds-in-expression-scripting-not-working-with-tag-value/90152/13 "2024-05-09T18:41:07Z")

</div>

Just thought of something here. A few cups of coffee will do that to a person. This script is on a tag. You should be doing an asynchronous write instead of a blocking one, as there is only-- by default-- three threads dedicated to those types of scripts. So, you want it to be as speedy as possible.

Change _writeBlocking()_ to _writeAsync()_

---

<div class="post-metadata">

**Author:** ![Luke\_Smith1](https://avatars.discourse-cdn.com/v4/letter/l/c89c15/32.png) [@Luke\_Smith1](https://forum.inductiveautomation.com/u/Luke_Smith1)\
**Post date:** [May 9, 2024, 6:50pm UTC](https://forum.inductiveautomation.com/t/addseconds-in-expression-scripting-not-working-with-tag-value/90152/14 "2024-05-09T18:50:54Z")

</div>

I'm on it. thank you!

---

<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:** [May 9, 2024, 7:02pm UTC](https://forum.inductiveautomation.com/t/addseconds-in-expression-scripting-not-working-with-tag-value/90152/15 "2024-05-09T19:02:01Z")

</div>

Also, you should do the check for initial change prior to any check of previous value. And, it should be `currentValue.value` not `current.value`

```python
if not initialChange and currentValue.value:
		cycleTime = system.tag.readBlocking(['[default]Station/ACell_15/Stations/Unload/StationCycleTime'])[0].value
		start = system.date.now()
		end = system.date.addSeconds(start, cycleTime)
		system.tag.writeAsync(['[default]Station/ACell_15/Stations/Unload/CycleStartTime', '[default]Station/ACell_15/Stations/Unload/CycleEndTime'], [start, end])

```

@Luke_Smith1

Honestly though, this should most likely be done in a Gateway Tag Change Event, rather than a Tag Value Change event, for performance reasons. Generally, you want any ValueChange script like this to execute in single digit ms.

You may also want to consider that it is possible that the value in the PLC/Device may not be updated when this event triggers so you could be reading stale data. Unless you have done something in the device to insure that this trigger tag is not updated until all needed data is ready. This is because `readBlocking()` reads the value of the tag in Ignition, not the value of the tag in the device. They are not always equivalent. There is potential for a race condition here.

---

<div class="post-metadata">

**Author:** ![Luke\_Smith1](https://avatars.discourse-cdn.com/v4/letter/l/c89c15/32.png) [@Luke\_Smith1](https://forum.inductiveautomation.com/u/Luke_Smith1)\
**Post date:** [May 9, 2024, 7:11pm UTC](https://forum.inductiveautomation.com/t/addseconds-in-expression-scripting-not-working-with-tag-value/90152/16 "2024-05-09T19:11:04Z")

</div>

Hi lrose,

Thank you for the input, this is my first ignition project in years so I am relearning everything as I go. Where and how to script to be the most efficient is where I am struggling the most, so your comment is very helpful. Thank you! I will try to switch it over to a Gateway Tag Change Event.
