# Kibana Watcher actions html body if list exists condition

**URL:** https://discuss.elastic.co/t/kibana-watcher-actions-html-body-if-list-exists-condition/311934
**Category:** Kibana
**Tags:** elastic-stack-alerting, painless
**Created:** [August 11, 2022, 2:05pm UTC](https://discuss.elastic.co/t/kibana-watcher-actions-html-body-if-list-exists-condition/311934 "2022-08-11T14:05:26Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mirokrastev](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@mirokrastev](https://discuss.elastic.co/u/mirokrastev)
#### Post date: [August 11, 2022, 2:05pm UTC](https://discuss.elastic.co/t/kibana-watcher-actions-html-body-if-list-exists-condition/311934/1 "2022-08-11T14:05:26Z")

</div>

I have the following ES response:

```auto
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : []
  },
  "aggregations" : {
    "calling" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "9991",
          "doc_count" : 4,
          "called" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "7",
                "doc_count" : 4
              }
            ]
          }
        },
        {
          "key" : "9999",
          "doc_count" : 4,
          "called" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : []
          }
        }
      ]
    }
  }
}

```

Here is my watcher action html body:

```auto
{{#ctx.payload.aggregations.calling.buckets}}

    {{#called.buckets.size}}
         <div>Number {{key}} called:</div>

         {{#called.buckets}}
              <div>{{doc_count}} times number {{key}}</div>
         {{/called.buckets}}

    {{/called.buckets.size}}

{{/ctx.payload.aggregations.calling.buckets}}

```

I want to use an if condition `called.buckets.size > 0`, basically, if inner bucket is empty, do nothing. I know that Mustache is logic-less, but is there another way to accomplish this task?

---

<div class="post-metadata">

### Author: ![richcollier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/richcollier/32/115035_2.png) [@richcollier](https://discuss.elastic.co/u/richcollier)
#### Post date: [August 12, 2022, 6:10am UTC](https://discuss.elastic.co/t/kibana-watcher-actions-html-body-if-list-exists-condition/311934/2 "2022-08-12T06:10:22Z")

</div>

You can use a `transform` block in your `actions` section to put some conditionals/logic and build a new `payload` object/data structure that will be passed on to the `email` action. For an example of using a `transform`, see:

[Alert only on 3 consecutive anomalies above a certain summed score · GitHub](https://gist.github.com/richcollier/31e4de8773d6f9183a6cf4799836b8e0) starting on line 98.

I build a new ArrayList called `records` and when passed on, it becomes the new `ctx.payload`

A different example: [Watch with simple terms agg and threshold · GitHub](https://gist.github.com/richcollier/13c8659e2102c2bbff7f3820c300c8a1)

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [September 9, 2022, 6:10am UTC](https://discuss.elastic.co/t/kibana-watcher-actions-html-body-if-list-exists-condition/311934/3 "2022-09-09T06:10:54Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
