# system.tag.queryTagHistory - Ignoring bad values & needing last known value

**URL:** <https://forum.inductiveautomation.com/t/system-tag-querytaghistory-ignoring-bad-values-needing-last-known-value/104909>\
**Category:** Ignition\
**Tags:** ignition81, scripting, database\
**Created:** [April 9, 2025, 4:20pm UTC](https://forum.inductiveautomation.com/t/system-tag-querytaghistory-ignoring-bad-values-needing-last-known-value/104909 "2025-04-09T16:20:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![r123](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@r123](https://forum.inductiveautomation.com/u/r123)\
**Post date:** [April 9, 2025, 4:20pm UTC](https://forum.inductiveautomation.com/t/system-tag-querytaghistory-ignoring-bad-values-needing-last-known-value/104909/1 "2025-04-09T16:20:09Z")

</div>

We don't want the values with bad quality to be added to the `chunk_history`. We also need to return a value for all tags even if it's the last known value outside the start and end dates. We use `includeBoundingValues=True` but it looks like the `ignoreBadQuality=1` is then ignored. No option for last known value with good quality?

The tags are all setup like this:

```python
	Deadband Style Discrete
	Deadband Mode Absolute
	Sample Mode OnChange
	Min Time Between Samples	1 Minute
	Max Time Between Samples	1 Hour

```

```python
	# Loop through the folderPathArray in chunks
	for i in range(0, len(folderPathArray), chunk_size):
		chunk = folderPathArray[i:i+chunk_size]
		
		# Query tag history for the current chunk
		chunk_history = system.tag.queryTagHistory(
			paths=chunk, 
			returnFormat="Tall",
			startDate=startTime,
			endDate=endTime,
			returnSize=-1,
			#includeBoundingValues=False,	# ← Needs to be False so that random values are not added when there is no sample in the time specified.
			includeBoundingValues=True, # ← Needs to be True so that all tags POST all the time???? But now you get Bad Quality samples, ignoreBadQuality is ignored.
			aggregationMode="LastValue",	# ← Only works with returnformat Wide?
			ignoreBadQuality=1,
			noInterpolation=1
		)

```
