DAS Script State Machines, Edge Detection and Recovery
Implement repeat suppression with explicit transitions and test sequences.
Make the state transition explicit
Prerequisite: custom objects and bounded loops. A condition remaining true differs from a condition becoming true. A threshold observer can either report every true sample or report only on transition. This lesson implements an edge detector using invented numbers. Its latches are local programming state, not distributed locks or transaction guarantees.
Define the machine
States: 0 means below threshold; 1 means already reported above threshold. Transition 0→1 emits one report; 1→1 emits none; a value below threshold returns to 0. Equality is defined here as neither triggering nor rearming. This explicit equality rule prevents an accidental change in behavior when replacing > with >=.
A deterministic test program
$feSequence = NewUserObj();
$feSequence.Set(0, 9);
$feSequence.Set(1, 11);
$feSequence.Set(2, 12);
$feSequence.Set(3, 8);
$feSequence.Set(4, 11);
$feThreshold = 10;
$feEdgeState = 0;
$feEdgeReports = 0;
$feStep = 0;
while ($feStep < 5) {
$feObserved = $feSequence.Get($feStep);
if ($feObserved > $feThreshold) {
if ($feEdgeState == 0) {
$feEdgeState = 1;
$feEdgeReports = $feEdgeReports + 1;
}
} else {
if ($feObserved < $feThreshold) {
$feEdgeState = 0;
}
}
$feStep = $feStep + 1;
}
MsgBox($feEdgeReports);
Walk through the sequence
9 leaves state at 0. 11 changes to 1 and emits report 1. 12 stays at 1. 8 rearms to 0. The last 11 emits report 2. The expected result is two reports. In a Timer callback, keep state between invocations and process one sample per invocation; do not recreate state=0 inside every callback.
Recovery and ownership
Bind the machine to its input identity. Changing symbol, interval, account or threshold invalidates the old interpretation. Define whether startup above threshold reports immediately or waits for a below-threshold observation. Missing or stale data should move to an explicit unknown/disabled policy, not silently look like zero. A busy flag can prevent your own sequential duplicate actions but cannot guarantee atomic behavior across independent callbacks or survive a process restart.
Exercise and worked answer
Replace the sequence with 11,10,11,9,11. Answer under this policy: two reports, because equality does not rearm. If equality should rearm, change the rule deliberately and the expected count becomes three. A stronger hysteresis design uses different trigger and reset thresholds, for example >10 to trigger and <9 to reset; values in between preserve state.