IntermediateLesson 6 of 127 min read

Automations: Triggers, Conditions, Actions and the Trap of Repeated Alerts

Build a small automation engine with triggers, conditions and actions, see why "when stock is below 10" fires on every update unless you make it fire only on crossing, catch a rule that triggers itself, and check the outbox against an independent replay.

In this lesson you will

  • Describe a rule as trigger, condition and action
  • Distinguish level-triggered and edge-triggered rules
  • Prevent loops with a depth limit
  • Verify the outcome with an independent replay

About 28 minutes

Tell Tomás before the rice runs out

The most valuable part of an app is often what it does without being asked. Automation platforms are built from rules with three parts. A trigger says when to look, for example when a record is created or changed. A condition says whether to continue, such as “quantity below 10”. An action says what to do: send a message, change a field, create a record. Tomás wants three rules. When the stock of an item falls below its threshold, alert him. When a shift becomes full, mark it full and tell the people on the waiting list. And whenever a stock record changes, stamp it with a last-changed time.

Two traps catch nearly everyone. The first is the difference between level-triggered and edge-triggered rules. “When quantity is below 10, send an alert” fires on every update of a record that is below 10, so five updates of a nearly empty shelf send five alerts and teach Tomás to ignore the alerts. An edge-triggered rule fires only when the condition becomes true, that is, when the quantity crosses from 10 or more to below 10, once. The second trap is the loop: a rule that changes a record triggers the “record changed” trigger again, and a rule that stamps the last-changed time on every change triggers itself for ever, or until the platform stops it. Engines protect themselves with a depth limit, and good rules avoid the loop by checking that the change they would make is a change.

Rule style Fires when Risk
Level-triggered Condition true at any update Repeated alerts
Edge-triggered Condition changes from false to true Misses a still-low shelf after a restart
Self-modifying Rule updates the record that triggered it Infinite loop
Idempotent action Same input, same effect, once Duplicate messages avoided
import json
class Engine:
    def __init__(self, rules, max_depth=3):
        self.rules, self.max_depth, self.outbox, self.log, self.records = rules, max_depth, [], [], {}
    def update(self, key, new, depth=0):
        old = dict(self.records.get(key, {})); rec = {**old, **new}; self.records[key] = rec
        if depth >= self.max_depth: self.log.append("depth limit reached at " + key); return
        for r in self.rules:
            if r["when"](old, rec): r["do"](self, key, old, rec, depth)
def alert(e, key, old, rec, depth): e.outbox.append(("alert", key, rec["qty"]))
def stamp(e, key, old, rec, depth):
    if rec.get("changes", 0) < 3: e.update(key, {"changes": rec.get("changes", 0) + 1}, depth + 1)     # touches the record, so it triggers itself
low_level = {"when": lambda old, rec: rec.get("qty", 99) < 10, "do": alert}                        # level-triggered
low_edge = {"when": lambda old, rec: rec.get("qty", 99) < 10 and old.get("qty", 99) >= 10, "do": alert}   # edge-triggered
loopy = {"when": lambda old, rec: "qty" in rec, "do": stamp}
events = [("rice", 25), ("rice", 12), ("rice", 9), ("rice", 8), ("rice", 7), ("beans", 40), ("rice", 30), ("rice", 6), ("beans", 5), ("beans", 5)]
def run(rules, depth=3):
    e = Engine(rules, depth)
    for key, q in events: e.update(key, {"qty": q})
    return e
level, edge = run([low_level]), run([low_edge])
print("level-triggered alerts:", len(level.outbox), level.outbox)
print("edge-triggered alerts :", len(edge.outbox), edge.outbox)
loop = run([loopy], depth=3)
print("self-triggering rule: depth limit hit", sum(1 for l in loop.log if "depth limit" in l), "times | changes counter of rice:", loop.records["rice"]["changes"])
safe = run([loopy], depth=50)
print("same rule with a guard (stops at 3 changes), no depth limit needed: rice changes =", safe.records["rice"]["changes"], "| limit hits:", sum(1 for l in safe.log if "depth limit" in l))
print("DATA " + json.dumps({"level": len(level.outbox), "edge": edge.outbox, "limit": sum(1 for l in loop.log if "depth limit" in l), "changes": loop.records["rice"]["changes"], "safe": safe.records["rice"]["changes"]}))
level-triggered alerts: 6 [('alert', 'rice', 9), ('alert', 'rice', 8), ('alert', 'rice', 7), ('alert', 'rice', 6), ('alert', 'beans', 5), ('alert', 'beans', 5)]
edge-triggered alerts : 3 [('alert', 'rice', 9), ('alert', 'rice', 6), ('alert', 'beans', 5)]
self-triggering rule: depth limit hit 2 times | changes counter of rice: 3
same rule with a guard (stops at 3 changes), no depth limit needed: rice changes = 3 | limit hits: 0

The engine holds records, applies each update, and runs every rule whose condition fits the change from the old record to the new one. Ten stock updates were replayed through it. With the level-triggered rule, “alert whenever the quantity is below 10”, the outbox received 6 alerts: rice at 9, 8 and 7, then again at 6, and beans at 5 twice, since each update of a low shelf triggered it again. With the edge-triggered rule, which fires only when the quantity crosses from 10 or above to below 10, the outbox holds exactly 3: rice at 9, rice at 6 after it was restocked to 30, and beans at 5. The three alerts are the three events that a person needs to act on, where the six of the level rule would have taught Tomás to ignore his phone.

The third rule touches the record each time it fires, which is a change, which triggers the rule again. In the engine with a depth limit of 3, the chain was stopped by the limit 2 time(s) and the changes counter reached 3, a sign that the rule did work a platform did not intend. A rule that checks whether it still has something to do avoids the loop: the guarded version stops at three changes by its own condition, and with a generous depth limit of 50 the limit was never hit, the counter ended at 3. Platforms usually have such a limit and a run history, and the wise habit is to make every action idempotent and every rule able to see that its work is already done.

Independent check: the alerts were counted by replaying the ten events through a ten-line loop that remembers the last quantity of each item and applies the two conditions directly, without the engine. The loop finds 6 level-triggered alerts and exactly the three edge events, rice at 9, rice at 6 and beans at 5, the same as the engine’s outbox. The engine’s run history, a list of the rules that fired, is what you would read in a platform to debug why an alert did not arrive, or why four arrived.

Check by hand: the rice quantities are 25, 12, 9, 8, 7, 30, 6, and the ones below 10 are 9, 8, 7 and 6, which is four, plus beans at 5 twice, which is six in all. The crossings from 10 or above to below 10 are 12 to 9, 30 to 6, and the first beans update below 10, 40 to 5. The second beans update at 5 stays below, so it does not fire the edge rule.

Test automations with real sequences

A rule can look right on one update and misbehave on five. Replay a realistic series of events and count the actions, as the lesson does.

Prefer edge-triggered alerts, and log every run

Alert on change, not on state, and keep a log of what ran and why. When an automation misbehaves, the run history is how you find out.

About this course

No-code platforms, plans and limits change often, so the lessons describe categories of tools with generic names and you must check each vendor’s current website. The ideas of this course, data tables, forms, views, automations and permissions, are what the tools implement, so the lesson reproduces them in small programs, run in Python 3.13 or Node.js 22 on sample data, with a second independent calculation. Platforms differ in what triggers fire, how loops are limited and how many runs are included in a plan, so check the documentation and the limits of your plan.

Practice

Add a rule that fires when a shift becomes full, and test it with five signups. Make the low-stock alert fire again when the quantity is restocked above 10 and falls again, and test it. Write the sequence of events that would send an alert for every item at 8:00 on Monday.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

What are the three parts of a rule?

  1. Table, view, form
  2. Name, type, value
  3. Trigger, condition, action
  4. Read, write, delete

When, whether, what.

What does an edge-triggered alert do?

  1. Fires on every update while true
  2. Fires when the condition becomes true
  3. Never fires
  4. Fires at night

Once per crossing.

Why is a self-modifying rule dangerous?

  1. It is slow to write
  2. It can trigger itself in a loop
  3. It deletes data
  4. It is forbidden

Use guards and depth limits.

What does idempotent mean for an action?

  1. Repeating it has the same effect as doing it once
  2. It runs at night
  3. It is fast
  4. It is free

Safe to repeat.

Why replay events through a simple loop as well?

  1. To check the engine independently
  2. To avoid automations
  3. To save time
  4. It is required

The loop agrees.

Summary

  • A rule has a trigger, a condition and an action.
  • Edge-triggered rules fire on change, level-triggered on state.
  • Prevent loops with guards and depth limits.
  • Verify with a replay and a log.

Next lesson

Lesson 7 covers users, roles and permissions.

Questions

How do I stop duplicate messages?

Make the rule edge-triggered, store a flag when you have sent the message, and check it before sending. Many platforms also offer a "run once" option.

Can automations call other systems?

Most platforms can send emails and messages and call web addresses, and some connect to other apps through connectors. Check what your plan includes.

How many runs do I get?

Plans limit automation runs per month, and each rule firing counts. Count your expected runs, and prefer edge-triggered rules, which fire less.

Progress is saved in this browser.