# Setting a custom variable in TSVB bucket script

**URL:** <https://discuss.elastic.co/t/setting-a-custom-variable-in-tsvb-bucket-script/294960>\
**Category:** Kibana\
**Created:** [January 20, 2022, 2:34pm UTC](https://discuss.elastic.co/t/setting-a-custom-variable-in-tsvb-bucket-script/294960 "2022-01-20T14:34:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew22](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Andrew22](https://discuss.elastic.co/u/Andrew22)\
**Post date:** [January 20, 2022, 2:34pm UTC](https://discuss.elastic.co/t/setting-a-custom-variable-in-tsvb-bucket-script/294960/1 "2022-01-20T14:34:35Z")

</div>

Hello,

Is it possible to set a variable inside a bucket script in tsvb? i would like to compare two values and set the new variable to be the value that exists.

right now I have:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/f/d/fd3e794dd4b2270ac68c88f300137f5d9aa7f161.png)

my code

```auto
if (params.inmin != null) {
   params.in= params.inmin;
} else if (params.inhour != null) {
   params.in=params.inhour;
}

if (params.outmin != null) {
    params.out= params.outmin;
} else if (params.outhour != null) {
   params.out=params.outhour;
} 

if (params.in > 0.50) {
    params.solved=1;
} else if (params.out > 0.50) {
  params.solved=1;
} else { 
   if (params.in > params.out) {
     params.solved=params.in;
   } else if (params.out > params.in) {
     params.solved=params.out;
    }
}

return params.solved;

```

which results in:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/c/0/c0c5c255d68800b9826d7b2132b0ac812827c454.png)  
but i would expect to see the line go all the way across the graph.

any help with this would be great!

Thank you

---

<div class="post-metadata">

**Author:** ![Andrew22](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Andrew22](https://discuss.elastic.co/u/Andrew22)\
**Post date:** [January 20, 2022, 4:57pm UTC](https://discuss.elastic.co/t/setting-a-custom-variable-in-tsvb-bucket-script/294960/2 "2022-01-20T16:57:25Z")

</div>

Seems the issue might start from when i check to see if the fields exist with :

```auto
if (params.inmin != null) {
   params.in= params.inmin;
} else if (params.inhour != null) {
   params.in=params.inhour;
}

if (params.outmin != null) {
    params.out= params.outmin;
} else if (params.outhour != null) {
   params.out=params.outhour;
} 

```

is there a better way to check if the field exists?

---

<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:** [February 17, 2022, 4:58pm UTC](https://discuss.elastic.co/t/setting-a-custom-variable-in-tsvb-bucket-script/294960/3 "2022-02-17T16:58:17Z")

</div>

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