# Watcher - Creating a Payload

**URL:** <https://discuss.elastic.co/t/watcher-creating-a-payload/364909>\
**Category:** Kibana\
**Tags:** elastic-stack-alerting, painless\
**Created:** [August 14, 2024, 1:56pm UTC](https://discuss.elastic.co/t/watcher-creating-a-payload/364909 "2024-08-14T13:56:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![erikg](https://avatars.discourse-cdn.com/v4/letter/e/91b2a8/32.png) [@erikg](https://discuss.elastic.co/u/erikg)\
**Post date:** [August 14, 2024, 1:56pm UTC](https://discuss.elastic.co/t/watcher-creating-a-payload/364909/1 "2024-08-14T13:56:45Z")

</div>

Hello,  
Is there a way to create a new payload in the condition portion of watcher?

I have this watcher that evaluates buckets, it checks to see if the buckets pass a condition. The issue I want to filter out the buckets in the conditon and currently its passing all the buckets as the condition evaluates true which is not what I want.

```auto
  "condition": {
    "script": {
      "source": """
      def result = false;
      def filteredBuckets = [];
      for (bucket in ctx.payload.aggregations.by_host_and_object.buckets) {
         def mostRecentEvent = bucket.most_recent_event.hits.hits[0]._source.event.outcome;
         def failuresCount = bucket.failures.doc_count;
         if (mostRecentEvent == 'failure' && failuresCount > 1) {
              filteredBuckets.add(bucket);
              return true;
         } 
      }, 
ctx.payload.filtered_buckets = filteredBuckets;
return false;
      """,
      "lang": "painless"
    }
  }

```

As you can see I attempted to assign the filteredBuckets to ctx.payload.filtered\_buckets.

I might be approaching this wrong
