# Dynamic values

The `value` key of a condition can be *dynamic* — useful when the value to be compared against the `field` isn't known in advance, or depends on another field in the payload.

To leverage dynamic values for a rule's conditions, you need to wrap the reference field in double curly brackets: `{{<reference_field>}}` where `<reference_field>` is the resource's field (expressed in dot notation) that will be used as a variable to calculate the value. You can also apply some basic aggregation operators to the reference field (as shown in the [examples](#examples) below):

<table><thead><tr><th width="112.16796875">Operator</th><th width="124.23828125">Field type<select><option value="QKGAwHxrkmo3" label="Boolean" color="blue"></option><option value="dQ6wiIf2uzZM" label="String" color="blue"></option><option value="Lmv1dyb7gtGF" label="Number" color="blue"></option></select></th><th width="250.34765625">Description</th><th width="511.76953125">Example value</th></tr></thead><tbody><tr><td><strong><code>min</code></strong></td><td><span data-option="Lmv1dyb7gtGF">Number</span></td><td>The smallest of the reference field values is used as the condition's value.</td><td><code>"{{min(order.line_items.shipment.available_shipping_methods.price_amount_cents)}}"</code></td></tr><tr><td><strong><code>max</code></strong></td><td><span data-option="Lmv1dyb7gtGF">Number</span></td><td>The biggest of the reference field values is used as the condition's value.</td><td><code>"{{max(order.line_items.compare_at_amount_cents)}}"</code></td></tr><tr><td><strong><code>avg</code></strong></td><td><span data-option="Lmv1dyb7gtGF">Number</span></td><td>The average of the reference field values is used as the condition's value.</td><td><code>"{{avg(order.line_items.sku.inventory.levels.quantity)}}"</code></td></tr></tbody></table>

{% hint style="info" %}
The reference field type must be consistent with the [matcher](/rules-engine/matchers.md) used against the field and value. Given the currently available functions, if you want to add one of the operators above, the reference field type must be numeric.
{% endhint %}

## Examples

### Direct dynamic value

The following condition uses a dynamic value to check if an order's line items have the same unit amount and *compare-at* amount (i.e. the actual selling price is equal to the full price displayed to the customer with a strikethrough — meaning the item is not on sale):

<pre class="language-json"><code class="lang-json">"conditions": [
  {
    "field": "order.line_items.unit_amount_cents",
    "matcher": "eq",
<strong>    "value": "{{order.line_items.compare_at_amount_cents}}"
</strong>  }
]
</code></pre>

### Dynamic value with operator

The following condition uses a dynamic value with the `min` operator to target the shipment line items associated with the cheapest shipping method among the ones available for an order's shipments:

<pre class="language-json"><code class="lang-json">"conditions": [
  {
    "field": "order.line_items.shipment.cost_amount_cents",
    "matcher": "eq",
<strong>    "value": "{{min(order.line_items.shipment.available_shipping_methods.price_amount_cents)}}"
</strong>  }
]
</code></pre>

## Date-based values

Dynamic values also support date expressions, making it straightforward to write conditions that are evaluated against the current date or against a date field from the payload. This is particularly useful for time-sensitive promotions — such as birthday discounts, seasonal offers, or deals tied to specific order timestamps — without hard-coding any dates in your rules.

Date expressions follow the same double curly brackets `{{...}}` notation. The available functions accept a `<date>` argument and optionally an `<interval>`:

<table><thead><tr><th width="279.3515625">Expression</th><th width="112.453125">Field type<select><option value="QKGAwHxrkmo3" label="Boolean" color="blue"></option><option value="dQ6wiIf2uzZM" label="String" color="blue"></option><option value="Lmv1dyb7gtGF" label="Number" color="blue"></option><option value="aLyiurAPk9rF" label="Datetime" color="blue"></option></select></th><th width="277.9453125">Description</th><th width="265.08984375">Example value</th></tr></thead><tbody><tr><td><strong><code>today</code></strong></td><td><span data-option="aLyiurAPk9rF">Datetime</span></td><td>The current date (<a href="https://en.wikipedia.org/wiki/Coordinated_Universal_Time">UTC</a>), resolved once per rule evaluation. It's a costant, no additional argument accepted.</td><td><code>"{{today}}"</code></td></tr><tr><td><strong><code>day(&#x3C;date>)</code></strong></td><td><span data-option="aLyiurAPk9rF">Datetime</span></td><td>The day of the month (1 – 31) extracted from <code>&#x3C;date></code>. When used as a value, the engine compares the day part of the <code>field</code> date against this number.</td><td><code>"{{day(today)}}"</code></td></tr><tr><td><strong><code>month(&#x3C;date>)</code></strong></td><td><span data-option="aLyiurAPk9rF">Datetime</span></td><td>The month (1 – 12) extracted from <code>&#x3C;date></code>. When used as a value, the engine compares the month part of the <code>field</code> date against this number.</td><td><code>"{{month(order.updated_at)}}"</code></td></tr><tr><td><strong><code>year(&#x3C;date>)</code></strong></td><td><span data-option="aLyiurAPk9rF">Datetime</span></td><td>The year extracted from <code>&#x3C;date></code>. When used as a value, the engine compares the year part of the <code>field</code> date against this number.</td><td><code>"{{year(today)}}"</code></td></tr><tr><td><strong><code>{{birthday}}</code></strong></td><td><span data-option="aLyiurAPk9rF">Datetime</span></td><td>Matches any date whose day and month equal today's, regardless of year. A year-agnostic shorthand for combining <code>{{day(today)}}</code> and <code>{{month(today)}}</code> in a single expression.</td><td><code>"{{birthday}}"</code></td></tr><tr><td><strong><code>{{range(&#x3C;date>, &#x3C;interval>)}}</code></strong></td><td><span data-option="aLyiurAPk9rF">Datetime</span></td><td>Defines a date window of <code>±&#x3C;interval></code> around <code>&#x3C;date></code>. Learn more <a href="#date-ranges">here</a>.</td><td><code>"{{range(today, 7.days)}}"</code></td></tr></tbody></table>

The `<date>` argument can be either:

* A **constant** — `today`, which resolves to the current date (UTC) at evaluation time.
* A **payload field path** — any date or datetime field from the current payload, expressed in dot notation (e.g. current order's timestamps such as `order.created_at`, `order.updated_at` etc.). The value is resolved per subject from the payload.

{% hint style="info" %}
Even if all date expressions operate on `field` values of type *Datetime*, when a date-part function is used as the value (e.g. `{{month(today)}}`), the engine automatically extracts the same part from the field date before comparing — no additional operator is needed on the field side.
{% endhint %}

### Date ranges

The `range(<date>, <interval>)` expression resolves to a pair of dates representing a symmetric window centered on `<date>`:

```
[date - interval, date + interval]
```

{% hint style="info" %}
Because the value is an array of two dates, it must always be used with the `gteq_lteq` matcher, which checks that the `field` date falls within the range (inclusive on both ends).
{% endhint %}

The `<interval>` must follow the format `n.<unit>`, where `n` is a positive integer and `<unit>` is one of `days`, `months`, or `years`. For example:

* `{{range(order.updated_at, 7.days)}}` — 7-day window around the order's latest update
* `{{range(today, 2.months)}}` — from 2months ago to 2 months from now
* `{{range(today, 1.year)}}` — from 1 year ago to 1 year from now

### Examples

#### Birthday month discount

The following condition lets you apply a promotion to all customers whose birthday falls in the current month by comparing the month of the `birthdate` field in the customer's metadata with the current month (i.e. the engine extracts the month from `customer.metadata.birthdate` and checks whether it equals the current month as integers):

<pre class="language-json"><code class="lang-json">"conditions": [
  {
<strong>    "field": "customer.metadata.birthdate",
</strong>    "matcher": "eq",
<strong>    "value": "{{month(today)}}"
</strong>  }
]
</code></pre>

#### Birthday discount (exact day and month)

You can activate a promotion on a customer's actual birthday by combining two conditions:

<pre class="language-json"><code class="lang-json">"conditions": [
  {
<strong>    "field": "customer.metadata.birthdate",
</strong>    "matcher": "eq",
<strong>    "value": "{{month(today)}}"
</strong>  },
  {
<strong>    "field": "customer.metadata.birthdate",
</strong>    "matcher": "eq",
<strong>    "value": "{{day(today)}}"
</strong>  }
]
</code></pre>

To do that, you can also leverage the `birthday` shorthand:

<pre class="language-json"><code class="lang-json">"conditions": [
  {
<strong>    "field": "customer.metadata.birthdate",
</strong>    "matcher": "eq",
<strong>    "value": "{{birthday}}"
</strong>  }
</code></pre>

#### Promotion for orders created in a specific date range

The following condition lets you activate a promotion for orders created within a 15-day window around the current date:

<pre class="language-json"><code class="lang-json">"conditions": [
  {
<strong>    "field": "order.created_at",
</strong>    "matcher": "gteq_lteq",
<strong>    "value": "{{range(today, 15.days)}}"
</strong>  }
]
</code></pre>

#### Match birthdate month with the order creatiom month

The following condition lets you apply a discount when the customer's birth month coincides with the month in which the order was created (i.e. the engine extracts the month from both the `field` date and the value expression, then compares them as integers):

<pre class="language-json"><code class="lang-json">"conditions": [
  {
<strong>    "field": "customer.metadata.birthdate",
</strong>    "matcher": "eq",
<strong>    "value": "{{month(order.created_at)}}"
</strong>  }
]
</code></pre>


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.commercelayer.io/rules-engine/conditions/dynamic-values.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
