# Manually storing historical data

**URL:** <https://forum.inductiveautomation.com/t/manually-storing-historical-data/10328>\
**Category:** Module Development\
**Created:** [August 10, 2015, 7:54pm UTC](https://forum.inductiveautomation.com/t/manually-storing-historical-data/10328 "2015-08-10T19:54:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![idelamer](https://avatars.discourse-cdn.com/v4/letter/i/e5b9ba/32.png) [@idelamer](https://forum.inductiveautomation.com/u/idelamer)\
**Post date:** [August 10, 2015, 7:54pm UTC](https://forum.inductiveautomation.com/t/manually-storing-historical-data/10328/1 "2015-08-10T19:54:52Z")

</div>

Hi,

We have a custom Tag Provider that receives data from field devices using a custom protocol, and uses the DBTagStore API to store values. Behaves much like a driving database provider.

Our field devices now have the capability to store data during a communication link outage, and forward the saved values when the link is re-established.

This would be for tags that are already configured to store history. We want to back-fill the gaps.

This is the code I’m trying, and it’s not working. It’s inside the Tag class (SimpleTag):

[code]void addHistoricalValue(Object value, Long timestamp)  
{  
TagValue historyEnabled = getAttribute(TagProp.HistoryEnabled);  
if ((historyEnabled != null) && historyEnabled.getValue().equals(true)) {  
HistoricalTagValue histValue = new PackedHistoricalTagValue(this.getTagPath(), this.getDataType().getTypeClass(), InterpolationMode.Discrete, TimestampSource.Value, new BasicTagValue(value, DataQuality.GOOD\_DATA, new Date(timestamp)));

```
	try {
		MyTagProvider.this.context.getHistoryManager().storeHistory(getAttribute(TagProp.PrimaryHistoryProvider).getValue().toString(), histValue);
	} catch (Exception e) {
		log.warn("Exception storing history: " + getName(), e);
	}
}

```

}  
[/code]

The console shows the following error:

[quote]TagHistoryDatasourceSink Unknown data type received by history sink, will not be stored: class com.inductiveautomation.ignition.gateway.history.PackedHistoricalTagValue  
[/quote]

I checked and the value and data type class seem OK. The value is of type Integer (Int4) and data type class accordingly.

Thanks for any support.

Ivan

---

<div class="post-metadata">

**Author:** ![Colby.Clegg](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/colby.clegg/32/3282_2.png) [@Colby.Clegg](https://forum.inductiveautomation.com/u/Colby.Clegg)\
**Post date:** [August 10, 2015, 8:33pm UTC](https://forum.inductiveautomation.com/t/manually-storing-historical-data/10328/2 "2015-08-10T20:33:34Z")

</div>

Hi,

I think the only thing you need to do is wrap up the tag value in a “ScanclassHistorySet” (BasicScanclassHistorySet would be the implementation you could use). The constructor is BasicScanclassHistorySet(providerName, scanclassName, execRate). The “provider name” should be name of the realtime tag provider. The scan class name doesn’t matter, as long as the execRate=0 (which you should do). Outside that, the BasicScanclassHistorySet is just a list of HistoricalTagValues, so add your value to it.

If it makes sense, for performance reasons you might try to group multiple values together into a single history set.

Regards,

---

<div class="post-metadata">

**Author:** ![idelamer](https://avatars.discourse-cdn.com/v4/letter/i/e5b9ba/32.png) [@idelamer](https://forum.inductiveautomation.com/u/idelamer)\
**Post date:** [August 11, 2015, 12:24pm UTC](https://forum.inductiveautomation.com/t/manually-storing-historical-data/10328/3 "2015-08-11T12:24:00Z")

</div>

Hi Colby,

Awesome! That did the trick.

And it actually suits our system, we receive a bunch of historical values at the same time, so we can forward all of them in a single BasicScanclassHistorySet object.

Cheers,  
Ivan

---

<div class="post-metadata">

**Author:** ![idelamer](https://avatars.discourse-cdn.com/v4/letter/i/e5b9ba/32.png) [@idelamer](https://forum.inductiveautomation.com/u/idelamer)\
**Post date:** [August 11, 2015, 7:13pm UTC](https://forum.inductiveautomation.com/t/manually-storing-historical-data/10328/4 "2015-08-11T19:13:18Z")

</div>

Hi Colby,

Just noticed something odd so I need to re-open this.

I noticed that when I send data using the code you suggested, the following happens:

- A new row is added to sqlth\_scinfo, a historical scan class named “_exempt_”
- When I send the historical data, the tag in sqlth\_te is retired
- A new tag is created in sqlth\_te, using the scan class id for “_exempt_”
- Then soon after this new tag is retired, and another one is re-created using the original scan class id

I though this was caused by using an empty string for scName in BasicScanclassHistorySet(), but even if I use the same scName (and setting the same poll rate, or with poll rate = 0), it will still post the historical values under a new tagid with this exempt scan class.

Is there any way to avoid this? It would be nice to keep all historical data under the same tagid.

Cheers  
Ivan

---

<div class="post-metadata">

**Author:** ![Colby.Clegg](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/colby.clegg/32/3282_2.png) [@Colby.Clegg](https://forum.inductiveautomation.com/u/Colby.Clegg)\
**Post date:** [August 11, 2015, 7:55pm UTC](https://forum.inductiveautomation.com/t/manually-storing-historical-data/10328/5 "2015-08-11T19:55:44Z")

</div>

Hi,

I didn’t realize you were also storing data normally through the tag as well (I thought it was purely manual). Yes, you’ll need to make sure that the scan class, data type, and interpolation mode all match up for the tag. I suppose there could also be some issue if you’re back filling, but it should just be those three things.

Take a look at how the various rows differ. Basically, the system loads the tag id, and if it doesn’t match on those three parameters (sc, data type, and interpolation mode), it adds a new one.

Regards,

---

<div class="post-metadata">

**Author:** ![idelamer](https://avatars.discourse-cdn.com/v4/letter/i/e5b9ba/32.png) [@idelamer](https://forum.inductiveautomation.com/u/idelamer)\
**Post date:** [August 11, 2015, 9:18pm UTC](https://forum.inductiveautomation.com/t/manually-storing-historical-data/10328/6 "2015-08-11T21:18:45Z")

</div>

I checked those, it turned out it was the TimestampSource, tag was set up for System and I was using Value.

Now it all matches and the history is stored for the same tagid.

Cheers  
Ivan

---

<div class="post-metadata">

**Author:** ![Colby.Clegg](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/colby.clegg/32/3282_2.png) [@Colby.Clegg](https://forum.inductiveautomation.com/u/Colby.Clegg)\
**Post date:** [August 11, 2015, 10:33pm UTC](https://forum.inductiveautomation.com/t/manually-storing-historical-data/10328/7 "2015-08-11T22:33:28Z")

</div>

Oh, yes, of course… I forgot that “Value” timestamp source also forces it to the “exempt” scanclass.

Regards,
