Date comparison in TagChange script

Good morning,

I am trying to do a simple script but to work with dates in Jython is driving me nuts.
I only want to write the current date in a dateTime Tag only if this Datetime Tag is 0 (I mean, 1970-01-01 ....)

I tried several things such as:

but the If clause is not working.

I tried also with == '1970-01-01 00:00:00 am'
or even getting the YEAR and comparing it with the int 1970.
None of the mentioned has worked...

Can anyone give me light on this doom question?

Thanks a lot

Even this is not working, how is this possible?

if currentValue.value <> previousValue.value and currentValue.value and not initialChange:
	TimeHoist= system.tag.readBlocking("[.]HoistingTime")[0].value
	if str(system.date.getYear(TimeHoist)) == '1970':	
		system.tag.writeBlocking("[.]HoistingTime", system.date.now())

image

One possible issue is this:

if currentValue.value <> previousValue.value

on the first execution previousValue will be None and trying to dereference value will fail.

Can you add some print statements that print the value of TimeHoist before doing the comparison?

Thanks Kevin, of course I can, but where can I see the print results if I program it on a TagChange script?

In the gateway log files (wrapper.log), or in the gateway logs webpage if you use system.util.getLogger

1 Like

In your if statement, the check of not initialChange needs to be first, to prevent null pointer errors on previousValue.

1 Like

I would move the not initialChange to the front of the ifs. That would catch the Nones on first execution.

1 Like

To avoid wrong diagnose, I deleted the first if, now the code looks like a simple if:

def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	TimeHoist= system.tag.readBlocking("[.]HoistingTime")[0].value
	
	if str(system.date.getYear(TimeHoist)) == '1970':	
		system.tag.writeBlocking("[.]HoistingTime", system.date.now())

even without the first if, the script is not working :frowning:

Why convert it to a string? Just compare the returned integer value to an integer date?

if system.date.getYear(TimeHoist)) == 1970:

Also, you're changing this script in a UDT definition, are you sure that there is an actual instance tag of the UDT defined and changing?

Finally, debugging with the Logger, I found that the default value is not "1970" but "1969".

In the tag tree the value is shown as "1970" but the gateway interpret the default DateTime as "1969".

Now I compared with "1969" and works fine.

thanks all for your help!!

Perhaps, to be less fragile and less inscrutable for the next person behind you:

def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	TimeHoist= system.tag.readBlocking("[.]HoistingTime")[0].value
	
	if system.date.getMillis(TimeHoist) == 0:	
		system.tag.writeBlocking("[.]HoistingTime", system.date.now())

Or if that doesn't work:
if TimeHoist.time == 0:

1 Like

That's what I would use.

That's an artifact of living in a western time zone. Checking the milliseconds avoids time zone problems.

3 Likes

Great!! thanks a lot!! now much more clearer for the future reader :slight_smile: