Getting tag's value from 8 hours ago

I am wanting to get a tags value from 8 hours ago and store it as another tag. When writing the script in the script console, it works. But I need some help making this work when creating a new tag. I want this value to move exactly 8 hours behind the current value.

Here is the code-

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


tag_path = "[default]Mill 2/XV2_5_RunHrsReal"
history = system.tag.queryTagHistory(paths=[tag_path],
                                      startDate=eight_hours_ago,
                                      returnSize=1)

if history:
      	 value = history.getValueAt(0, 1) 
print(value)

Thanks for any help.
Derrick

Have you tried implementing a Tag ValueChange event? Tags paths can be acces relative to the current yah path.

No, I am working on uptime report so I am wanting the run hours 8 hours ago and then compare to current run hours. This number constantly changes. I haven't spent alot of time scripting so I am new most things.

I don't understand what this means. Could you elaborate?

This code works in the script console, but not when creating a new Expression tag. I am trying to put the value from 8 hours ago into a tag and use that tag in a report.

So you're trying to put the script into an expression tag for use in the reporting Module?

You don't need a tag. Just use a Tag Historian Query in the report?

2 Likes

Yeah, I have it setup like this currently but if the value does not change then the range value will be the same as the current value. So let say the hour meter is at 900.1 hours 8 hours ago and 900.1 currently, the range should be 0, but it will be 900.1.. But if I get this value and use it as a tag, then I can use that tag in other places.

As an expression tag and then use it in a report. That way I can do more with it than the one report.

Place the script in a project library.

In the report, you can call that script in a Script Datasource. Anywhere else that you may need that data, you can call that script.

There is no need for an additional tag.

4 Likes

Ok. Tested this code again on script console and it gives the value I need. I added to script library, went to gateway settings- gateway scripting project, set that up. In project library created a new script "Mill1". I created and expression tag and in the expression just used runScript("Mill1") and if I set the data type of the tag to Float I get nothing. If I set to a string Data type, the value is "module Mill1 at 2"

Assuming you're dead-set on making it a tag (may be redundant, I'm not sure if Ignition would cache the historian query anyway here):

You're trying to run a script module when you should be running a function in that module

Inside Mill1 should be:

def getOldValue(tag_path):

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

	history = system.tag.queryTagHistory(paths=[tag_path],
                                      startDate=eight_hours_ago,
                                      returnSize=1)

	if history:
		return history.getValueAt(0, 1)
	else:
		return None #will probably cause an error but you'll know something's up

and then your runScript should be
runScript("Mill1.getOldValue", 1000, "[default]Mill 2/XV2_5_RunHrsReal")

where 1000 is your refresh rate in milliseconds.

Let's see the script library implementation

I don't believe runscript is the correct approach, but even if I'm wrong, I'm certain the library script is not being called correctly here. What is your function's name?

1 Like

That works.

Thank you!

Tip: 1000 ms is probably excessive. I would imagine that once a minute (60000 ms) would be more than adequate.

1 Like

**** Jumps on Soap Box ****

You just don't need a tag.

Sure, it's just one tag, not a huge deal, but 1 tag quickly turns into hundreds. Now you're adding excess load on your gateway and bloating your internal DB, this will eventually lead to performance issues.

You're duplicating data. Just call the library script directly from wherever you actually need the data (report, vision window, perspective session...etc).

Okay, I've said my piece, do as you will.

**** Steps down from Soap Box ****

5 Likes

lrose, is Markdown missing <rant> and </rant> tags?

4 Likes

It works.

It's more of a question of "best practice" vs "works". There's lots of ways to get something done in Ignition but some of them will make your (or your successor's) life harder down the line.

Think about how often you actually need the data to update, how many machines you'll be doing this with, et cetera.

2 Likes

I'll keep that in mind. For this situation, it will be fine. I just needed a couple of tags and they're in their own folder and labeled. Thanks again.