# Watcher with different thresholds

**URL:** <https://discuss.elastic.co/t/watcher-with-different-thresholds/347276>\
**Category:** Elastic Observability\
**Tags:** elastic-stack-monitoring\
**Created:** [November 16, 2023, 6:43am UTC](https://discuss.elastic.co/t/watcher-with-different-thresholds/347276 "2023-11-16T06:43:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shayn](https://avatars.discourse-cdn.com/v4/letter/s/77aa72/32.png) [@shayn](https://discuss.elastic.co/u/shayn)\
**Post date:** [November 16, 2023, 6:43am UTC](https://discuss.elastic.co/t/watcher-with-different-thresholds/347276/1 "2023-11-16T06:43:20Z")

</div>

hi  
im trying to figure up if I've the ability to alert different thresholds in same watcher task .  
i have index that's contains some different devices with numer of events  
i want to create watcher that will have different threshold per device (i need about 6-7 different threshold.. for example eqp1 will have threshold X and eqp2 will have threshold y and so on..

 ![watcher](https://us1.discourse-cdn.com/elastic/original/3X/d/5/d5c631e3b9e1e1f69e529d0cd1498c162740285b.jpeg)

current json watcher

```auto
PUT _watcher/watch/dbfae395-2733-4e46-82ba-b80d12d08ae3
{
  "trigger": {
    "schedule": {
      "interval": "1h"
    }
  },
  "input": {
    "search": {
      "request": {
        "body": {
          "size": 0,
          "query": {
            "bool": {
              "filter": {
                "range": {
                  "@timestamp": {
                    "gte": "{{ctx.trigger.scheduled_time}}||-1h",
                    "lte": "{{ctx.trigger.scheduled_time}}",
                    "format": "strict_date_optional_time||epoch_millis"
                  }
                }
              }
            }
          },
          "aggs": {
            "bucketAgg": {
              "terms": {
                "field": "model.keyword",
                "size": "10",
                "order": {
                  "metricAgg": "desc"
                }
              },
              "aggs": {
                "metricAgg": {
                  "max": {
                    "field": "num_of_events"
                  }
                }
              }
            }
          }
        },
        "indices": [
          "acs_device_event*"
        ]
      }
    }
  },
  "condition": {
    "script": {
      "source": "ArrayList arr = ctx.payload.aggregations.bucketAgg.buckets; for (int i = 0; i < arr.length; i++) { if (arr[i]['metricAgg'].value > params.threshold) { return true; } } return false;",
      "params": {
        "threshold": 20000
      }
    }
  },
  "transform": {
    "script": {
      "source": "HashMap result = new HashMap(); ArrayList arr = ctx.payload.aggregations.bucketAgg.buckets; ArrayList filteredHits = new ArrayList(); for (int i = 0; i < arr.length; i++) { HashMap filteredHit = new HashMap(); filteredHit.key = arr[i].key; filteredHit.value = arr[i]['metricAgg'].value; if (filteredHit.value > params.threshold) { filteredHits.add(filteredHit); } } result.results = filteredHits; return result;",
      "params": {
        "threshold": 20000
      }
    }
  },
  "actions": {
    "webhook_1": {
      "webhook": {
        "host": "XXXXXX",
        "port": XXXXXX,
        "method": "post",
        "scheme": "http",
        "body": "{\n \"message\": \"Watch [{{ctx.metadata.name}}] to many events on acs has exceeded the threshold of 20K\"\n}"
      }
    }
  }
}

```

is it possible ?  
thank you

---

<div class="post-metadata">

**Author:** ![yago82](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yago82/32/97755_2.png) [@yago82](https://discuss.elastic.co/u/yago82)\
**Post date:** [December 9, 2023, 3:56pm UTC](https://discuss.elastic.co/t/watcher-with-different-thresholds/347276/2 "2023-12-09T15:56:42Z")

</div>

Hi,

In your script, you can add a condition for each device and set a different threshold for each one. Here's an example of how you can modify your script to add different thresholds for different devices:

```auto
"source": "ArrayList arr = ctx.payload.aggregations.bucketAgg.buckets; for (int i = 0; i < arr.length; i++) { if (arr[i].key == 'eqp1' && arr[i]['metricAgg'].value > params.threshold_eqp1) { return true; } else if (arr[i].key == 'eqp2' && arr[i]['metricAgg'].value > params.threshold_eqp2) { return true; } } return false;",
"params": {
  "threshold_eqp1": 20000,
  "threshold_eqp2": 30000
}

```

In this example, 'eqp1' has a threshold of 20,000 and 'eqp2' has a threshold of 30,000. You can add more conditions for more devices as needed.

Remember to also modify your transform script to include these different thresholds.

Please note that this is a simple example and might need to be adjusted based on your exact requirements and data structure.

---

<div class="post-metadata">

**Author:** ![shayn](https://avatars.discourse-cdn.com/v4/letter/s/77aa72/32.png) [@shayn](https://discuss.elastic.co/u/shayn)\
**Post date:** [December 11, 2023, 6:15am UTC](https://discuss.elastic.co/t/watcher-with-different-thresholds/347276/3 "2023-12-11T06:15:49Z")

</div>

I will try that ..  
Thank you very mush
