# Math Calculation in Elastic Watcher not working

**URL:** <https://discuss.elastic.co/t/math-calculation-in-elastic-watcher-not-working/359178>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting, painless\
**Created:** [May 9, 2024, 12:24pm UTC](https://discuss.elastic.co/t/math-calculation-in-elastic-watcher-not-working/359178 "2024-05-09T12:24:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Santosh\_G](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/santosh_g/32/124283_2.png) [@Santosh\_G](https://discuss.elastic.co/u/Santosh_G)\
**Post date:** [May 9, 2024, 12:24pm UTC](https://discuss.elastic.co/t/math-calculation-in-elastic-watcher-not-working/359178/1 "2024-05-09T12:24:48Z")

</div>

I had the below response I am receiving from Elastic dev tools and I need to calculate the % difference increase/decrease between the 2 aggregation count.

> {  
> "alternative\_input": {  
> "hits": {  
> "total": {  
> "value": 33909841,  
> "relation": "eq"  
> },  
> "max\_score": null,  
> "hits":   
> },  
> "aggregations": {  
> "second\_interval\_events": {  
> "meta": {},  
> "doc\_count": 1300  
> },  
> "first\_interval\_events": {  
> "meta": {},  
> "doc\_count": 1400  
> }  
> }  
> },  
> "ignore\_condition": true,  
> "action\_modes": {  
> "email": "force\_execute"  
> },  
> "record\_execution": true  
> }

Requirement : in condition need to check if the % diff is more than 35 , then need to trigger alert and in the Mail we need to show how much % difference

Condition Code:

> "condition": {  
> "script": {  
> "source": "return (((ctx.payload.aggregations.first\_interval\_events.doc\_count - ctx.payload.aggregations.second\_interval\_events.doc\_count)\*100/ctx.payload.aggregations.first\_interval\_events.doc\_count) \> Integer.parseInt(params.threshold));",  
> "params":{  
> "threshold" : "0.35"  
> },  
> "lang": "painless"  
> }  
> },

Above condition not working and for verification i made threshold 0 and able to see email, but not sure how to calcualte Percentage difference to show it Email subject.

Used transform

> "actions": {  
> "email": {  
> "transform": {  
> "script": {  
> "source": " return ['percentageDiff': (ctx.payload.aggregations.first\_interval\_events - ctx.payload.aggregations.second\_interval\_events / ctx.payload.aggregations.first\_interval\_events)\*100]",  
> "lang": "painless"  
> }  
> },

If i am trying to refer {{ctx.payload.percentageDiff}} in email , its value always coming as 0.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [May 9, 2024, 1:52pm UTC](https://discuss.elastic.co/t/math-calculation-in-elastic-watcher-not-working/359178/2 "2024-05-09T13:52:20Z")

</div>

Did you try 100.0 otherwise I think the whole calculation gets converted to integer.  
Also, you should probably set that as a parameter

---

<div class="post-metadata">

**Author:** ![Santosh\_G](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/santosh_g/32/124283_2.png) [@Santosh\_G](https://discuss.elastic.co/u/Santosh_G)\
**Post date:** [May 9, 2024, 3:01pm UTC](https://discuss.elastic.co/t/math-calculation-in-elastic-watcher-not-working/359178/3 "2024-05-09T15:01:10Z")

</div>

I had tried the below snippet for transform

```auto
"transform": {
    "script": {
      "source": "return [
'first': ctx.payload.aggregations.first_interval_events, 'second':ctx.payload.aggregations.second_interval_events , 
'percentageDiff':
(ctx.payload.aggregations.first_interval_events.doc_count - ctx.payload.aggregations.second_interval_events.doc_count / (ctx.payload.hits.total/2))*100.0 ]",
      "lang": "painless"
    }
  },

```

Error:

```auto
[320](;#L320) "return ['first': ctx.payload.aggregations.first_interval_events, 'second':ctx.payload.aggregations.second_interval_events , 'percentageDiff':(ctx.payload.aggregations.first_interval_events.doc_count - ctx.payload.aggregations.second_interval_events.doc_count / (ctx.payload.hits.total/2))*100.0]",

[321](;#L321) " ^---- HERE"

[322](;#L322) ],

[323](;#L323) "script": "return ['first': ctx.payload.aggregations.first_interval_events, 'second':ctx.payload.aggregations.second_interval_events , 'percentageDiff':(ctx.payload.aggregations.first_interval_events - ctx.payload.aggregations.second_interval_events / ctx.payload.agg ...",

[324](;#L324) "lang": "painless",

[325](;#L325) "position": {

[326](;) "offset": 265,

[327](;#L327) "start": 0,

[328](;#L328) "end": 296

[329](;#L329) },

[330](;#L330) "caused_by": {

[331](;#L331) "type": "class_cast_exception",

[332]( "reason": "Cannot apply [/] operation to types [java.util.HashMap] and [java.lang.Integer]"
}
        }
      },
"actions": []
    },
    "messages": [
      "failed to execute watch transform"
    ]

```

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [May 9, 2024, 4:57pm UTC](https://discuss.elastic.co/t/math-calculation-in-elastic-watcher-not-working/359178/4 "2024-05-09T16:57:02Z")

</div>

The error states you are

> [@Santosh\_G](#):
>
> `[332]( "reason": "Cannot apply [/] operation to types [java.util.HashMap] and [java.lang.Integer]"`

Looks like this is invalid

`(ctx.payload.hits.total/2))`

Perhaps look here for some example

> **[examples/Alerting/Sample Watches at master · elastic/examples](https://github.com/elastic/examples/tree/master/Alerting/Sample%20Watches)**
>
> Home for Elasticsearch examples available to everyone. It's a great way to get started. - elastic/examples
