Is there a way to add a expression to a tag change scripts to say the tag equals this setpoint? Then once the tag reach’s a setpoint I want to run the script then wait 2 weeks before the script runs again even if It reaches setpoint again during that 2 week period.
I would use a memory tag of type datetime that holds a “not before” value. Any tag change that would otherwise meet your criteria would then check that tag. If current time is after that tag value, update it with a new value two weeks in the future, and run the rest of the script.
I have created my memory tag but I am lost from there.
my memory tag is [default]TestDateTime
import system if system.tag.read ("[default]Test123").value ==1: #logger = system.util.getLogger("Gateway Timer Printer Script") #logger.info("script started") ###remove when not testing script###system.util.execute( ['C:\\PDFtoPrinter\\PDFtoPrinter.exe', "\\\\amifile\\Departments\\Work Instructions and Procedures\\Forms\\QD 717 Daily Furnace baghouse pm.PDF", "\\\\ami-print\\PLC-Office"]) #logger.info("Script ended")
Like Phil said, the easiest way to achieve this is going to be writing the date the logic executes on to a memory tag and using that tag for comparison down the road. Here’s some code that will achieve the functionality you are looking for…
import java.util.Calendar as Calendar #Read the current datetime from the gateway currdt = system.tag.read("[System]Gateway/CurrentDateTime").value #Create calendar instance of current datetime gateway tag to use in comparison statements cal = Calendar.getInstance() #Sets calendar instance date and time equal to your current gateway date and time cal.setTime(currdt) #Read the value stored in your memory datetime tag. Replace path with your tag path.. testdtpath = "[default]Testing/TestDateTime" testdt = system.tag.read(testdtpath).value #If memory tag value is null, assumes that this is the first time the script has run if testdt == None: datetest = True #If memory tag has a value, creates a new calendar instance, sets it equal to the datetime value in your memory tag, and then tests to see if the current gateway datetime is after the memory tag datetime value else: testdtcal = Calendar.getInstance() testdtcal.setTime(testdt) datetest = cal.after(testdtcal) #Reads value of Test123 tag. Replace path with your tag path.. test123 = system.tag.read("[default]Testing/Test123").value #Tests conditions for execution. If the test123 tag equals 1, and the current gateway datetime is after the memory tag datetime, we execute the logic. if (test123 == 1) and datetest: #Modifies calendar variable to write new "Next completion date" value to the TestDateTimetag. In this example, adds 14 days to the date that the script condition executes on. cal.add(Calendar.DAY_OF_MONTH, 14) #Obtains the new time 14 days after the condition is true in a date format. nextcompletiondate = cal.getTime() #Writes the next date that the script should execute on to the tag "TestDateTime" system.tag.write(testdtpath,nextcompletiondate) #Rest of your logic/code goes here....
Something like this (I would delegate to a script module):
tagpaths = ['[default]'+y for y in ['Cond1', 'Cond2', 'NotBefore']] def conditionalRun(event): cond1, cond2, notbefore = [x.value for x in system.tag.readAll(tagpaths)] # Check run permissives if cond1 and cond2: # Check Not Before criteria now = system.date.now() if now.before(notbefore): return system.tag.write(tagpaths[-1], system.date.addDays(now, 14)) # Do actions that must not repeat faster that every two weeks.