Gateway Timer Script to enable udt instance within a time frame and then disable outside that

Can Anyone help on checking if the script is correct, I have been struggling to make it work. below is the script. I wanted to enable multiple udt instances from 6:00am-7:00pm daily and then disabled them outside the specified time range. here is the script.

# Get current time and extract the hour (0-23 format)
    now = system.date.now()
    current_hour = system.date.getHour24(now)
    current_minute = system.date.getMinute(now)

# Define target path to your UDT instance
    udt_path = "[default]UDTInstancePath"

# Convert current time to total minutes from midnight for exact range check (6:00 AM = 360 min, 7:00 PM = 1140 min)
    total_minutes = (current_hour * 60) + current_minute
    is_within_time = (360 <= total_minutes < 1140)  # 6:00 AM to 7:00 PM

# Read current enabled state to avoid unnecessary writes
    current_state = system.tag.readBlocking([udt_path + ".Enabled"])[0].value

# Write new state only if it differs
    if current_state != is_within_time:
        system.tag.writeBlocking([udt_path + ".Enabled"], [is_within_time])

A little help please.

What problem are you attempting to solve via this approach? How is it failing, is the script just not toggling the enabled value at all, or is it toggling it at the wrong times? Are you getting an error in the logs when the script attempts to run?

You can try adding logging to various points using a logger from system.util.getLogger to verify that you are getting the values you expect at each point.

I would start with verifying total_minutes, is_within_time, and current_state are returning the values you actually want/expect. Along with that, verify you are pointing at the correct UDT instance.

I just wanted a script that can trigger enabling and disabling udt instances within a Tag browser in a specified time frame, say enabled from 6:00am-7:00pm daily. So I am just trying to use the Gateway event scripting feature.

Why do these tags need to be disabled outside this time period? Is there a tag script or something on these tags that you want to suppress outside of the time window? Are you trying to prevent historical data from being accumulated outside the time window?

Your immediate problem is that you are trying to enable/disable a tag within a time frame, what is the MAIN problem that your approach is solving/attempting to solve? Your current approach may not be the best solution.

I would do it like this:

now = system.date.now()
total_minutes = (system.date.getHour24(now) * 60) + system.date.getMinute(now)
system.tag.writeBlocking(["[default]UDTInstancePath" + ".Enabled"], [(360 <= total_minutes < 1140)])

Don't worry about 'unnecessary writes'...

I'm gonna to guess your math isn't mathing.

Example using system.date.* functions:

def enabledState(tagPathsIn, override=None):
	'''simple script to enable a tag between the hours of 6am and 7pm
		
		Parameters:
			tagPathsIn (list) : list of tags to affect
			override   (bool) : override value to set 
	'''

	tagPathsOut = ['{}.enabled'.format(tag) for tag in tagPathsIn]
	
	now = system.date.now()
	
	midnight = system.date.midnight(now)
	start = system.date.addHours(midnight, 6)
	end = system.date.addHours(midnight, 19)
	
	if override is None:
		writeState = system.date.isBetween(now, start, end)
	else:
		writeState = override
	 
	system.tag.writeBlocking(tagPathsOut, [writeState]*len(tagPathsOut ))


tagList = ['[default]path/to/tag1', '[default]path/to/tag2']

enabledState(tagList)

That said, I would probably do a driven tag group.

This is great thank you very much!