Troubleshooting
Fix Ignition custom historian aggregates that return zeros
Short answer
A zero-filled result from an Ignition custom historian aggregate means the callback failed, not that every stored value was zero. The Gateway log names the cause: NameError: global name 'shared' is not defined, raised in pyaggimpl. Select the project that contains your aggregate's script library as the Gateway Scripting Project, publish it, and rerun the query.
On this page
- Read the zero-filled result together with the Gateway log
- Point the Gateway Scripting Project at the project with the script library
- Match the script reference and test on known samples
- Worked example: a 24 h day at one sample per minute, zeros and bad quality excluded
- Why the result is a table of rows, not one number
- When zeros survive the fix
- Questions asked
The zeros are not your data. When a custom tag history aggregate fails inside the Gateway, the historian still builds its timestamped result table and fills every interval with zeros, while the real fault — NameError: global name 'shared' is not defined — appears only in the Gateway logs under com.inductiveautomation.ignition.common.script.JythonExecException. The script that ran the query reports nothing. The fix is not in the calculation and not in the historian's license: select the project that contains the aggregate's script library as the Gateway Scripting Project, publish that project, and rerun the query. Then read what the callback returns with a rule that keeps the numerator and the denominator honest: a nonzero daily average accepts a sample only when its quality is good and its value is not zero, and the same accepted set feeds both the sum and the count. Work the fault in that order: log, project setting, arithmetic.
Read the zero-filled result together with the Gateway log #
A returned dataset proves the query produced a result structure; it does not prove the custom calculation ran. In this fault the callback throws before it processes a single sample, and the historian fills each interval of the result with zeros anyway. Check both places, and let the log decide:
- Timestamped rows of zeros in the query result — the shape of a failed callback, not a measurement. Interpret them only after reading the log.
NameError: global name 'shared' is not definedin the Gateway logs — the scope executing the aggregate cannot resolve the script-library name the script references.- No error in the script that ran the query — expected here. The callback executes in the Gateway's scope when the historian runs the aggregate, not inside your calling script, so the caller stays clean while the log takes the exception.
- An active historian license — not the discriminator either. In the case behind this article the historian was licensed and the zeros still came back.
The logged traceback places the failure at File "<string>", line 2, in pyaggimpl; the actionable part is the NameError. Run the failing query once, note the time, and match it to the Gateway log entry: that ties the displayed zeros to a failed callback before you touch the calculation. A NameError like this is a script-scope fault with a known shape, and the knowledge base walkthrough of the Ignition 8 NameError for a missing shared script library follows the same name-resolution failure to its fix.
Point the Gateway Scripting Project at the project with the script library #
A project's script library and the Gateway's script execution context are separate concerns. Putting a function in a project does not by itself make that library visible to Gateway-scoped execution: Python resolves shared in the active execution scope and does not search every project until it finds a match. A correct function referenced from the wrong project context fails before it processes a single historical sample. The Gateway Scripting Project setting selects which project's script resources supply that context.
- Identify the project whose script library holds the functions the aggregate calls — in this case a library named
shared. - In the Gateway configuration, locate the setting that selects the Gateway Scripting Project, the project whose scripts the Gateway uses when it executes code in its own scope.
- Select that project and save.
- Publish the project through your normal project workflow so the script resources reach the Gateway.
- Rerun the query and check the Gateway logs: the name-resolution error should be gone and the result should carry calculated values instead of zeros.
One caution before step 3: this setting steers every script the Gateway executes in its own scope, so on a running system check what the existing Gateway scripts resolve before you change it. Proceed once the reference resolves cleanly and the result holds real values.
Match the script reference and test on known samples #
Selecting the right project will not repair a misspelled reference. Compare the aggregate's configured script path with the actual library location and function name in that project, including capitalization and nesting, and check the callback's required arguments and return type against the custom tag history aggregates page of the installed Ignition documentation. Then run a short query over an interval whose samples you know: confirm the callback executes and returns the expected number before widening the range. A clean small run is your dependency check — the name resolves, the arguments match, and the historian accepts the return type.
Interpolation belongs in the same test. Turning it off changes which samples the callback receives — recorded samples only — and it neither causes nor fixes the name-resolution error. Keep interpolation off while validating an average that is meant to use recorded samples, and inspect the actual inputs for boundary values.
Worked example: a 24 h day at one sample per minute, zeros and bad quality excluded #
The case is the one this fault comes from: a daily average for one tag with zero values omitted, plus a count of the good-quality rows behind it. The inputs below are illustrative — the tag is a tank level in mm; take your own counts and sums from your stored rows.
- Range: one day at one sample per minute, interpolation off: 24 h × 60 min/h = 1,440 samples.
- Bad-quality samples: 43.
- Good-quality samples whose value is exactly 0 mm: 12.
- Sum of accepted sample values: 166,200 mm.
Rule in words: nonzero average = sum of accepted values ÷ number of accepted samples, where a sample is accepted only when its quality is good and its value is not zero — and the same eligibility test governs numerator and denominator.
Arithmetic:
- Good-quality samples: 1,440 − 43 = 1,397.
- Accepted after dropping exact zeros: 1,397 − 12 = 1,385.
- Nonzero daily average: 166,200 mm ÷ 1,385 = 120.0 mm. Check: 1,385 × 120.0 = 166,200.
- The count aggregate returns
cnt= 1,385 — not 1,397 and not 1,440 — because it counts the same accepted set the average divides by.
Result: a 120.0 mm daily average backed by 1,385 accepted samples. Decision: divide only when the accepted count is greater than zero; when an interval accepts zero samples, return the empty-result representation the aggregate interface documents instead of a zero average, so "no qualifying data" never reads as a measured 0 mm. Validate the sum and the count against a manually inspected sample set — a good-quality-row count alone will not do it, because it still includes the zeros.
Quality matters twice. Ordinary historian queries already drop bad-quality updates, the behavior behind why a bad quality update hides historian values, but your callback must apply its own quality test to both the running sum and the count. Exact-zero exclusion is also not the same as rejecting near-zero noise; a tolerance would be its own process decision.
Why the result is a table of rows, not one number #
The callback computes one value for the interval it is handed; the query decides how many intervals the result holds. A scalar from one callback invocation does not require the whole query to return a single scalar — in the fault that opened this article the result held one row per timestamp, each carrying the failed callback's zero. Two things must be right at once: the callback runs, and the query's range and aggregation interval produce the shape you want. For a single daily number, set the range to the day and the aggregation interval so the result holds one row, define the day's start and end in the reporting time zone you intend, and read the returned timestamps to confirm. Row counts are also bounded by the query's own result-size controls — the same limit behind a flat historian trend from the Query Row Limit.
Keep interpolation off for the sample-based average above. A sample average weights each accepted sample equally; a time-weighted average weights values by duration and needs its own explicit rule for the gaps between samples.
When zeros survive the fix #
Once the name-resolution error is gone, compare the aggregate against raw history before trusting it. Pull one known tag, value and timestamp from the stored rows and check the callback's output for that interval by hand. If the callback now runs and genuine zeros still reach the average, the stored history itself contains them — a reconnecting edge node that writes zero on reconnect is a distinct fault with its own fix, described in why the historian stores zero after an MQTT edge node reconnects. The two look different: callback zeros arrive uniform across every interval and come with a log entry; data zeros sit in the raw rows before any aggregation.
Questions asked
Do I need to relicense or reinstall the historian when a custom aggregate returns zeros?
No. In the case behind this article the historian was licensed and still returned the zero-filled table. The Gateway log line discriminates: a NameError raised inside pyaggimpl is a script-scope fault, not a licensing fault. Confirm the license state in the Gateway configuration only after the script error is cleared and the result is still wrong.
Can the script library stay in the project where I build my screens?
Yes. Any project can be selected as the Gateway Scripting Project; what matters is that the selected project contains the script library and function the aggregate references, and that the project is published so the Gateway can see the resources. If you later move the library to a different project, update the setting and publish again — otherwise the same NameError returns.
How do I exclude near-zero noise instead of exact zeros from the average?
Replace the equals-zero test with a deadband: accept a sample when its absolute value sits above the deadband you chose. Size that deadband from the instrument's accuracy and the process question you are answering, not from the average's output. Apply it to the eligibility of both the running sum and the count, exactly as with exact-zero exclusion, and keep bad-quality samples out regardless of their value.