Pulling value from history enabled tag, improper practice or error?

I have a history enabled tag, I am attempting to write a script to print the value the tag has currently, as well as the value it had earlier in the day. However, the value for earlier always is returned as "None" though I am certain there is data available. The code I am using is as follows

def retailCompare():
    eight_hours_ago = system.date.addHours(system.date.now(), -8)


    tag_path = system.tag.readBlocking("[default]tagnamechangedforprivacyisuppose")
    history = system.tag.queryTagHistory(paths=[tag_path],
                                        startDate = system.date.addHours(system.date.now(), -8),
                                        endDate = system.date.now(),
                                        returnSize = 1,
                                        aggregationMode = "Minimum",
                                        returnFormat = "Wide",)


    if history:
          	value = history.getValueAt(0,1)
          	value1 = tag_path[0].value
    print(value)
    print(value1)

This code when ran in the script console returns:


I can accurately pull the timestamp value at any time, but can't pull the value value unless I am accessing it's current value, what am I doing wrong?

You aren't passing a tag path to your history query, but the result of the readBlocking--a list of QVs. So the history query will look up the stringified version of that list as though it is a tag path, and won't find it, yielding null. (Look at the column name for that in the history dataset to see what I mean.)

Don't use the variable name tagpath for something that isn't a tag path.

So it should be:

history = system.tag.queryTagHistory("[default]CWS Retail/Bag Check Weigh - Line 5/Statistics/Current/Batch/Total Accepted Count"), startDate = , endDate =

And that should get me the actual value of the tag?

Also my apologies, I thought it was good practice to define tag paths like I did above, is that true for cases other than this or just generally not true to do so?

Okay yep changed that and it now works as expected, thank you.

I think I was erroneously following the docs for system.tag.queryTagCalculations where I mistook it using system.tag.queryTagCalculations(paths=['Historical Tag']

Thank you.

Sure, put tag paths (strings) into python variables. Reference them by varname in the multiple places needed.

I tend to use variations of qvList as the assignment name for the return value from any readBlocking() call, to remind me what its structure is.

1 Like

Potentially dumb followup here, I can now simply write this value to a new tag using system.tag.writeBlocking([path],[oldvalue]) and then enable history on it right?

That makes no sense to me. But try it.