# Adding conditions on action in watcher

**URL:** <https://discuss.elastic.co/t/adding-conditions-on-action-in-watcher/134155>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [June 1, 2018, 7:11am UTC](https://discuss.elastic.co/t/adding-conditions-on-action-in-watcher/134155 "2018-06-01T07:11:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ishantanu](https://avatars.discourse-cdn.com/v4/letter/i/da6949/32.png) [@ishantanu](https://discuss.elastic.co/u/ishantanu)\
**Post date:** [June 1, 2018, 7:11am UTC](https://discuss.elastic.co/t/adding-conditions-on-action-in-watcher/134155/1 "2018-06-01T07:11:41Z")

</div>

Hi,

While I was going through docs for adding conditions on actions in Watcher, I had a few doubts.

Is there any way I can access the `payload._value` of transform block inside the condition on watcher?

Ex.

```auto
"transform": {
      "type": "script",
      "status": "success",
      "payload": {
        "_value": 0.44380686824204707
      }
    },

```

Example condition:

```auto
"condition": { 
        "compare" : { "ctx.payload._value" : { "gt" : ctx.metadata.threshold } }
}

```

In other words, alert only when the above condition succeeds.

In case I am trying to add a condition to action, it is throwing an error saying `invalid [compare]`.

The action I am performing is `log`.

---

<div class="post-metadata">

**Author:** ![elastock](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elastock/32/35672_2.png) [@elastock](https://discuss.elastic.co/u/elastock)\
**Post date:** [June 1, 2018, 9:24am UTC](https://discuss.elastic.co/t/adding-conditions-on-action-in-watcher/134155/2 "2018-06-01T09:24:01Z")

</div>

You can add data into the context like this `ctx.vars._value='0.44380686824204707';`

```
  "transform": {
  "script" : "ctx.vars._value=0.44380686824204707"
},
```

---

<div class="post-metadata">

**Author:** ![ishantanu](https://avatars.discourse-cdn.com/v4/letter/i/da6949/32.png) [@ishantanu](https://discuss.elastic.co/u/ishantanu)\
**Post date:** [June 1, 2018, 9:50am UTC](https://discuss.elastic.co/t/adding-conditions-on-action-in-watcher/134155/3 "2018-06-01T09:50:00Z")

</div>

@elastock This is not working. It gives:

```auto
Watcher: [parse_exception] could not parse action [inlined/transform]. unknown action type [type]

 OK

```

This is my action:

```auto
"actions": {
    "log": {
      "transform": {
        "type": "script",
        "status": "success",
        "script" : "ctx.vars._value=0.44380686824204707"
       },    
      "logging": {
        "level": "info",
        "text": {
          "source": "Percentage change for upstream from previous day: {{ctx.payload._value}}",
          "lang": "mustache"
        }
      }
    }
  },

```

Let me know if something is wrong. Also, the value `0.44380686824204707` is not static, it will change after transformation.

---

<div class="post-metadata">

**Author:** ![ishantanu](https://avatars.discourse-cdn.com/v4/letter/i/da6949/32.png) [@ishantanu](https://discuss.elastic.co/u/ishantanu)\
**Post date:** [June 1, 2018, 10:13am UTC](https://discuss.elastic.co/t/adding-conditions-on-action-in-watcher/134155/4 "2018-06-01T10:13:30Z")

</div>

Okay. I think I got this working. I didn't need to use transform, I just needed `condition`.

---

<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:** [June 5, 2018, 8:19am UTC](https://discuss.elastic.co/t/adding-conditions-on-action-in-watcher/134155/5 "2018-06-05T08:19:15Z")

</div>

just to clear things up: the transform is always executed after the condition - and only if the condition is true.

---

<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 3, 2018, 8:19am UTC](https://discuss.elastic.co/t/adding-conditions-on-action-in-watcher/134155/6 "2018-07-03T08:19:23Z")

</div>

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