Run a script at a specific time daily

inductiveautomation.com/forum/v … 50&t=15912

Regarding the topic above which is now locked (probably because I prematurely marked it as solved?) it seems that my script runs successfully only once and never again, unless I edit the tag change script. I can edit it in any way and then it works again…once and only once.

My logic is (was) nearly identical to the suggestion except that I made my expression tag equal to Hour x 100 + Minute. So 2359 is 11:59 pm. And I am looking to run the script at 11:59 pm, so instead of:

if not initialChange and newValue.value==0:
project.myScript.myFunction()

I had:

if not initialChange and newValue.value==HourRPT:
project.MakeRPT

Where in my project HourRPT is a memory tag containing the value at which time I want to trigger the call. This lets me try things out without messing with the PC clock or waiting until a minute til midnight to test it.

I have tried replacing newValue.value with currentValue, currentValue.value, and several other modifications in an attempt to solve this but the same problem remains. It works after I make any change and never again…

My last attempt replaced my memory tag with >= a constant 2355 (since I was up til almost midnight messing with it) and I did away with the not Initialchage part. So it should have run the tag change script once every minute and should have passed the “if” test five times at 2355, 2356, 2357, 2358, and 2359, but it didn’t. It ran once at 2355…

I can call my MakeRPT script with a button or from the script console and it works (it creates a text file with some tag data in certain physical locations). So somehow the logic I am using is only deemed true once.

When I monitor the Gateway script in the browser, it appears that it does indeed run every minute on the dot. I am stumped trying to figure out why the if test seems to only go true once.

Here’s what I have right now (but the behavior is identical to all the other attempts I have made):

if currentValue >= 2355:
project.MakeReportRP

And, my tag named “Hour” does show the correctly calculated value and changes each minute like I’d expect.

Any ideas?

Please post your actual, complete event script, and use the code block so we can read it easily. It’s unclear to me how you are getting the values in your “if” expression from the variables the event supplies. Consider using system.util.getLogger(…).info(…) before the if statement to send debugging details from the event to the console.

I’ve put it back to what I had originally:

if newValue.value >= system.tag.read("HourRPT").value: project.MakeReportRP

I just altered it to this:
EDIT: and it needs more work…I have a lot to learn…

[code]logger=system.util.getLogger(“MakeReport”)

x=newValue.value
y=system.tag.read(“HourRPT”).value

logger.info(x)
logger.info(y)

Screenshot of my tag data:

if x >= y:
project.MakeReportRP
logger.info(“Made one”)[/code]

Okay, after monitoring the console, I figured out how to “logger” my values. Here is my tag change script:

[code]logger=system.util.getLogger(“MakeReport”)

x=newValue.value
y=system.tag.read(“HourRPT”).value

logger.info(str(x))
logger.info(str(y))

if x >= y:
project.MakeReportRP
logger.info(“Made one”)[/code]

I edited the value of HourRPT so that it should make a new report every minute after 635 am. The file it creates uses the system time as part of the filename so I should see the file attributes change each minute.

This is what my logger info shows in the console. It shows the two values, and “Made One” telling me that the if statement passed its test.

The script project.MakeReportRP should create the file whether or not it already exists and then add the text to the file. It seems the tag change script does execute every minute, so maybe I need to dig into the nuts and bolts of the MakeReportRP script to figure out what might be causing it not to create a file.

I just deleted the file 3 minutes ago and still there is no new file being created…

Thanks for the tip on the logger.info

[quote=“OkiePC”] project.MakeReportRP[/quote]Here’s your problem. This doesn’t run the script. You must define a function within MakeReportRP that contains your functionality, and call it like so: project.MakeReportRP.myFunction()Scripts and script modules are actually run only once when loaded or edited – they are imported into either global or project scope. The code object and anything it defines then persists. That’s why it would only run once per edit for you.

Thank you.

I edited my script and put def daily(): as the first line and then indented the rest of the script, and appended my call in the tag change script with .daily() and it has now created the text file every minute since this change.

I’m going to set my HourRPT time back to one minute til midnight and will report back here after a few days of watching this run without any further alterations and mark it solved.

Thanks again for furthering my understanding on this.