Nodes/ComfyUI-Counternodes/Interval Counter B
ComfyUI Node

Interval Counter B

Interval Counter with a math mode — transform the count instead of just adding one

By GHOSTLXH·Created 2 years ago·Updated 2 years ago· 11
Interval Counter B
    • INT
    resetfalse
    modeincrement
    min_value0
    max_value100
    step1
    trigger_interval3
    tick0
    expressionvalue + 1

    Interval Counter B is the plain Interval Counter with one extra mode bolted on: expression. Same interval-then-count logic, same persisted state, same tick input - but instead of being limited to increment, decrement, and jumps, you can type a math expression that transforms the counter's value however you like. Multiply it, square it, flip it negative, do modulo tricks. For anything fancier than "+1 every N runs," this is the node to grab.

    The pack's A and B counters are a recurring theme: B is the slightly smarter variant of A in every case. If you're choosing between the two counters, decide on the expression mode first - if you don't need it, the plain one is simpler and has nothing extra to misconfigure.

    How it works

    The interval machinery is identical to Interval Counter: it counts executions, and only when the count reaches trigger_interval does it do something. Modes are the same four plus expression, and the expression is a Python-ish string where value stands for the current counter. The default is "value + 1", which is just increment with extra steps. Change it to "value * 2" for doubling, "value + 10" for a bigger stride, or "value % 5" to keep a number cycling 0–4. After evaluation the result is clamped into min_valuemax_value, and here those bounds go negative (unlike the plain A counter, which floors at 0).

    On the safety question: the README warns that the expression input will happily execute malicious code, and you should treat that as the rule - only ever feed it strings you wrote yourself, never anything from an untrusted source. As shipped, the code is actually more constrained than the warning implies: it evaluates via ast.literal_eval and an allowlist of + - * / ** % operators rather than running raw Python. That's reassuring, but it's an allowlist, not a sandbox, and the author clearly wants you nervous. Be nervous. Keep the expression a literal you typed.

    Inputs and outputs

    All of Interval Counter's inputs plus expression (a STRING). So: reset, mode (now five choices), min_value, max_value, step, trigger_interval, tick, and expression. Output is a single INT.

    Two inputs to actually care about as a beginner: trigger_interval (how many rounds between updates) and expression (what the update does). step only applies in the increment/decrement modes. And remember the tick port from the A version - feed it a value that changes every run, or ComfyUI caches the node and the counter never advances. tick isn't part of the math; it exists purely to keep the node re-executing.

    Installing it

    It ships with the ComfyUI-Counternodes pack, so one install covers all six nodes. ComfyUI Manager: search ComfyUI-Counternodes, install, restart. Or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/GHOSTLXH/ComfyUI-Counternodes
    

    No models, no extra Python packages - just torch, which ComfyUI already has.

    Gotchas

    The state file trap from the A counter is here too: it persists to interval_counter_b_state.json in the pack folder, survives restarts, and is shared between every workflow using the node. Count starting in the middle of your series? Flip reset true once or delete the JSON. Also, because the expression is clamped after evaluation, a "value * 2" that overshoots max_value just sits at the ceiling instead of erroring - fine, but it means your expression's "end behavior" is a plateau, not a crash.

    CategoryCustom Nodes/Counters

    Inputs (8)

    NameTypeDefaultDescription
    resetBOOLEANfalse
    modeCOMBOincrement5 options: increment, decrement, inc_to_max, dec_to_min, expression
    min_valueINT0-10000–10000
    max_valueINT100-10000–10000
    stepINT11–10000
    trigger_intervalINT31–10000
    tickINT00–999999
    expressionSTRINGvalue + 1

    Outputs (1)

    NameTypeDescription
    INTINT