# Difference in Tag History Values Between Script and Excel Export

**URL:** <https://forum.inductiveautomation.com/t/difference-in-tag-history-values-between-script-and-excel-export/104964>\
**Category:** Ignition\
**Tags:** perspective, scripting\
**Created:** [April 10, 2025, 5:29pm UTC](https://forum.inductiveautomation.com/t/difference-in-tag-history-values-between-script-and-excel-export/104964 "2025-04-10T17:29:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![willer](https://avatars.discourse-cdn.com/v4/letter/w/f4b2a3/32.png) [@willer](https://forum.inductiveautomation.com/u/willer)\
**Post date:** [April 10, 2025, 5:29pm UTC](https://forum.inductiveautomation.com/t/difference-in-tag-history-values-between-script-and-excel-export/104964/1 "2025-04-10T17:29:29Z")

</div>

I'm working on a script that separates tag values into "peak" and "off-peak" periods. However, I'm noticing a discrepancy: the total value shown on my graph/label (based on the script) doesn't match the total I get when I export the tag history to Excel using the tag historian.

To troubleshoot, I tried passing the tag value directly to a label and applying a script transformation, hoping to filter the tag values using a `start_date` and `end_date` as parameters. Here's the basic transformation script I'm working with:

 ![transformação de tag direto](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/3/73354ce60562ff8076db15cf4624057c0c2b6bcc.png)

But I'm not sure how to access or filter the tag values within that transformation using a time range. Is it possible to use `start_date` and `end_date` parameters in a script transform like this? Or is there a better way to make sure the tag values shown match the historical export data?

Any guidance would be much appreciated!

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [April 10, 2025, 8:11pm UTC](https://forum.inductiveautomation.com/t/difference-in-tag-history-values-between-script-and-excel-export/104964/2 "2025-04-10T20:11:33Z")

</div>

I'm not really sure what you're asking.

Tag historian is a separate subsystem from the live tag values - obviously they're correlated to each other, but the live value you get from a tag is not buffered or rinsed through the historian at all.

You can choose to use a history binding to bring in the recorded history (which may or may not be subject to interpolation and compression, per your provided settings) **or** you can choose to use the realtime value of the tag.

Can you share a concrete example of where you're seeing a discrepancy?

---

<div class="post-metadata">

**Author:** ![willer](https://avatars.discourse-cdn.com/v4/letter/w/f4b2a3/32.png) [@willer](https://forum.inductiveautomation.com/u/willer)\
**Post date:** [April 10, 2025, 8:26pm UTC](https://forum.inductiveautomation.com/t/difference-in-tag-history-values-between-script-and-excel-export/104964/3 "2025-04-10T20:26:47Z")

</div>

Thanks for the reply!

To clarify my setup: I’m using an XYChart to show active energy consumption, and I’m calling a script through an expression binding like this:  
runScript("Medidor.Consumo\_Diario.calcular\_consumo\_tag", 60000, {view.params.Path}+"/CT/WHP", {../Container\_DateTime/DateTime\_Inicio.props.formattedValue}, {../Container\_DateTime/DateTime\_Fim.props.formattedValue})

This script calculates daily consumption and separates it into “peak” and “off-peak” periods. Here's the full script I'm using:

 ![script](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/8/384e6583aaa97bb18621095ad666c6a6bbc8f315.png)  
The problem is that the values displayed in the chart differ from the values I see when I export the same tag using the tag browser’s “Export to Excel” function — especially in the off-peak values. I'm trying to understand why this happens.

Could this be due to how the script is calculating the consumption using the tag history for specific periods (e.g., 17h-20h for peak)? Or maybe some kind of interpolation difference between how the chart and the export handle historical data?

Any thoughts or suggestions on how to align these values would be super helpful. Thanks again!

---

<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:** [April 10, 2025, 8:35pm UTC](https://forum.inductiveautomation.com/t/difference-in-tag-history-values-between-script-and-excel-export/104964/4 "2025-04-10T20:35:11Z")

</div>

Post code, man! Not pictures of code! Please see [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831).

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [April 10, 2025, 8:43pm UTC](https://forum.inductiveautomation.com/t/difference-in-tag-history-values-between-script-and-excel-export/104964/5 "2025-04-10T20:43:16Z")

</div>

> [@willer](#):
>
> Could this be due to how the script is calculating the consumption using the tag history for specific periods (e.g., 17h-20h for peak)? Or maybe some kind of interpolation difference between how the chart and the export handle historical data?

Yes, absolutely it could be. You're comparing apples to oranges. I would be **surprised** if a script you wrote gave you the exact same results as the historian. I don't know what the nested `calcular_consumo_periodo` function in the script you posted is doing - is _it_ querying the historian?

It _sounds_ like, though I can't really be sure so far, you might just not want to use the historian at all. The historian is really best suited for constantly "on", changing values that can be queried and compressed relatively efficiently. If you have "shifts" or "cycles" or anything else that adds semantic meaning to specific time windows, you are probably better off with a dedicated table structure for just this data. You can use transaction groups or scripting to log your data into this table _based on a condition_, and retrieve it with bindings.

---

<div class="post-metadata">

**Author:** ![willer](https://avatars.discourse-cdn.com/v4/letter/w/f4b2a3/32.png) [@willer](https://forum.inductiveautomation.com/u/willer)\
**Post date:** [April 11, 2025, 11:20am UTC](https://forum.inductiveautomation.com/t/difference-in-tag-history-values-between-script-and-excel-export/104964/6 "2025-04-11T11:20:15Z")

</div>

Thanks again for your response!

The idea behind the `calcular_consumo_periodo` function is to read the tag values between `inicio_fora_ponta` and `fim_fora_ponta`, because energy consumption differs depending on the time of day. So the goal is just to separate the consumption values (peak/off-peak) so that we can later convert them properly in Excel.

However, the issue is that whenever the client has off-peak consumption, the numbers shown in my chart don’t match what we see when exporting the tag history to Excel. The chart values seem off — especially during the off-peak periods — even though the time ranges are being applied correctly in the script.

You mentioned using the historian directly — could you give me an example or suggestion on how I could use the historian query to do this instead? Maybe using `system.tag.queryTagHistory()` with aggregation mode and date range filtering?

Any example or direction would be really helpful. Thanks again for the support!

---

<div class="post-metadata">

**Author:** ![willer](https://avatars.discourse-cdn.com/v4/letter/w/f4b2a3/32.png) [@willer](https://forum.inductiveautomation.com/u/willer)\
**Post date:** [April 11, 2025, 11:22am UTC](https://forum.inductiveautomation.com/t/difference-in-tag-history-values-between-script-and-excel-export/104964/7 "2025-04-11T11:22:13Z")

</div>

Apologies, I hadn’t seen the proper way to post code here. Thanks a lot for the heads-up! I’ll make sure to follow the formatting guidelines you shared for future posts. Really appreciate your help!

---

<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:** [April 11, 2025, 11:43am UTC](https://forum.inductiveautomation.com/t/difference-in-tag-history-values-between-script-and-excel-export/104964/8 "2025-04-11T11:43:48Z")

</div>

For this kind of application the historian isn't great. As you have found out, it doesn't give proper values for start and end times and interpolation is difficult.

The solution is to write your own logger. It takes a little setup but the results are simple and exact.

- Create a new database / table `energyMeter` (or whatever) with columns `t_stamp, meter_id, kWh_total, kVAr_total` or whatever you're reading.
- Write a Project Library script to read each of the meters' tags and write the values to the table.
- Create a scheduled gateway event:  
 ![Meter reading scheduled script](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/7/d78f3ace6411e58f9e4d7f0528400b131877ebb1.png)

1. Gateway events.
2. Scheduled.
3. Give it a sensible name.
4. On the hour and every 15 minutes.
5. Check the interpretation.
6. On the Script tab write a one-liner to call the Project Library script.

Now you will know the exact reading for every time interval and can manipulate those as required.

For testing you could set Minutes = \* to force a read every minute.

---

<div class="post-metadata">

**Author:** ![Felipe\_CRM](https://avatars.discourse-cdn.com/v4/letter/f/dfb087/32.png) [@Felipe\_CRM](https://forum.inductiveautomation.com/u/Felipe_CRM)\
**Post date:** [April 11, 2025, 1:37pm UTC](https://forum.inductiveautomation.com/t/difference-in-tag-history-values-between-script-and-excel-export/104964/9 "2025-04-11T13:37:15Z")

</div>

A workaround to at least get more consistent results from the historian is to make sure you always query the data "AsStored" (and "Tall" if you have multiple tags) so it doesn't apply its interpolation. Of course the quality of the results then depends on how often you had your historian actually recording a value, i.e. the deadband set up as "Discrete".
