# Need to add time range filter condition in below watcher

**URL:** <https://discuss.elastic.co/t/need-to-add-time-range-filter-condition-in-below-watcher/304719>\
**Category:** Kibana\
**Tags:** elastic-stack-alerting, painless\
**Created:** [May 14, 2022, 5:36am UTC](https://discuss.elastic.co/t/need-to-add-time-range-filter-condition-in-below-watcher/304719 "2022-05-14T05:36:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mangeshmj1992](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mangeshmj1992](https://discuss.elastic.co/u/mangeshmj1992)\
**Post date:** [May 14, 2022, 5:36am UTC](https://discuss.elastic.co/t/need-to-add-time-range-filter-condition-in-below-watcher/304719/1 "2022-05-14T05:36:24Z")

</div>

Hello team,  
Need to add time range filter condition in below watcher. i am unable to identify where i need to exactly put below condions:

I think my script is checking whole data if i not mentioned any range filter. I want to check data from last 1 hour only on below watcher script.

**Conditions:**

```auto
{
                  "range": {
                    "@timestamp": {
                      "gte": "now-1h",
                      "lte": "now",
                      "format": "strict_date_optional_time"
                    }
                  }
                }

```

**Watcher Script:**

```auto
{
  "trigger": {
    "schedule": {
      "interval": "5m"
    }
  },
  "input": {
    "search": {
      "request": {
        "search_type": "query_then_fetch",
        "indices": [
          "index-"
        ],
        "rest_total_hits_as_int": true,
        "body": {
          "size": 100,
          "_source": [
            "Error",
            "@appTimestamp"
          ],
          "query": {
            "bool": {
              "must": [],
              "filter": [
                {
                  // all my filter conditions here
              ],
              "should": [],
              "must_not": []
            }
          },
          "aggs": {
            "Error": {
              "terms": {
                "field": "Error.keyword"
              },
              "aggs": {
                "range": {
                  "date_range": {
                    "field": "@timestamp",
                    "format": "H",
                    "ranges": [
                      {
                        "key": "Error_Count_10mins_ago",
                        "from": "now-12m",
                        "to": "now-7m"
                      },
                      {
                        "key": "Error_Count_5mins_ago",
                        "from": "now-6m",
                        "to": "now-1m"
                      }
                    ],
                    "keyed": true
                  }
                },
                "deviation": {
                  "bucket_script": {
                    "buckets_path": {
                      "FiveMinAgo": "range['Error_Count_5mins_ago']>_count",
                      "TenMinAgo": "range['Error_Count_10mins_ago']>_count"
                    },
                    "script": "if (params.TenMinAgo == 0) {0} else {(params.FiveMinAgo - params.TenMinAgo)*100/params.TenMinAgo}"
                  }
                },
                "filter_aggs": {
                  "bucket_selector": {
                    "buckets_path": {
                      "FiveMinAgo": "range['Error_Count_5mins_ago']>_count",
                      "TenMinAgo": "range['Error_Count_10mins_ago']>_count",
                      "deviation": "deviation.value"
                    },
                    "script": "(params.TenMinAgo >= 80 && params.FiveMinAgo >= 130 && params.deviation >= 40)"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "condition": {
    "script": {
      "source": """
        ctx.payload.aggregations.Error.buckets.size() > 0;
      """,
      "lang": "painless"
    }
  },
 

 "actions": {
    // all actions
    }

```

---

<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:** [June 11, 2022, 5:37am UTC](https://discuss.elastic.co/t/need-to-add-time-range-filter-condition-in-below-watcher/304719/2 "2022-06-11T05:37:13Z")

</div>

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