# Script condition and ctx.vars

**URL:** <https://discuss.elastic.co/t/script-condition-and-ctx-vars/37701>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [December 21, 2015, 2:53pm UTC](https://discuss.elastic.co/t/script-condition-and-ctx-vars/37701 "2015-12-21T14:53:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcku](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mcku/32/33775_2.png) [@mcku](https://discuss.elastic.co/u/mcku)\
**Post date:** [December 21, 2015, 2:53pm UTC](https://discuss.elastic.co/t/script-condition-and-ctx-vars/37701/1 "2015-12-21T14:53:08Z")

</div>

Hello All,

ES 2.1.1. I am keeping some state info in ctx.vars, set in a transform.

Then check for some conditions in ctx.vars,  
apparently can't be accessed within `condition` `script`,  
but can be accessed within actions and subsequent inputs.

```
transform": {
 "chain":[ 
  {       
    "script": "ctx.vars.userid = ctx.payload.hits.hits[0]._source.user.id; ctx.vars.lang = ctx.payload.hits.hits[0]._source.lang;"          
  },

```

the following returns true (note the hard-coded 'tr') :

```
"condition": {
  "script" : "return ctx.payload.hits.hits[0]._source.lang.equals('tr') ; " 
},

```

but this is not.

```
 "condition": {
  "script" : "return ctx.vars.lang.equals('tr') ; " 
},

```

However, in `actions`, i can access ctx.vars without any issue.  
I think somehow `condition` cannot read ctx.var.  
Could anyone confirm?

Thx

---

<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:** [December 22, 2015, 8:10am UTC](https://discuss.elastic.co/t/script-condition-and-ctx-vars/37701/2 "2015-12-22T08:10:28Z")

</div>

Hey,

a transform is only supposed to modify the payload. You should have access to that in the actions as well and/or use a chained input?

Wondering about the use-case here, interested in the example to see what does not work that way.

--Alex

---

<div class="post-metadata">

**Author:** ![mcku](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mcku/32/33775_2.png) [@mcku](https://discuss.elastic.co/u/mcku)\
**Post date:** [December 24, 2015, 10:44am UTC](https://discuss.elastic.co/t/script-condition-and-ctx-vars/37701/3 "2015-12-24T10:44:08Z")

</div>

Actually, I was inspired by this post:

> [@Adding data in the ctx](https://discuss.elastic.co/t/adding-data-in-the-ctx/24680/2):
>
> Any script in a watch can add variables to the context. For example something like this can be added to a script condition: ctx.vars.my\_var=5; return ctx.vars.my\_var \<= 5; Once a context variable has been defined it is accessible in all places of a Watch that supports scripting. So in the above example the ctx.vars.my\_var can also be checked in a script transform elsewhere in the Watch. Context variables are scoped to the watch execution to are defined in. Context variables aren't stored in th…

As a `transform` and `condition`supports scripting, they should have access to `ctx.vars`. Is this falsified already or still valid, wondering, especially the vendor perspective would be important here as this is a commercial component.

The use case is very significant I think, such as increasing dimensionality of watch evaluations by causal relationships. In that sense, chain inputs and chain transforms together would provide with huge flexibility and would expand the usability of watcher, which is a huge step. Kudos for that.  
(But at my small step here now, I am still kinda stuck!)

Thanks anyway...

---

<div class="post-metadata">

**Author:** ![mcku](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mcku/32/33775_2.png) [@mcku](https://discuss.elastic.co/u/mcku)\
**Post date:** [December 24, 2015, 12:54pm UTC](https://discuss.elastic.co/t/script-condition-and-ctx-vars/37701/4 "2015-12-24T12:54:28Z")

</div>

I am still doing some more tests on that but my eval license has expired :shame: .

---

<div class="post-metadata">

**Author:** ![mcku](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mcku/32/33775_2.png) [@mcku](https://discuss.elastic.co/u/mcku)\
**Post date:** [December 26, 2015, 6:58am UTC](https://discuss.elastic.co/t/script-condition-and-ctx-vars/37701/5 "2015-12-26T06:58:40Z")

</div>

After some workarounds, I realize a powerful event processor is ready. Thanks for your support.

---

<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:** [December 28, 2015, 5:23pm UTC](https://discuss.elastic.co/t/script-condition-and-ctx-vars/37701/6 "2015-12-28T17:23:45Z")

</div>

Hey,

happy to hear, you got it working. If you have an interesting use-case to share, we are all ears 🙂

--Alex

---

<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:47pm UTC](https://discuss.elastic.co/t/script-condition-and-ctx-vars/37701/7 "2017-07-06T13:47:37Z")

</div>


