# Do watcher watch scripts have the ability to add values to the result payload

**URL:** <https://discuss.elastic.co/t/do-watcher-watch-scripts-have-the-ability-to-add-values-to-the-result-payload/44349>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [March 14, 2016, 6:04pm UTC](https://discuss.elastic.co/t/do-watcher-watch-scripts-have-the-ability-to-add-values-to-the-result-payload/44349 "2016-03-14T18:04:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![beckerdo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/beckerdo/32/8355_2.png) [@beckerdo](https://discuss.elastic.co/u/beckerdo)\
**Post date:** [March 14, 2016, 6:04pm UTC](https://discuss.elastic.co/t/do-watcher-watch-scripts-have-the-ability-to-add-values-to-the-result-payload/44349/1 "2016-03-14T18:04:21Z")

</div>

I am using a few file scripts to influence my watch output. I see from the documentation that you can use mustache templates to get result and payload values into your conditions and transforms. However, can a condition file script add information to the result or payload of a watch.

What I would like to say in my condition file script is something like:

> ```
> {{ctx.foo}}="bar"
> {{ctx.baz}}="booze"
> return true
> 
> ```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 15, 2016, 9:12am UTC](https://discuss.elastic.co/t/do-watcher-watch-scripts-have-the-ability-to-add-values-to-the-result-payload/44349/2 "2016-03-15T09:12:32Z")

</div>

Hey Dan,

you're right, that this is a task for [transforms](https://www.elastic.co/guide/en/watcher/current/transform.html). Transforms though (on watch level, not on action level obviously) gets executed after the condition check.

```json
PUT /_watcher/watch/cluster_health_watch
{
  "trigger": {
    "schedule": {
      "interval": "600s"
    }
  },
  "input": {
    "http": {
      "request": {
        "host": "localhost",
        "port": 9200,
        "path": "/_cluster/health"
      }
    }
  },
  "condition": {
    "script": "ctx.payload.my = new Date() ; return true"
  },
  "actions": {
    "log_error": {
      "logging": {
        "text": "Found {{ctx.payload}}"
      }
    }
  }
}

```

resulted in this output calling /\_execute

```json
...
    "actions": [
        {
          "id": "log_error",
          "type": "logging",
          "status": "success",
          "logging": {
            "logged_text": "Found {...all the health data... , my=Tue Mar 15 10:08:30 CET 2016}"
          }
        }
      ]
...

```

I'd still prefer using transforms to have scripting being only a reading component, but that might just be me.

--Alex

---

<div class="post-metadata">

**Author:** ![beckerdo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/beckerdo/32/8355_2.png) [@beckerdo](https://discuss.elastic.co/u/beckerdo)\
**Post date:** [March 15, 2016, 2:02pm UTC](https://discuss.elastic.co/t/do-watcher-watch-scripts-have-the-ability-to-add-values-to-the-result-payload/44349/3 "2016-03-15T14:02:49Z")

</div>

Thanks, very cool!

I am a Groovy newbie. Is there documentation on what is supported in a Watcher script file? Is it a plain Groovy script file?

I can see from my explorations that the following script file won't compile. Any way to import standard Java packages:

> ```
> import java.text.DateFormat
> def plainFormatter = DateFormat.instance
> println plainFormatter.format(new Date())
> 
> ```

Thanks, beckedo

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 17, 2016, 12:18pm UTC](https://discuss.elastic.co/t/do-watcher-watch-scripts-have-the-ability-to-add-values-to-the-result-payload/44349/4 "2016-03-17T12:18:07Z")

</div>

Hey,

the docs about [Scripting and Security](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-security.html) should help you here.

--Alex

---

<div class="post-metadata">

**Author:** ![beckerdo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/beckerdo/32/8355_2.png) [@beckerdo](https://discuss.elastic.co/u/beckerdo)\
**Post date:** [March 17, 2016, 1:44pm UTC](https://discuss.elastic.co/t/do-watcher-watch-scripts-have-the-ability-to-add-values-to-the-result-payload/44349/5 "2016-03-17T13:44:35Z")

</div>

I did not know about the ClassPermission sandbox. However the article helped me discover the joda-time DateTime, DateTimeUtils, DateTimeZone, and Instant availability which is very helpful.

Thanks, beckerdo

---

<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:** [July 6, 2017, 1:46pm UTC](https://discuss.elastic.co/t/do-watcher-watch-scripts-have-the-ability-to-add-values-to-the-result-payload/44349/6 "2017-07-06T13:46:19Z")

</div>


