Calculating Total Time in Seconds

This may help you out:

# Create sample dataset

sampleHeaders = ['t_stamp', 'value']
sampleData = [['2021-07-01 13:42:28', 1],
              ['2021-07-01 13:43:23', 1],
              ['2021-07-01 13:43:28', 0],
              ['2021-07-01 13:43:29', 0],
              ['2021-07-01 13:43:30', 1],
              ['2021-07-01 13:43:32', 1],
              ['2021-07-01 13:43:36', 0],
              ['2021-07-01 13:43:39', 1],
              ['2021-07-01 13:43:41', 1],
              ['2021-07-01 13:43:45', 0]
             ]
sampleData = [[system.date.parse(row[0]), row[1]] for row in sampleData]

sampleDataSet = system.dataset.toDataSet(sampleHeaders, sampleData)

# -----------------------------------------------------------------------

# Using a PyDataSet will let us use slicing
pyData = system.dataset.toPyDataSet(sampleDataSet)

# Process Data. Since the first row will not have an elapsed time, we can
# use the None for the run time
headers = ['t_stamp', 'value', 'run time']
data = [list(pyData[0]) + [None]]
#initialize elapsed time
elapsedSeconds = 0
# Using zip will let us iterate over two things at once.
# Start currentRow at row 1 instead of row 0
for currentRow, prevRow in zip(pyData[1:], pyData):
	# Check if the value in the previous row is a 1. 
	# If true, add to the elapsed time.	
	if prevRow['value'] == 1:
		elapsedSeconds += system.date.secondsBetween(prevRow['t_stamp'], currentRow['t_stamp'])
	if currentRow['value'] == 0:
		# Check whether we need to add the elapsed time to the 'run time' column or None
		if prevRow['value'] == 1:
			data.append(list(currentRow) + [elapsedSeconds])
			elapsedSeconds = 0
		else:
			data.append(list(currentRow) + [None])
	else:
		data.append(list(currentRow) + [None])
			
datasetOut = system.dataset.toDataSet(headers, data)

Resultant dataset:

row | t_stamp                      | value | run time
-----------------------------------------------------
0   | Thu Jul 01 13:42:28 EDT 2021 | 1     | None    
1   | Thu Jul 01 13:43:23 EDT 2021 | 1     | None    
2   | Thu Jul 01 13:43:28 EDT 2021 | 0     | 60      
3   | Thu Jul 01 13:43:29 EDT 2021 | 0     | None    
4   | Thu Jul 01 13:43:30 EDT 2021 | 1     | None    
5   | Thu Jul 01 13:43:32 EDT 2021 | 1     | None    
6   | Thu Jul 01 13:43:36 EDT 2021 | 0     | 6       
7   | Thu Jul 01 13:43:39 EDT 2021 | 1     | None    
8   | Thu Jul 01 13:43:41 EDT 2021 | 1     | None    
9   | Thu Jul 01 13:43:45 EDT 2021 | 0     | 6 
4 Likes