Execute Script On Tag Change Not Working

I have a script that is sending a text, if a tag is set to true I want it to execute the script.

This is the value change setup that I currently have that is not working.

Here is the SeanTextHelpCall Script.

from java.io import File
from javax.imageio import ImageIO

#importing the Time library for the date and the time. 
from time import sleep
import datetime

# Importing the necessary modules for email sending
from java.nio.file import Files
from java.nio.file import Paths

def Main():


	tags = [
		#Regular Help Call Buttons
		(".../Block A/Station 1010/Mode/Station 1010/CallPB", "Regular", "1010"),
		(".../Block A/Station 1040/Mode/Station 1040/CallPB", "Regular", "1040"),
		(".../Block B/Station 1100/Mode/Station 1100/CallPB", "Regular", "1100"),
		(".../Block B/Station 1530/Mode/Station 1530/CallPB", "Regular", "1530"),
		(".../Block B/Station 1160/Mode/Station 1160/CallPB", "Regular", "1160"),
		(".../Block C/Station 1640/Mode/Station 1640/CallPB", "Regular", "1640"),
		(".../Block D/Station 1310/Mode/Station 1310/CallPB", "Regular", "1310"),
		(".../Block E/Station 1398/Mode/Station 1398/CallPB", "Regular", "1398"),
		#Quality Help Call Buttons
		(".../Block A/Station 1010/Mode/BlockA_Front_Help_CallPB_Quality", "Quality", "1010"),
		(".../Block A/Station 1040/Mode/BlockA_Middle_Help_CallPB_Quality", "Quality", "1040"),
		(".../Block B/Station 1100/Mode/BlockB_Beginning_Help_CallPB_Quality", "Quality", "1100"),
		(".../Block B/Station 1530/Mode/BlockB_Middle_Help_CallPB_Quality", "Quality", "1530"),
		(".../Block B/Station 1160/Mode/BlockB_Back_Help_CallPB_Quality", "Quality", "1160"),
		(".../Block C/Station 1640/Mode/BlockC_Help_CallPB_Quality", "Quality", "1640"),
		(".../Block D/Station 1310/Mode/BlockD_Help_CallPB_Quality", "Quality", "1310"),
		(".../Block E/Station 1398/Mode/BlockE_Help_CallPB_Quality", "Quality", "1398")
	]

	
	#Getting Times & Dates
	todays_date = datetime.date.today().strftime('_%m_%d_%y')
	current_time = datetime.datetime.now().strftime('_%H_%M')
	
	smtp = "WorkingSMTP.com"
	username = ""  # not required as smtpProfile is used
	sender = "SMTPEMAIL@THISWORKS.com"
	password = "password"
	subject = ""
	body = current_time + "\n"
	
	for tagPath, helpType, station in tags:
		if system.tag.read(tagPath).value:  # Assuming you're reading the tag like this
			body += helpType +  " Help Call At Station " +station + "\n"
	
	
	
	recipients = ["1231231234@txt.att.net"]
	
	if "Help Call" in body:
		system.net.sendEmail(smtp=smtp, fromAddr=sender, subject=subject, body=body,html=0, to=recipients, password=password)	

The texts send through if I manually set the tags to TRUE and THEN manually execute in the script editor tool.

However, the script doesn't run automatically when I have the setup as I do in the Image.

There are many things wrong with the script you have posted, but rather than diving into that, it seems like what you want could be achieved by an alarm with email notification. I would suggest implementing it the normal way rather than going down the custom scripting path when there doesn't seem to be a need to.

6 Likes

In Gateway Scripts a value for tag change can be set and tags can be added.

The Script referenced is "SeanTextHelpCall.Main()" in the script section.

This isn't the solution. If you don't have the alarm notification module, then there's still better ways of handling this. Most likely your original problem was that you didn't have the script you're trying to call set as the Gateway Scripting project. Besides that, it's better to write a generic script that has the parameters of helpType and station passed to it and it only sends the email, then configure in each tag change script the small script of checking the value and only calling the script with the station number and help type if the tag is true.

Again, this would be better as an alarm notification, but if you didn't buy that module, this would be a minimal workaround.

3 Likes

The actual problem I suspect was that you were trying to call a project script from a Tag Value Change script. However, since, Value Change Scripts do not have access to project scripts not defined in the Gateway Scripting Project, it was most likely not executing.

Because Gateway Tag Change Events are a project resource, they do have access to project scripts and it is executed.

As for the script, as @amy.thompson pointed out, there are issues.

  1. You should not use the Python Datetime library for dates, instead, use the functions provided by Ignition.
  2. You're importing sleep, but not using it (which is good) but you should also not be importing it. In my experience anytime sleep is being used in ignition, something is not being done in the most optimal way. If there are other functions within this script library, I would suggest that you look at eliminating any use of sleep in them.
  3. You are making multiple calls to system.tag.read() I can tell from your screen snips that this is Ignition 8, and therefore you should be using system.tag.readBlocking() and you should be reading all of the tags in one go, as opposed to 1 at a time. Done correctly you don't need to read the tags, the value is handed to you in the event.
  4. You're doing too much work in this script. Ideally this script would be generic.

I would write your script like this in the Gateway event:

SeanTextHelpCall.prepareHelpCall(str(event.tagPath),newValue.value)

Your script library functions would then look like this:

stations = ["1010","1040","1100","1530","1160","1640","1310","1398"]
def prepareHelpCall(tagPath, value):
	for station in stations:
		if station in tagPath:
			if "Quality" in tagPath:
				sendHelpCallMessage("Quality",station)
				break
			sendHelpCallMessage("Regular",station)
		
def sendHelpCallMessage(helpType, station):
	
	currentDateTime = system.date.format(system.date.now(), '_mm_dd_yyyy_HH_MM')
	
	smtp = "WorkingSMTP.com"
	usernaem = ""
	sender = "SMTPEMAIL@THISWORKS.com"
	password = "password"
	subject = ""
	body = "{} \n{} Help Call At Station {} \n".format(currentDateTime,helpType, station)
	
	recipients = ["1231231234@txt.att.net"]
	
	system.net.sendEmail(smtp=smtp, fromAddr=sender, subject=subject, body=body, html = False, to=recipients, password=password)
2 Likes

I would skip the prepareHelpCall all together and just run everything off of the tag change on the UDT tag. Since they're not passing the tag name to the sendHelpCallMessage, you could just pass the 2 parameters directly to sendHelpCallMessage from the tags without doing any loops at all.

Tag Value Change scripts should execute in around 10ms or less. This script may not meet that criteria. So a gateway tag change event is better suited.

Separating the functions out into separate functions based on what they’re doing is not only cleaner (imo) but also provides for the event script to be a single line call, avoiding any legacy scoping issues.

Sending the tag path allows for the function to determine the station generically,

1 Like