Hello, I have been working away at doing some way of creating a simple script to do some query Tag calculations to take two tags, sum them, and then divide one sum by the other and multiply by 100. I put the complete script below. The problem I have is that on trying to perform the final calculation and returning it “return float(sum_value_tag1) / float(sum_value_tag2) * 100,” on the the 4th last line of the below I get a “‘return’ outside function” that I am struggling to fix. I tried a similar script in the script console and it worked, but putting it in this format I get the a Syntaxerror that is not apparent to me.
def calculateEfficiency(tagPath1, tagPath2, timeMinutes):
# Define the tag paths
# tag_path_1= Total Power for Historian Data Points
# tag_path_2= Total Flow for Historian Data Points
# Define the time range
start_date = system.date.addMinutes(system.date.now(), -timeMinutes) # Last X minutes
end_date = system.date.now()
# Query the sum for each tag individually
# The 'sum' calculation will return the sum of values over the specified time range
sum_tag1_data = system.tag.queryTagCalculations(
paths=[tag_path_1],
calculations=['sum'],
startDate=start_date,
endDate=end_date,
noInterpolation=True
)
# The 'sum' calculation will be in the column named 'sum'
sum_value_tag1 = sum_tag1_data.getValueAt(0, "sum")
# The 'sum' calculation will return the sum of values over the specified time range
sum_tag2_data = system.tag.queryTagCalculations(
paths=[tag_path_2],
calculations=['sum'],
startDate=start_date,
endDate=end_date,
noInterpolation=True
)
# The 'sum' calculation will be in the column named 'sum'
sum_value_tag2 = sum_tag2_data.getValueAt(0, "sum")
if sum_value_tag2 != 0:
# Perform the division and multiply by 100
return float(sum_value_tag1) / float(sum_value_tag2) * 100
else:
system.util.getLogger("LoggerName").warn("Cannot divide by zero: Sum of Tag2 is zero.")
return None
def calculateEfficiency(tagPath1, tagPath2, timeMinutes):
# Define the tag paths
# tag_path_1= Total Power for Historian Data Points
# tag_path_2= Total Flow for Historian Data Points
# Define the time range
start_date = system.date.addMinutes(system.date.now(), -timeMinutes) # Last X minutes
end_date = system.date.now()
# Query the sum for each tag individually
# The 'sum' calculation will return the sum of values over the specified time range
sum_tag1_data = system.tag.queryTagCalculations(
paths=[tag_path_1],
calculations=['sum'],
startDate=start_date,
endDate=end_date,
noInterpolation=True
)
# The 'sum' calculation will be in the column named 'sum'
sum_value_tag1 = sum_tag1_data.getValueAt(0, "sum")
# The 'sum' calculation will return the sum of values over the specified time range
sum_tag2_data = system.tag.queryTagCalculations(
paths=[tag_path_2],
calculations=['sum'],
startDate=start_date,
endDate=end_date,
noInterpolation=True
)
# The 'sum' calculation will be in the column named 'sum'
sum_value_tag2 = sum_tag2_data.getValueAt(0, "sum")
if sum_value_tag2 != 0:
# Perform the division and multiply by 100
return float(sum_value_tag1) / float(sum_value_tag2) * 100
else:
system.util.getLogger("LoggerName").warn("Cannot divide by zero: Sum of Tag2 is zero.")
return None
Thank-you for the response jpark. I was thinking it was that, but have done and redone the indentation for the last part, ensuring that i don’t have spacing and still get the error, and why I ended up here.
Is there anyway you can show me the error of the below snippet.
if sum_value_tag2 != 0:
# Perform the division and multiply by 100
return float(sum_value_tag1) / float(sum_value_tag2) * 100
else:
system.util.getLogger("LoggerName").warn("Cannot divide by zero: Sum of Tag2 is zero.")
return None