[PE] Calculated field state is not initialized - Script CF on Device Profile
Author: DnTuziCreated Sep 3, 2026Updated Sep 8, 2026
LabelsBugfeature
Describe the bug
A Script-type Calculated Field defined on a Device Profile intermittently fails during normal runtime operation with:
Calculated field state is not initialized!
java.lang.NullPointerException: Cannot invoke "org.thingsboard.server.service.cf.ctx.state.CalculatedFieldScriptEngine.executeScriptAsync(Object[])" because "expression" is null
The error is not limited to newly created or newly assigned devices — it occurs during ongoing operation for multiple devices that already share the same device profile and were previously processing telemetry without issues.
Steps to Reproduce
- Create a Script-type Calculated Field on a Device Profile (in our case "V-Matic"), with:
count— Time series rolling, current entity, keyadminProdProcess...designSpeed— Attribute, current entity, keyadminMachDesignS...state— Time series rolling, current entity, keystatusStateCurrent
- Let telemetry flow normally to multiple devices assigned to this profile. The CF works correctly for a period of time.
- At some point (not tied to a specific reproducible trigger we've identified yet), the CF starts throwing the NPE above for telemetry from several devices under the same profile simultaneously.
- Workaround: Opening the Calculated Field in the UI, making a trivial change (or no change) and saving it again immediately resolves the issue — the CF resumes working correctly for a period of time before the error reappears.
Expected behavior
The Calculated Field context (CalculatedFieldCtx, including the compiled TBEL expression/engine) should remain valid for the lifetime of the CF configuration and should not need to be manually re-saved to keep functioning.
Observations / Suspected root cause
- Since the CF is defined at Device Profile level, all devices under that profile share the same
CalculatedFieldCtx(per theCalculatedFieldManagerMessageProcessorarchitecture, which is per-tenant and maintains one context per CF). - The fact that re-saving fixes it for all affected devices at once, and it later breaks again for multiple devices at once, strongly suggests the shared context itself is being invalidated/evicted or loses its compiled
expression/script engine at some point during runtime — rather than an issue with any specific device's local state. - This looks distinct from the NPE hardening shipped in PE v4.3.1.3 ("Hardened calculated field owner init against NPE and reduced DB load"), which appears to target owner (tenant/customer) resolution during first-contact initialization, not runtime context invalidation for profile-level Script CFs.
Environment
- ThingsBoard Version: 4.3.1.3 PE
- Calculated Field type: Script (TBEL)
- Scope: Device Profile-level CF (not single-device)
- Arguments: current-entity Time series rolling (x2) + current-entity Attribute (x1)
- Cluster setup: (please fill in: monolith vs. microservices/Kafka queue mode, number of rule-engine instances)
Additional context
- Error recurs across multiple devices under the same profile at overlapping times, not just isolated/new devices — pointing to a shared-context problem rather than per-device initialization race.
- (please attach: exact timestamps of failures vs. any CF edits, rule-engine restarts, deployments, or cluster rebalancing events if available; full stack trace; server logs around the failure window)
Source: thingsboard/thingsboard