Script syntax error mismatched input "expcting Indent, <string>, 9,0

def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	if initialChange or previousValue.value is None:
		return
	# when value turn off
	
	if not currentValue.value and previousValue.value:
	
	#Tracking time and durations
start_time = None
duration = None
	
	# Get current time
	current_time = system.date.now()
	
	# Grab Opc Tags
	
	alarm_value = system.tag.readBlocking([default]DT Fault /Alarm).value
	station = system.tag.readBlocking([default]DT Fault /Station 4)
	 # get the description from alarm value to search the array
	Description = system.tag.readBlocking([default]DT Fault /StationAlarmArray/StationAlarmArray_[alarm_value]_/Description)
	
	# Alarm_value greater than 0 Start timers
	if alarm_value >= 1:
		if start_time is None:
			start_time = current_time
	else
		if start_time is not None
			end_time = current_time
			duration = system.date.secondsBetween(start_time, end_time)
			
	system.db.runPrepUpdate("""
		Insert INTO [dbo].[Dt_LH_Faults]
					([Date]
					,[station]
					,[alarm_value]
					,[Description]
					,[start_time]
					,[end_time]
					,[Duration])
				Values (GETDATE(),?,?,?,?,?,?)
				""",[Date,Station,Fault Idx, Fault Description,Start Time,End Time, Fault Duration], "CAL")

Can anyone help with the error and take a look to see if this is done right new to scripting thanks in advance

Python (and this forum) is sensitive to whitespace/indentation - it's how Python opens and closes "blocks" of code where other languages might use e.g. curly braces. Your start_time and duration variables are not indented at all, which means there's outside the scope of even the valueChanged function you're defining, which means they're not going to do anything. But that's a problem, because the previous semantic line, if not currentValue.value and previousValue.value: is opening a new lexical scope.

If you're used to a C/Java/Javascript/etc style language, think of it as you've left an open curly brace on the line with the if, but you're never closing it.

I would highly recommend a brief introductory Python course to get basic fundamentals of the syntax right.

3 Likes