I appreciate your feedback and I've applied your advice. I don't want to miss the forest for the trees though, as my script is still producing unexpected output. This is the core issue I'm seeking help with. I've gone through several iterations with the same results: The script writes to the CSV file every few minutes even while the values for 'triggerComponent[0]' and 'previousValue0' remain unchanged at a value of 0 for each. I've been watching the tag values live as the script runs.
Ignition 7.9
The purpose of the script:
- To write the states of specific components to a file when an unexpected shutdown occurs
- This shutdown is caused by a type of event that occurs on average about 5 times per week
- Only one new line should be written per each event
What I expect the script to do:
- When the value of 'triggerComponent[0]' changes, the script runs
- Then it checks if the value for 'previousValue0' (this is a memory tag) is greater than or equal to 10
(this is to ensure the script writes only when the event occurs) & 'triggerComponentValue[1] is less
than or equal to 1
- If both are true, the script writes to the CSV file
What the script is doing:
- Writing to the CSV file every few minutes while the values that it checks remain at 0
Additional Information
- The only print statement that shows up in the 'wrapper.log' is "Data written to CSV at..."
- In the 'Gateway Tag Change Scripts' interface, I originally used both 'triggerComponent' tags in the
'Tag Path(s)' window. I now have only triggerComponent[0].
- I've replace potentially sensitive info with an 'x'
Here is the current script. I will update lines as necessary for efficiency, but I would love to work out the core functionality first.
Thank you
Gateway Tag Change Script
import system
import csv
import os
# Define the list of triggering components
triggerComponentPath = [
"x/GVBED1/F19/F19:35",
"x/GVBED1/F53/F53:245",
]
# Define the list of components to monitor with corresponding column headers
componentPaths = [
{"path": "x/GVBED1/F53/F53:60", "header": "D1 inlet temp"},
{"path": "x/GVBED1/F53/F53:65", "header": "D1 outlet temp"},
{"path": "x/GVBED1/F53/F53:70", "header": "D2 inlet temp"},
{"path": "x/GVBED1/F53/F53:75", "header": "D2 outlet temp"},
{"path": "x/GVBED1/F53/F53:80", "header": "D3 inlet temp"},
{"path": "x/GVBED1/F53/F53:85", "header": "D3 outlet temp"},
{"path": "x/GVBED1/F53/F53:115", "header": "Burn Chamber temp"},
{"path": "x/GVBED1/F53/F53:120", "header": "Plenum temp"},
{"path": "x/GVBED1/PD90/F554/F554:14", "header": "TCV-101 D1 Damper pos"},
{"path": "x/GVBED1/N:108/TCV102", "header": "TCV-102 D2 Damper pos"},
{"path": "x/GVBED1/PD90/F554/F554:16", "header": "TCV-103 D3 Damper pos"},
{"path": "x/GVBED4/B10_Damper_Scaled_pos_Ing", "header": "B10 Damper pos"},
{"path": "x/GVBED3/ACO_D4_Dry_Scaled", "header": "ACO moisture %"},
{"path": "x/GVBED1/N110/TCV104", "header": "TCV-104 D3 Cold Damper pos"},
{"path": "x/GVBED4/TCV107", "header": "TCV-107 D1 Cold Damper pos"},
{"path": "x/GVBED4/TCV108", "header": "TCV-108 D2 Cold Damper pos"},
{"path": "x/GVBED3/M15_Press_Fiber_Lbs_Hour", "header": "Lbs per hour"},
{"path": "x/GVBED4/TE_Tank_Farm", "header": "Outside Temp"}
]
# Define the path of the CSV file to store the state information
csvDirectoryPath = "C:\\Users\\x\\Documents\\Ignition Fire Report\\"
csvFilePath = csvDirectoryPath + "output.csv"
# Define the path of the Memory Tag to store the previous state of triggerComponentValues[0]
previousValue0TagPath = "x/GVBED1/F19/F19:35_previous"
# Initialize fire event flag
fire_event_detected = False
# Event handler for Gateway Tag Change Event
def tagChangeEvent(event):
global fire_event_detected # Declare the flag as global to modify it
# Check if the event is due to the initial subscription
if not event.initialChange:
# Print when the tagChangeEvent function is called
print "tagChangeEvent called at", system.date.now()
# Check if the tag that changed is F19:35
if event.getTagPath() == "x/GVBED1/F19/F19:35":
# Read the current value of the triggering components
triggerComponentValues = [system.tag.read(tagPath).value for tagPath in triggerComponentPath]
# Read the previous value from the Memory Tag
previousValue0 = system.tag.read(previousValue0TagPath).value
# Print the values of F19:35 and F19:35_previous
print "F19:35 value:", triggerComponentValues[0]
print "F19:35_previous value:", previousValue0
# Check if the previous value of triggering component[0] was at least 10 and the current value of triggerComponent[1] is 1 or less
if previousValue0 >= 10 and triggerComponentValues[1] <= 1:
if not fire_event_detected:
# Fire event detected, set flag
fire_event_detected = True
print "Fire event detected, setting flag to True"
# Get the current timestamp
currentTimestamp = system.date.now()
# Open the file in append mode using the csv module
csvfile = open(csvFilePath, "a")
writer = csv.writer(csvfile)
# Check if the file is empty
if os.path.isfile(csvFilePath) and os.path.getsize(csvFilePath) == 0:
# Write column headers if the file is empty
columnHeaders = ["Timestamp"]
columnHeaders.extend(component["header"] for component in componentPaths)
writer.writerow(columnHeaders)
# Capture component states
states = [system.tag.read(component["path"]).value for component in componentPaths]
# Write captured states to CSV file
row = [currentTimestamp]
row.extend(states)
writer.writerow(row)
csvfile.close()
print "Data written to CSV at", currentTimestamp
elif fire_event_detected and not (previousValue0 >= 10 and triggerComponentValues[1] <= 1):
# Conditions for a fire event no longer met, reset flag
fire_event_detected = False
print "Fire event conditions no longer met, resetting flag to False"
# Update the previous value in the Memory Tag
system.tag.write(previousValue0TagPath, triggerComponentValues[0])