# Moving Window Average Alarms

**URL:** <https://forum.inductiveautomation.com/t/moving-window-average-alarms/49286>\
**Category:** Ignition\
**Created:** [July 28, 2021, 8:19am UTC](https://forum.inductiveautomation.com/t/moving-window-average-alarms/49286 "2021-07-28T08:19:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sahil.jain](https://avatars.discourse-cdn.com/v4/letter/s/8e8cbc/32.png) [@sahil.jain](https://forum.inductiveautomation.com/u/sahil.jain)\
**Post date:** [July 28, 2021, 8:19am UTC](https://forum.inductiveautomation.com/t/moving-window-average-alarms/49286/1 "2021-07-28T08:19:07Z")

</div>

Hi,

I have some tags being populated from a device. I would like to create various moving window average based alarms - e.g. if the average value in the past n minutes is outside some setpoints, raise an alarm. I’m looking at implementing this for a wide variety of tags, for a wide variety of window sizes.

I can do this with gateway scripts, but I’m not sure if that’s the best way. Are there any other techniques I could use for this?

Thanks

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [July 28, 2021, 12:57pm UTC](https://forum.inductiveautomation.com/t/moving-window-average-alarms/49286/2 "2021-07-28T12:57:37Z")

</div>

I would be using my [objectScript](https://www.automation-pros.com/simaids/doc/expression.objectscript.html) expression function (from my [Simulation Aids](https://inductiveautomation.com/moduleshowcase/module/automation-professionals-llc-simulation-aids) module) in an expression tag, using it’s `state` variable to hold recent history in a fifo for fast computation. The alarms would then be set on the expression tag as usual.

---

<div class="post-metadata">

**Author:** ![sahil.jain](https://avatars.discourse-cdn.com/v4/letter/s/8e8cbc/32.png) [@sahil.jain](https://forum.inductiveautomation.com/u/sahil.jain)\
**Post date:** [July 29, 2021, 10:45am UTC](https://forum.inductiveautomation.com/t/moving-window-average-alarms/49286/3 "2021-07-29T10:45:15Z")

</div>

Thanks for the suggestion, I’ll try it out and post how I go!

---

<div class="post-metadata">

**Author:** ![vipo](https://avatars.discourse-cdn.com/v4/letter/v/eada6e/32.png) [@vipo](https://forum.inductiveautomation.com/u/vipo)\
**Post date:** [November 25, 2021, 8:45am UTC](https://forum.inductiveautomation.com/t/moving-window-average-alarms/49286/4 "2021-11-25T08:45:39Z")

</div>

Did you manage to implement this ?

I’ve got the exact same request…

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 25, 2021, 4:17pm UTC](https://forum.inductiveautomation.com/t/moving-window-average-alarms/49286/5 "2021-11-25T16:17:28Z")

</div>

Try this script:

[exprTagUtil.py](https://www.automation-pros.com/ignition/exprTagUtil.py)

Requires my Simulation Aids module, linked above.

---

<div class="post-metadata">

**Author:** ![sahil.jain](https://avatars.discourse-cdn.com/v4/letter/s/8e8cbc/32.png) [@sahil.jain](https://forum.inductiveautomation.com/u/sahil.jain)\
**Post date:** [November 25, 2021, 9:06pm UTC](https://forum.inductiveautomation.com/t/moving-window-average-alarms/49286/6 "2021-11-25T21:06:50Z")

</div>

I was curious how I could do it using base ignition, and what I came up with was:

1. Create an expression tag referencing the current time (updating at the frequency you want to update data).
2. Create a change event which logs the data you want to another tag (in this case, it runs a `queryTagHistory` using the window size to get the average in that period).
3. If you historise this other tag, you get a history of moving window averages.

I don’t think this would scale well, but my use case was for a few tags in a very small system, but used across multiple different systems.

For any real production system, I would assume the solution proposed by @pturmel is much more performant.
