Created a new Memory Tag and in the Scripting / Value Changed i entered
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
import system.tag
paths = ["[.]BodyMaker_Auto_Stop"]
calculations = ["DurationOn"]
startDate = system.time.now().minusHours(10) # last 10 hour
endDate = system.time.now()
result = system.tag.queryTagCalculations(
paths=paths,
calculations=calculations,
startDate=startDate,
endDate=endDate
)
# result will have a column "DurationOn" with the total time ON in seconds
The value in this tag will not change even though the path Tag is changing.
If I understand correctly, you placed a Value Changed script on a memory tag and are expecting it to update its own value whenever another tag's value changes? If so, this is incorrect. A Value Changed script executes when there is a change in value of the tag on which it is placed. So, you would need to put this script on the "BodyMaker_Auto_Stop" tag and then write the result to the new memory tag using system.tag.writeBlocking(). Or, if possible, find a way to perform the calculation you want and make the new tag an expression or derived tag.
I looked through Inductive Automation University and Googled how to Get Duration ON from a binary tag into an integer tag so i could see the amount the tag was ON for the last 10 hours. It lead me to believe all i needed to do was copy the code and then replace the target tag name. Yes, i'm barely a beginner so really need the guidance. I think you can get an idea of what i'm trying to do so any suggestions are welcomed
Since you want it only for 10 hours, you're going to have use history. Turn on history on the boolean tag. Then you can query history for the amount of time the tag is "on". I think this is what that script of yours was trying to do.. but doing it completely horribly.
OR
You'd have to create a dataset tag where you append start/stop times, and then query against that and script the calc.
I have had the history enabled in the tag that is "Bodymaker_Auto_Stop".
I have another Tag (memory) named "BMs_Down_last10Hr". i want to pull the Duration ON for the last 10 hours from "Bodymaker_Auto_Stop" and put that time value in "BMs_Down_last10Hr" and the update can be several seconds if needed. Want to know the best way to accomplish this.
Does it need to go into scripting or do i put this in the "Bodymaker_Auto_Stop" event handler and have it move those values on value changed?
Querying and aggregating the tag history every time the source tag changes might be a little too much to do in a tag change script. You might be better off putting the update in a gateway tag change script or maybe a gateway timer script that runs every minute or every few minutes.
Inside your script you want to use a system.tag.queryTagHistory with an aggregation mode of "DurationOn".
There's examples of how to use the functions on the linked manual pages. This is a small enough piece that I would recommend you to try to put it together yourself before sic'ing the AI on it, it's a good learning opportunity.
EDIT: Meant timer script, not scheduled script. Just saw you wanted an update every few seconds which is very doable with a timer script.
This reads like an XY problem. Why do you want to get the Duration ON into an integer tag? If it is only for display purposes then the aggregation should occur on the UI.
Since you are using tag history, you've already got the data there is no reason to then store that in a tag. Do the aggregation when you need it, not indefinately.
I'm having trouble coming up with a reason to do this aggreagation when no one is looking at it.