Gateway Tag Change Script to Change another tag

One of my tag (let's call this "Distance") has a big fluctuation in reading, and my ultimate goal is to trigger an alarm based on a tag when it reaches an average value below certain point.
Idea I had is to create historian into a database (using Transaction Groups), then call up last 1 hour value using SQL in Named Queries.
I've been looking, but how would I be able to write a script that runs whenever the fluctuating tag value changes, and when it runs, it will calculate the average of the queries found from the Named Queries, output the value to another tag (let's call this one "Avg_Distance")?

Or is there much more efficient way to do this?

The best way to do this would be to create the average tag in the PLC and just set up an alarm for that tag's value.

1 Like

Or, use a query tag.

1 Like

I would use an expression tag that uses my objectScript() expression function's state variable to implement a single-pole digital filter. Something like this:

objectScript("args[0] * (1.0-args[1]) + state.get('prior', args[0]) * args[1]\n"
  +"state['prior']=__retv", {[.]NoisyTag}, {[.]FilterFactorTag})

The FilterFactorTag would be a real number typically ~0.90 ish. The expression tag should execute with the group rate, not on change.

Then you can make an alarm on the expression tag.