# Use painless variable across watch segments

**URL:** <https://discuss.elastic.co/t/use-painless-variable-across-watch-segments/247715>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting, painless\
**Created:** [September 7, 2020, 6:41am UTC](https://discuss.elastic.co/t/use-painless-variable-across-watch-segments/247715 "2020-09-07T06:41:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lstoneir](https://avatars.discourse-cdn.com/v4/letter/l/b9bd4f/32.png) [@lstoneir](https://discuss.elastic.co/u/lstoneir)\
**Post date:** [September 7, 2020, 6:41am UTC](https://discuss.elastic.co/t/use-painless-variable-across-watch-segments/247715/1 "2020-09-07T06:41:29Z")

</div>

i want to set **name\_of\_server** in condition area and use it inside **logging** action.  
is this possible?

```auto
"actions": {
    "my-logging-action": {
        "condition":{
            "script": {
                "source": "
                name_of_server='';
                for(int i=0; i < ctx.payload.aggregations.range_by_date.buckets[0].blades.buckets.length; i++){
                      if(ctx.payload.aggregations.range_by_date.buckets[0].blades.buckets[i][ctx.metadata.element].value > params.threshold){
                          name_of_server = ctx.payload.aggregations.range_by_date.buckets[0].blades.buckets[i][blade_name];
                          return true;
                      }
                  }",
                "lang":"painless",
                "params": { "threshold": 5 }
            }
        },
      "logging": {
        "level": "info",
        "text": "{name_of_server} alert"
      }
    }
  }

```

---

<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:** [September 16, 2020, 9:57am UTC](https://discuss.elastic.co/t/use-painless-variable-across-watch-segments/247715/3 "2020-09-16T09:57:55Z")

</div>

if you set name of server in the context, it will be available in the logging action, like

```auto
ctx.payload.name_of_server = 'foo-bar

```

then you can access that one via `ctx.payload.name_of_server` in your logging action.

---

<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:** [October 14, 2020, 9:58am UTC](https://discuss.elastic.co/t/use-painless-variable-across-watch-segments/247715/4 "2020-10-14T09:58:22Z")

</div>

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