Checking date/time using python

Hello,

I am new to python scripting and am wondering if anyone could with something I am trying to do. The goal is to write a value (1) to a tag on every monday at 8 AM and once this has happened once, to return the value back to a zero. I am trying to execute this using script running in the gateway. Is this possible to do?

Hi there.
It is indeed possible.
What you can do is create a boolean expression tag which will be used as a trigger becoming true every monday at 8 AM.
Put this expression in your trigger boolean tag, you can call it trigger:

getDayOfWeek(now())=2&&getHour24(now())=8

Create a memory tag (you can call it target).
Once the trigger becomes true, it will execute a Tag Change Gateway Event Script that will write value (1) in the target tag using this script :

system.tag.write("target",1)

This action will just set your target tag to 1, if you want to reet it to 0, you will have to use the invokeLater function in order to add a delay to your script :
https://docs.inductiveautomation.com/display/DOC79/Adding+a+Delay+to+a+Script

In this example, the script writes a value 1 in a tag and resets the value after 3 seconds :

def reset_tag():
    system.tag.write("target",0)

system.tag.write("target",1)
system.util.invokeLater(reset_tag, 3000)```
1 Like

If you are wanting to keep it in a Gateway Script you can try this approach. The only thing outside the gateway script is the tag itself. Just remember to change the wday to 0 for monday, and the hour to 8.

I keep getting this error when using the example script from above, not sure what I am doing wrong here? I appreciate the help though!

You’re using the OPC item path while the function is expecting the TagPath.

Right click on the tag you want to write to in the Tag Browser of the designer, and select Copy Tag Path, then paste that in for the tag path in the system.tag.write function in your script.

The tag path is a string, so you will need to put it in quotes. Something like this:

system.tag.write('Your Tag Path Here')

Thank you for all the help so far, I am still unable to get this to change values of the tag that I am trying to write to. In the picture I am trying to get this to change values today, wednesday at 12 noon (EST time over here) but I was unable to get this working. Is there anything I need to do to the tag that I have imported or the project for this to be successful?

Don’t use python’s time module. Ignition is written in java, and its datetime objects are actually instances of java.util.Date. All of Ignition’s system.date.* functions will give you this data type. Use them.

1 Like