# How to use variable inside a switch condition?

**URL:** <https://discuss.elastic.co/t/how-to-use-variable-inside-a-switch-condition/285188>\
**Category:** Kibana\
**Tags:** canvas\
**Created:** [September 27, 2021, 3:36am UTC](https://discuss.elastic.co/t/how-to-use-variable-inside-a-switch-condition/285188 "2021-09-27T03:36:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![isaacli](https://avatars.discourse-cdn.com/v4/letter/i/bb73d2/32.png) [@isaacli](https://discuss.elastic.co/u/isaacli)\
**Post date:** [September 27, 2021, 3:36am UTC](https://discuss.elastic.co/t/how-to-use-variable-inside-a-switch-condition/285188/1 "2021-09-27T03:36:52Z")

</div>

Hello, I am trying to build a traffic light element inside Canvas.  
I am using different variables to control the expression.  
However, I found that the variables are not working inside the "Switch" condition blocks.

```auto
filters
| essql 
  query={string "SELECT wlsdomain
    FROM " {var "index_wls"} "
    WHERE " {var "severity"} "
      AND wlscluster LIKE " {var "cluster_dm"} "
      AND QUERY('logMessage:" {var "keyword_stuck"} "')
      AND \"@timestamp\" > DATE_ADD('seconds', " {var "interval"} ", NOW())"}
| math "size(wlsdomain)"
| metric 
  metricFont={font underline=false weight="bold" align="center" family="'Open Sans', Helvetica, Arial, sans-serif" italic=false size=20 color={if {all {eq 0}} then="rgba(0,0,0,0)" else="#FFF"}} metricFormat="0,0.[000]"
| render 
  containerStyle={containerStyle borderRadius="100px" border="rgba(255,255,255,0)" 
  backgroundColor={filters 
  | essql query={string "SELECT wlsdomain
    FROM " {var "index_wls"} "
    WHERE " {var "severity"} "
      AND wlscluster LIKE " {var "cluster_dm"} "
      AND QUERY('logMessage:" {var "keyword_stuck"} "')
      AND \"@timestamp\" > DATE_ADD('seconds', " {var "interval"} ", NOW())"} 
      | math "size(wlsdomain)"
      | switch case={case if={all {eq 0}} then={var "color_normal"}} 
               case={case if={all {gt 0} {lte {var "threshold_warn_st"}}} then={var "color_warn"}} 
               case={case if={all {gte {var "threshold_alert_st"}}} then={var "color_alert"}} 
               default={var "color_normal"}
    }
  }

```

 ![Variable Not Work](https://us1.discourse-cdn.com/elastic/original/3X/0/f/0fda342c6ceb490c2314b1093443f7a5a7afb86d.png)

According to the attached screenshot, the value of the element is equal to 40. It is greater than or equal to 4 ( {var "threshold\_alert\_st"} ). So based on the condition inside the Switch, it should turn to red color ( {var "color\_alert"} ). However, it still in green color.

Several foundings:

1. The variables **"threshold\_warn\_st"** and **"threshold\_alert\_st"** are not working inside the condition.
2. The other variables outside the Switch condition block are worked properly. (e.g. {var "color\_warn"} )

So may i know how to parse the variable inside the condition correctly? Thank you so much.

Regards,  
Isaac

---

<div class="post-metadata">

**Author:** ![poff](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/poff/32/81795_2.png) [@poff](https://discuss.elastic.co/u/poff)\
**Post date:** [October 1, 2021, 2:15pm UTC](https://discuss.elastic.co/t/how-to-use-variable-inside-a-switch-condition/285188/2 "2021-10-01T14:15:29Z")

</div>

Hey there!

From what I can tell, your expression looks correct. There was a bug however with number variables being inadvertently converted to string: [[Canvas] Number variable is treated as a string · Issue #90833 · elastic/kibana · GitHub](https://github.com/elastic/kibana/issues/90833)  
I think you might be experiencing something like that. This bug has been fixed in 7.14.1 and 7.15!

---

<div class="post-metadata">

**Author:** ![isaacli](https://avatars.discourse-cdn.com/v4/letter/i/bb73d2/32.png) [@isaacli](https://discuss.elastic.co/u/isaacli)\
**Post date:** [October 4, 2021, 1:10am UTC](https://discuss.elastic.co/t/how-to-use-variable-inside-a-switch-condition/285188/3 "2021-10-04T01:10:50Z")

</div>

Thank you so much!

---

<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:** [November 1, 2021, 1:11am UTC](https://discuss.elastic.co/t/how-to-use-variable-inside-a-switch-condition/285188/4 "2021-11-01T01:11:38Z")

</div>

Closed on November 1, 2021, 1:11am UTC

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