> For the complete documentation index, see [llms.txt](https://docs.heisenware.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.heisenware.com/reference/functions/utilities/counter.md).

# Counter

With a counter, you maintain a numerical value, for example a production count or sequence number. You can increment, decrement, set, and reset the count, and react to every change with `onChange`. You must create an instance of the counter to use it. The count lives in memory only: after a restart of the platform the counter starts from its initial value again. The code class name is `Counter`.

### `create`

Creates a new counter instance, optionally starting from an initial value.

#### Parameters

<table><thead><tr><th width="150">Input</th><th>Description</th><th width="100">Type</th></tr></thead><tbody><tr><td><code>initial</code></td><td>The number to start counting from and to reset to. Default 0.</td><td>number</td></tr></tbody></table>

#### Output

Returns the name of the created instance.

#### Example

```yaml
# initial
10
```

### `delete`

Deletes a counter instance.

#### Parameters

None.

#### Output

Returns `true` upon removal.

{% hint style="danger" %}

#### Irreversible action

Deleting removes the instance configuration.
{% endhint %}

### `increment`

Adds a step to the count.

#### Parameters

<table><thead><tr><th width="150">Input</th><th>Description</th><th width="100">Type</th></tr></thead><tbody><tr><td><code>step</code></td><td>The amount to add. Default 1.</td><td>number</td></tr></tbody></table>

#### Output

Returns the new count as a number.

### `decrement`

Subtracts a step from the count.

#### Parameters

<table><thead><tr><th width="150">Input</th><th>Description</th><th width="100">Type</th></tr></thead><tbody><tr><td><code>step</code></td><td>The amount to subtract. Default 1.</td><td>number</td></tr></tbody></table>

#### Output

Returns the new count as a number.

### `setCount`

Sets the count to a value.

#### Parameters

<table><thead><tr><th width="150">Input</th><th>Description</th><th width="100">Type</th></tr></thead><tbody><tr><td><code>count</code></td><td>The new count.</td><td>number</td></tr></tbody></table>

#### Output

Returns the new count as a number.

### `reset`

Resets the counter back to its initial value. You can optionally provide a new initial value, including 0, to use for this and all future resets.

#### Parameters

<table><thead><tr><th width="150">Input</th><th>Description</th><th width="100">Type</th></tr></thead><tbody><tr><td><code>initial</code></td><td>An optional new initial value.</td><td>number</td></tr></tbody></table>

#### Output

Returns the count after the reset as a number.

#### Example

```yaml
# initial
100
```

### `getCount`

Retrieves the current value of the counter.

#### Parameters

None.

#### Output

Returns the current count as a number.

## Event listeners

Triggering an event executor subscribes it; triggering it again with the same inputs changes nothing. The payload arrives on the executor's event outputs, not on its return. A subscription follows the executor's inputs and address, pauses while the App is stopped and ends with its scope: a user's with the user's last connection, a shared one never by itself. To end it by hand, choose **Stop listening** in the menu of the executor's event input; the next trigger subscribes again. See [Backend events](https://docs.heisenware.com/concepts/backend-events.md).

### `onChange`

Subscribes to the `change` event. The callback runs whenever the count changes through `increment`, `decrement`, `setCount`, or `reset`. A call that leaves the count as it was is silent.

#### Parameters

<table><thead><tr><th width="150">Input</th><th>Description</th><th width="100">Type</th></tr></thead><tbody><tr><td><code>listener</code></td><td>The callback function.<br>Payload: <code>count</code>, the count after the change, and <code>previous</code>, the count before it.</td><td>callback</td></tr></tbody></table>

#### Output

Returns `true` once the executor is listening. The values arrive on the event outputs.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.heisenware.com/reference/functions/utilities/counter.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
