Tag Level Expression

Hello Team,

I am looking for a tag level expression to compare current value and the previous/Historical 5mins average value. if the data is not changing, my tag should generate an alert. Could you please help me with this request

Thanks in advance

To many ways to do this.
Trying to do this using a expression tag might the hardest way tho. You could try using runScript() but it will be a pain for such a requirement.
Using an aux tag as boolean to trigger the alarm and a timer gateway script, might be the easiest way.
So, in the script ask for the avg for the last 5 mins and compare it with the current value and if both are the same write a true to alarm that tag else a false. On same tag, create an alarm mode equal to 1.

Try the code that should be placed in the timer gateway script, I haven’t test it.

now = system.date.now()
tagPath = '[default]value'
alarmPath = '[default]alarmValue'
currentValue = system.tag.read(tagPath).value
historyDataset = system.tag.queryTagHistory(paths=[tagPath], startDate= system.date.addMinutes(now,-5) , endDate=now, returnSize=1, aggregationMode="Average", returnFormat='Wide')
if historyDataset.rowCount == 1:
	avg = historyDataset.getValueAt(0,1)
	if avg == None:
		avg = currentValue
else:
	avg = currentValue

if avg == currentValue:
	system.tag.write(alarmPath, True)
else:
	system.tag.write(alarmPath, False)

Try to use read and write blocking instead.

@jespinmartin1 Thanks for your response. Can we configure this script on UDT level?

Oh ok then, there are a couple of things you should do.
In this case I do recommend creating a global project. No for inherence, instead for Gateway Scripting Project. Configure the name of the project in Gateway Setting in Config web page like:

After that, in global project, create a project script and give it a name and create a function. That function should receive a parameter that is the tagPath from which tag you wish to calculate the Average. Also this function should return either true of false, useful for the alarm: This may give an idea.

def calcAvg(tagPath):
	now = system.date.now()
	currentValue = system.tag.read(tagPath).value
	historyDataset = system.tag.queryTagHistory(paths=[tagPath], startDate= system.date.addMinutes(now,-5) , endDate=now, returnSize=1, aggregationMode="Average", returnFormat='Wide')
	if historyDataset.rowCount == 1:
		avg = historyDataset.getValueAt(0,1)
		if avg == None:
			avg = currentValue
	else:
		avg = currentValue
	
	if avg == currentValue:
		return True
	else:
		return False

Please improve this script using readBlocking and queryTagCalculation functions. You may also need to change the script to fit your needs.

Then create a boolean tag in your UDT definition of expression source.
The expression should look like:

runScript('alarmAverage.calcAvg',300000,'[default]value')

Where the first parameter is the reference to the script path, the seconds parameter is the refresh rate in millis (5minutes in this example) and at the end the tagPath to calculate the historical Average.
If you’re clever, you should be able to pass a tagPath in the 3rd parameter based on a UDT definition: (maybe using tag() expression or by creating another tag that holds the path reference)

Finally, since this will be a bool tag that will be true of false based on the script, create an alarm on it in equal mode to 1, details for this are trivial.