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)
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.
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.
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.
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")
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?
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).
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.