Create an Expression Tag from a string?

Good morning,

Some background:
I have a requirement from a client that they would like the ability to write a string containing tag alias's and using math operators (add, sub, multiply, divide, brackets, exponents) to calculate a new value. I can do this every time one of the tag's value changes but my issue is around updating a memory tag that uses expression language and historize with the Tag Historian module. The goal is to plot these tags on trends.

So far my tests are set up using a Textfield, and on button click read the text prop then paste the changed string to a label. Ex: If I write: "Pressure 01 + Pressure 02" the label will output as:
{[default]JobData/Pressure 01/value]} + {[default]JobData/Pressure 02/value]}

Pressure 01 & Pressure 02 are alias names we stored in a dataset with the tag paths.

Is there a way to write this into new string into a memory tag as an expression with tag paths?

Code for reference:

def runAction(self, event):
	string = self.getSibling("TextField").props.text
	dset = system.tag.readBlocking("[default]ScreenParameters/JobTagConfig")[0].value
	
	
	
	def check(word):
		headers = ["alias","tagPath"]
		set = []
		for row in range(dset.getRowCount()):
			if dset.getValueAt(row,1) in word:
				set.append([dset.getValueAt(row,1),dset.getValueAt(row,3)])
		return system.dataset.toDataSet(headers,set)
	
	returned = check(string)
	
	for row in range(returned.getRowCount()):
		#value = system.tag.readBlocking(returned.getValueAt(row,1))[0].value
		#string = string.replace(str(returned.getValueAt(row,0)), str(value))
		string = string.replace(str(returned.getValueAt(row,0)), ("{"+returned.getValueAt(row,1)+"}"))
	
	print string
	#self.parent.parent.getChild("Label").props.text = project.evaluate.evaluate(string)
	self.parent.parent.getChild("Label").props.text = string

Interesting question and I didn't expect it to be possible.

I tried a quick tag binding from a Text Field component to [default]ExpressionTag.Expression and it updates and works. You should be able to write it from a script.

I'd be very, very careful with giving the users this ability. It opens up the possibility to do the equivalent of SQL injection via your tags. I would be inclined to give them a dialog, let them choose tags and operators from dropdowns and then sanitise the operation before writing it to the tag's expression.

1 Like