IMDTouchKnowledge BaseShop main store

How-to

Display a Previous-Day Historian Average on an HMI Screen

HMI and touchscreens Published 6 min read

Short answer

Query the Historian in average retrieval mode with an explicit start and end one day apart, prove the query standalone in SQL Server Management Studio, then assign the single returned value to the display object's attribute by script. History Summary cannot select the prior-day range, so it is the wrong tool for this page.

On this page
  1. Why History Summary is the wrong tool for a fixed range
  2. Choose the method for one number
  3. Define the interval before the query
  4. What average retrieval computes
  5. Prove the query standalone, then script it
  6. Move the 2012 script to 2017 attributes
  7. Match the symptom to the layer
  8. Questions asked

Show the prior-day average with a Historian average-retrieval query whose start and end are explicit timestamps one day apart, prove that query standalone in SQL Server Management Studio, then let a script write the one returned value to the attribute a simple box displays. That is the whole job: one statistic, one box, page format untouched, and no new licensing. History Summary is the wrong starting point here because its configuration in this setup exposes no way to select the prior-day range — a limit of that data type's options, not of the Historian, which computes the number natively.

Why History Summary is the wrong tool for a fixed range #

History Summary hands you a summary through the configuration its data type supports. What it did not expose in this setup was range selection, so it cannot express "from midnight yesterday to midnight today." Stop there: the mismatch is about the option, not the statistic. The Historian computes averages over any interval through its retrieval modes, and one of them is built exactly for this. The decision falls out immediately — drop History Summary for this page and use average retrieval.

Choose the method for one number #

Method What it demands Verdict for one value and a box
Average retrieval plus script A proven query and a script that assigns the result Use this; keeps the page format, no added license
Embedded trend and query widgets The licensing option that enables them in InTouch Only if operators need trending and interaction on this page
Reporting product such as Dream Report Its own license and report configuration Not for one number
History Summary A selectable range in its configuration Cannot express a fixed prior-day interval in this setup

Keep the data sources straight while you choose. The reports page you have to match reads historical tags stored in the PLC itself, and displaying PLC CSV data logs on an HMI screen is that side of the house — a different source from the Historian this query targets. If the longer-term goal is controller history in a database, logging PLC data to SQL through a gateway keeps that load off the HMI.

Define the interval before the query #

"Previous day" has two defensible meanings and they give different answers. The prior calendar day runs from one midnight boundary to the next; a rolling window runs 24 hours back from the moment of refresh. Commit to the calendar day unless operations defines its reporting day on shift boundaries — and if it does, use those boundaries as the two timestamps, not midnight.

Write both boundaries as explicit timestamps, not relative offsets. At a 07:30 refresh on 2025-06-12, the calendar-day interval is 2025-06-11 00:00:00 to 2025-06-12 00:00:00, and its end does not move when the screen refreshes; a rolling window would instead run 2025-06-11 07:30:00 to 2025-06-12 07:30:00. Refresh on window open and once after the midnight rollover — a closed day's average cannot change intraday, so faster polling buys nothing.

What average retrieval computes #

Average retrieval is a cyclic mode: it returns one row per tag per cycle, and each row carries the time-weighted average over that cycle, stamped at the cycle start when wwTimeStampRule is set to Start as the documentation's example does. The mode is selected through wwRetrievalMode, using the average setting exactly as the Average Retrieval example query prints it. The consequence for this page: make the single cycle span the whole prior day — start and end exactly one day apart — and a single-tag query returns exactly one row.

Time-weighted means hold time counts. An illustrative temperature tag that sits at 70 °C for the first 10 hours of the day and 90 °C for the remaining 14 averages (70 °C × 10 h + 90 °C × 14 h) ÷ 24 h = 1960 ÷ 24 = 81.7 °C. Averaging the two stored values instead gives (70 + 90) ÷ 2 = 80.0 °C, a full 1.7 °C low, because it ignores how long each value held. That is the size of error the correct retrieval mode prevents, and it grows whenever values hold for unequal durations.

Prove the query standalone, then script it #

Never embed an untested query. Work in this order:

  1. Assemble the query: the tag name exactly as the Historian knows it, average retrieval mode, and the two explicit boundaries from the section above. Run it in SQL Server Management Studio against the Historian's query extension.
  2. Check the shape: one row, one value, one timestamp at the interval start. More than one row means the cycle does not span the day.
  3. Test a day with data and a day without, and note what the empty result looks like — the HMI must never present that silently as a number.
  4. Move the proven query into the application script unchanged. The script's only jobs are the connection, the execution, and assigning the returned scalar to the attribute the box displays, using the SQL access calls your project's scripts already use.
  5. Log the final binding string with LogMessage before the assignment and compare it character-for-character with an item defined in the OI-Server.
  6. Decide the failure policy now: on a failed refresh, show an explicit state or an update time, never the last value unlabeled. An operator acting on yesterday's average must never be reading a number from an older, failed refresh.

Move the 2012 script to 2017 attributes #

The tutorial pattern that stops working assigns I/O from a constructed string:

Dim dataSource As String;
dataSource = "PLCSim.tagname." + Me.Tagname;
Me.Inlet1.CLS.InputSource = dataSource + "_IV1_CLS";

The sibling assignments Me.Inlet1.OLS.InputSource and Me.Inlet1.CmdOpen.OutputDest follow the same shape, and all of these object names are the tutorial's examples, not universal names — replace them with the attributes that exist in your deployed application. What changed in 2017 is the target, not the technique: UDAs are gone, and the assignments go to object attributes. When a source string is assigned dynamically it must resolve to an item defined in the configured OI-Server; a wrong prefix or suffix there is a binding fault, not a calculation fault. Print the string first — LogMessage(dataSource + "_IV1_CLS") — and read it in LogViewer.

And do not chase the yellow exclamation marks that appear right after deployment: they mark references that are not yet assigned at deploy time. Uploading the runtime changes after deployment is what clears them; the path structure has not changed underneath you.

Match the symptom to the layer #

Symptom Layer at fault Action
Standalone query fails or returns an unexpected statistic Tag name, retrieval mode, or boundaries Fix it in Management Studio before touching the script
Query is good, the screen never updates Script binding LogMessage the string; compare with the OI-Server item list
Screen updates, number disagrees with a known day Interval definition Check calendar-day versus rolling boundaries — the 80.0 °C versus 81.7 °C kind of gap
Number goes stale silently Refresh and failure policy Re-run on open and after midnight; show a state on failure

Questions asked

Why can't History Summary show yesterday's average?

In this setup its configuration exposes no time-range selection, so it cannot express midnight-to-midnight for a chosen day. That is a limit of the data type's options, not evidence the Historian cannot compute the statistic. Average retrieval over an explicit interval returns the number, and a script binds it to the box.

Does the 2012 tutorial script with UDAs work in a 2017 application?

No. The tutorial's UDA references have no counterpart in a 2017 application; the equivalent assignments go to object attributes that exist in the deployed project. Yellow exclamation marks right after deployment are simply references not yet assigned, and uploading the runtime changes clears them, so the path structure has not secretly changed.

How far off is a plain average of stored samples?

Take a tag that holds 70 °C for 10 hours and 90 °C for the other 14: the time-weighted average is (70 × 10 + 90 × 14) ÷ 24 = 81.7 °C, while averaging the two stored values gives 80.0 °C. The 1.7 °C gap is the hold-time weighting, and it grows whenever values sit for unequal durations.

Do I need extra licensing to display one historian number?

Not for one number. Script-backed retrieval uses the SQL access your project already has. Embedded trend and query widgets require the licensing option that enables them in InTouch, and reporting products such as Dream Report carry their own license; both are justified only when operators need interaction or scheduled reports, not a single displayed value.

Keep reading

  • Troubleshooting

    Fix a PanelView Float Display Showing the Wrong Value

    The PLC is right and the display is misreading it: a PanelView numeric display shows 6040 because its tag's Data type is set to unsigned integer while F8:9 holds a 32-bit float. Set the tag to IEEE Float/REAL in the tag form editor, confirm the object uses that tag, and the display reads 1.60072.

    HMI and touchscreens

  • Troubleshooting

    Why an HMI Screen Freezes Until a Script Finishes

    The event handler runs on the single interface thread that also paints the screen, so repaints queue behind it and the status text appears only when the handler exits. Keep handlers to about 100 ms, run RFID, tag and SQL work through system.util.invokeAsynchronous(), and post display changes back with system.util.invokeLater().

    HMI and touchscreens

  • Troubleshooting

    Why HMI operation records miss PLC start stop transitions

    An HMI operation record writes an entry only when a touch changes a value, so background transitions of PLC output Y5 never reach it. Build the start/stop history from a state-driven event log on Y5 that stores both the activation and the return transition, verify the tag reads back live, and latch in the PLC any state shorter than the panel's polling period.

    HMI and touchscreens