Problem adding variable in email body

Good morning,

I’m able to send my report by email when I have a tag change, it work perfectly.
What I’m trying to do is to add a variable in the body, in this case the email is not sent.

value = newValue.value

if value == 1:

    body = "TagTestValue: "  #If I just add this in the body, it works
    # body = "TagTestValue: " + system.tag.read("[default]TagTest").value  # With this line, adding the tag.read in the body, the email is not sent and I have none error in the console

	system.report.executeAndDistribute(path="MyPath", project="MyProject", action= "email", 
	actionSettings = {"to":["myEmail"], "smtpServerName":"EmailSetting", "from":"myCompany", "subject":"MySubject", "body":body})

Where’s the mistake?

Thank you very much.

value = newValue.value

if value == 1:
    variable = system.tag.read("[default]TagTest")
    variableQV = variable.value
    body = "TagTestValue: " +str(variableQV)
    

	system.report.executeAndDistribute(path="MyPath", project="MyProject", action= "email", 
	actionSettings = {"to":["myEmail"], "smtpServerName":"EmailSetting", "from":"myCompany", "subject":"MySubject", "body":body})
1 Like

Thanks for your help Matrix_Engeneering. It works great.

One question about this: I see that if I try to send a variable DateTime and it has a correct value, the email is sent. If the variable DateTime has not a correct value (for example is “null”), the email is not sent.

Is there a way to check if the variable has a correct value, so I can manage the email body in the properly way?

Thank you very much

Where are you getting DateTime from, a tag?

You can try something like this maybe changing -1 to “None”

x = system.tag.read("[System]Gateway/CurrentDateTime")

print x

QV = x.value

print QV

if QV != -1:
	print "send email"
	#Put Your Send Email Code Here
	

Hi Matrix_Engineering and thanks for your reply.

I try to explain what I need to do, sorry for the english: I have a report that is sent when a batch is finished, and in this report I have a StartTime and an EndTime tags that I fill when this batch starts and ends.

At the end of the batch I send an email with process data and trends, and in the body I add the StartTime and EndTime with a specific format

startBatch = system.tag.read([default]myPath).value
startBatch = system.date.format(startBatch , dd MMMM yyyy HH:mm)

If the DateTime have correct values, the email is sent, but I've seen that if one or both the DateTime tags have no value ("Null"), the email is not sent, so I would check if the DateTime is valid before to add it in the body, in the case is not valid I will manage it in another way.

I try this

if QV != -1:
if QV != "None":
if QV != "Null":

but in all the cases the email is not sent.

Thanks again for your help.

Yes because you don’t have QV in your script.

I would change so your variables are unique (for the formatted):

startBatch = system.tag.read([default]myPath).value
startBatchF = system.date.format(startBatch , dd MMMM yyyy HH:mm)

Then when you go to add the variable to email, make sure to typecast as string:

str(startBatchF)

When checking != None etc, you don’t need to add quotes around None and Null

None is special in Python & Jython. You are supposed to test for it with the is keyword. Or is not. Like so:

if startBatch is None:
    # log that the batch interval is invalid...

Thanks everybody for the help.

I change the script as you suggest and it works perfectly!