Difference in Tag History Values Between Script and Excel Export

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:

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!

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?

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:


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!

Post code, man! Not pictures of code! Please see Wiki - how to post code on this forum.

1 Like

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.

1 Like

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!

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!

1 Like

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:
  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.

2 Likes

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".

2 Likes