# How can I create a Tag that averages every 24 hours (8am-8am)

**URL:** <https://forum.inductiveautomation.com/t/how-can-i-create-a-tag-that-averages-every-24-hours-8am-8am/86249>\
**Category:** Ignition\
**Tags:** ignition81, perspective, scripting, designer\
**Created:** [February 23, 2024, 1:20pm UTC](https://forum.inductiveautomation.com/t/how-can-i-create-a-tag-that-averages-every-24-hours-8am-8am/86249 "2024-02-23T13:20:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ndubuisi\_Uzoho](https://avatars.discourse-cdn.com/v4/letter/n/b5e925/32.png) [@Ndubuisi\_Uzoho](https://forum.inductiveautomation.com/u/Ndubuisi_Uzoho)\
**Post date:** [February 23, 2024, 1:20pm UTC](https://forum.inductiveautomation.com/t/how-can-i-create-a-tag-that-averages-every-24-hours-8am-8am/86249/1 "2024-02-23T13:20:13Z")

</div>

How can I create a Tag that averages every 24 hours from 8am the previous day to 8am today.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [February 23, 2024, 1:37pm UTC](https://forum.inductiveautomation.com/t/how-can-i-create-a-tag-that-averages-every-24-hours-8am-8am/86249/2 "2024-02-23T13:37:34Z")

</div>

> [@Ndubuisi\_Uzoho](#):
>
> ... a Tag that averages every 24 hours ...

That averages what? Line speed? Cycles per hour?  
There probably are ways to it but sounds like you really should be considering using a database and recording the count on the hour and then it's a simple SQL query to return the values any way you like, for any day, week, month or year you like. It will be more reliable and much more useful.

---

<div class="post-metadata">

**Author:** ![Ndubuisi\_Uzoho](https://avatars.discourse-cdn.com/v4/letter/n/b5e925/32.png) [@Ndubuisi\_Uzoho](https://forum.inductiveautomation.com/u/Ndubuisi_Uzoho)\
**Post date:** [February 23, 2024, 1:41pm UTC](https://forum.inductiveautomation.com/t/how-can-i-create-a-tag-that-averages-every-24-hours-8am-8am/86249/3 "2024-02-23T13:41:55Z")

</div>

it averages a flowrate per day tag

---

<div class="post-metadata">

**Author:** ![Ndubuisi\_Uzoho](https://avatars.discourse-cdn.com/v4/letter/n/b5e925/32.png) [@Ndubuisi\_Uzoho](https://forum.inductiveautomation.com/u/Ndubuisi_Uzoho)\
**Post date:** [February 23, 2024, 1:44pm UTC](https://forum.inductiveautomation.com/t/how-can-i-create-a-tag-that-averages-every-24-hours-8am-8am/86249/4 "2024-02-23T13:44:58Z")

</div>

I already have a an event based Tag named oil\_flowrate\_bpd but will want to calculate the 8am to 8am (24hours) of the same tag and write it into another tag and store the output as a history tag. So i need the script to do that.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [February 23, 2024, 1:49pm UTC](https://forum.inductiveautomation.com/t/how-can-i-create-a-tag-that-averages-every-24-hours-8am-8am/86249/5 "2024-02-23T13:49:47Z")

</div>

> I already have a an event based Tag named oil\_flowrate\_bpd ...

What happens if there is no event for a couple of hours either side of 08:00? You won't have a count value to calculate from. You might need a scheduled record.

> ... but will want to calculate the 8am to 8am (24hours) of the same tag and write it into another tag and store the output as a history tag.

If `oil_flowrate_bpd` is a counter then just store the counter value every hour in a custom database. The Historian isn't the right tool for the job. There's no need for extra tags. The count for any day can be retrieved using either a Query Binding or using a Named Query.

---

<div class="post-metadata">

**Author:** ![Ndubuisi\_Uzoho](https://avatars.discourse-cdn.com/v4/letter/n/b5e925/32.png) [@Ndubuisi\_Uzoho](https://forum.inductiveautomation.com/u/Ndubuisi_Uzoho)\
**Post date:** [February 23, 2024, 2:02pm UTC](https://forum.inductiveautomation.com/t/how-can-i-create-a-tag-that-averages-every-24-hours-8am-8am/86249/6 "2024-02-23T14:02:02Z")

</div>

Ok. kindly assist me with creating a scheduled record.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [February 23, 2024, 2:07pm UTC](https://forum.inductiveautomation.com/t/how-can-i-create-a-tag-that-averages-every-24-hours-8am-8am/86249/7 "2024-02-23T14:07:00Z")

</div>

You've some study to do.

> **[Gateway Event Scripts | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/platform/scripting/scripting-in-ignition/gateway-event-scripts#gateway-scheduled-scripts)**
>
> Gateway Event Scripts Overview

> **[system.tag.readBlocking | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-tag/system-tag-readBlocking)**
>
> Reads the value of the tags at the given paths.

> **[system.db.runPrepUpdate | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-db/system-db-runPrepUpdate)**
>
> Runs a prepared statement against the database, returning the number of rows that were affected.

You'll need some basic familiarity with SQL.

---

<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:** [February 23, 2024, 2:33pm UTC](https://forum.inductiveautomation.com/t/how-can-i-create-a-tag-that-averages-every-24-hours-8am-8am/86249/8 "2024-02-23T14:33:49Z")

</div>

> [@Transistor](#):
>
> If `oil_flowrate_bpd` is a counter

"Rate" and "counter" are contradictions. If it really is an analog **rate** , then it needs to be historized with fine granularity, and the historian queried for the time periods to average. Using the historian every 8am to compute the past 24hour average is the right approach. The result should then be stored in a dedicated table, not in another historized tag.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [February 23, 2024, 2:44pm UTC](https://forum.inductiveautomation.com/t/how-can-i-create-a-tag-that-averages-every-24-hours-8am-8am/86249/9 "2024-02-23T14:44:31Z")

</div>

> [@pturmel](#):
>
> "Rate" and "counter" are contradictions.

I know. I know. We were in trouble from the first post.
