# Writing 0 value to DB

**URL:** <https://forum.inductiveautomation.com/t/writing-0-value-to-db/107766>\
**Category:** Ignition\
**Tags:** scripting, ignition81, perspective\
**Created:** [June 25, 2025, 12:50pm UTC](https://forum.inductiveautomation.com/t/writing-0-value-to-db/107766 "2025-06-25T12:50:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anant\_Upadhyayula](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@Anant\_Upadhyayula](https://forum.inductiveautomation.com/u/Anant_Upadhyayula)\
**Post date:** [June 25, 2025, 12:50pm UTC](https://forum.inductiveautomation.com/t/writing-0-value-to-db/107766/1 "2025-06-25T12:50:28Z")

</div>

Here is the set up I have. I have a tag for currentShift, which changes between `1, 2 3 and back to 1`. Starting at 7:00 AM, Below script run on the tag value change. When 1 becomes 2, I write the data to DB and reset the shift counts values to 0. The behavior I am seeing is, the OEE data written to DB is 0. Possibly the values were reset to 0 before the DB write is complete. is there a better way to handle this. Can a small delay be added to ensure the data is written to DB before resetting the values?

```python

	if initialChange or currentValue.value == previousValue.value:
		return # Skip on initial load or no actual change
		
	try:
		# Cache new shift start time first
		newStart = system.date.now()
		
		shiftNumber = previousValue.value # Save the shift we are finishing

		tagPaths = [
			"[.]../../Performance/OEE/OEE",
			"[.]../../Performance/OEE/Availability",
			"[.]../../Performance/OEE/Performance",
			"[.]../../Performance/OEE/Quality",
		]

		# Read all values at once
		tagValues = system.tag.readBlocking(tagPaths)
		#params = [tv.value for tv in tagValues]
		params = [shiftNumber] + [tv.value for tv in tagValues]
		# Build MS-SQL insert
		query = """
		INSERT INTO OEE_Data (
			shiftNumber, OEE, Availability, Performance, Quality
		) VALUES (?, ?, ?, ?, ?)
		"""

		# Execute insert
		system.db.runPrepUpdate(query, params, "OEE_Data")
		
		# Reset tags to "0" at the start of a new shift
		system.tag.writeBlocking([
		"[.]../../Current/ProductionData/ShiftCounts/shift1Count",
		"[.]../../Current/ProductionData/ShiftCounts/shift2Count",
		"[.]../../Current/ProductionData/ShiftCounts/shift3Count",
		"[.]../../Current/ProductionData/ShiftCounts/shiftCount",
		], [0]*4)
	except Exception as e:
		system.util.getLogger("OEE_Data").error("Failed to write shift data: " + str(e))

```

---

<div class="post-metadata">

**Author:** ![Ryan\_Deardorff](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan_deardorff/32/63815_2.png) [@Ryan\_Deardorff](https://forum.inductiveautomation.com/u/Ryan_Deardorff)\
**Post date:** [June 25, 2025, 1:54pm UTC](https://forum.inductiveautomation.com/t/writing-0-value-to-db/107766/2 "2025-06-25T13:54:53Z")

</div>

You aren't passing `shiftNumber` into that first query as a parameter, only `params` which contain the four values read from tags.

---

<div class="post-metadata">

**Author:** ![Anant\_Upadhyayula](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@Anant\_Upadhyayula](https://forum.inductiveautomation.com/u/Anant_Upadhyayula)\
**Post date:** [June 25, 2025, 2:20pm UTC](https://forum.inductiveautomation.com/t/writing-0-value-to-db/107766/3 "2025-06-25T14:20:00Z")

</div>

thats my bad, I updated the code, I had the shiftnumber, in the stripped down version I forgot to keep that.

---

<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 25, 2025, 4:22pm UTC](https://forum.inductiveautomation.com/t/writing-0-value-to-db/107766/4 "2025-06-25T16:22:45Z")

</div>

> [@Anant\_Upadhyayula](#):
>
> Possibly the values were reset to 0 before the DB write is complete. is there a better way to handle this.

This isn't happening with the script you've posted. The tag values you're reading are immutable; they're not live references back to the actual tag value. A `QualifiedValue` containing `1` from a tag read will continue to contain `1`, even if you write to the tag again.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [June 25, 2025, 5:03pm UTC](https://forum.inductiveautomation.com/t/writing-0-value-to-db/107766/5 "2025-06-25T17:03:51Z")

</div>

Log the values right before the query, to make sure they are what you think they are.  
Once you confirm that the values inserted in the table are not the values you pass as parameters, we can start trying to figure out why.
