# How to use Math operations in actions with ctx.payload.\*

**URL:** <https://discuss.elastic.co/t/how-to-use-math-operations-in-actions-with-ctx-payload/153673>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [October 23, 2018, 7:42pm UTC](https://discuss.elastic.co/t/how-to-use-math-operations-in-actions-with-ctx-payload/153673 "2018-10-23T19:42:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![shiv94](https://avatars.discourse-cdn.com/v4/letter/s/bc8723/32.png) [@shiv94](https://discuss.elastic.co/u/shiv94)\
**Post date:** [October 23, 2018, 7:42pm UTC](https://discuss.elastic.co/t/how-to-use-math-operations-in-actions-with-ctx-payload/153673/1 "2018-10-23T19:42:42Z")

</div>

Hey, how do I use math operations like multiplication (_) in actions with ctx.payload._  
for example, {{#..}}{{\_source.system.filesystem.used.pct}}{{/..}} gives value 0.7 but I want 0.7\*100 = 70  
and also how do I convert bytes to gb or mb. {{#..}}{{\_source.system.memory.total}}{{/..}} value is 38114820096 bytes. How do I get required format?

---

<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:** [October 24, 2018, 7:35am UTC](https://discuss.elastic.co/t/how-to-use-math-operations-in-actions-with-ctx-payload/153673/2 "2018-10-24T07:35:42Z")

</div>

You cannot do those calculation within mustache. You have to `transform` the data using a script first and then access this data in your mustache template.

See [https://www.elastic.co/guide/en/elastic-stack-overview/6.4/transform-script.html](https://www.elastic.co/guide/en/elastic-stack-overview/6.4/transform-script.html)

---

<div class="post-metadata">

**Author:** ![shiv94](https://avatars.discourse-cdn.com/v4/letter/s/bc8723/32.png) [@shiv94](https://discuss.elastic.co/u/shiv94)\
**Post date:** [October 24, 2018, 1:21pm UTC](https://discuss.elastic.co/t/how-to-use-math-operations-in-actions-with-ctx-payload/153673/3 "2018-10-24T13:21:23Z")

</div>

"transform": {  
"script": {  
"source" : "doc['\_source.system.filesystem.used.pct'] \* multiplier",  
"params": { "multiplier": 100 }  
}  
}  
How do I use this array field  
{{ctx.payload.aggregations.host.buckets}}{{top\_hit.hits.hits}}{{\_source.system.filesystem.used.pct}}?

---

<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:** [October 24, 2018, 1:52pm UTC](https://discuss.elastic.co/t/how-to-use-math-operations-in-actions-with-ctx-payload/153673/4 "2018-10-24T13:52:04Z")

</div>

see [https://www.elastic.co/guide/en/elastic-stack-overview/6.4/transform.html](https://www.elastic.co/guide/en/elastic-stack-overview/6.4/transform.html)

be aware that a transform **replaces** the existing payload, so if you want to have access to the existing fields, you have to return those plus the newly added ones. See [https://github.com/elastic/examples/tree/master/Alerting](https://github.com/elastic/examples/tree/master/Alerting) for a couple of examples

---

<div class="post-metadata">

**Author:** ![shiv94](https://avatars.discourse-cdn.com/v4/letter/s/bc8723/32.png) [@shiv94](https://discuss.elastic.co/u/shiv94)\
**Post date:** [October 24, 2018, 2:37pm UTC](https://discuss.elastic.co/t/how-to-use-math-operations-in-actions-with-ctx-payload/153673/5 "2018-10-24T14:37:15Z")

</div>

This information helpful to me. How do I use array field like this {{#ctx.payload.aggregations.host.buckets}}{{#top\_hit.hits.hits}}{{\_source.system.filesystem.used.pct}} in source field.

If I use "source" : "doc['\_source.system.filesystem.used.pct'] \* multiplier", it throws a runtime error. I cannot access \_source.system.filesystem.used.pct field directly because its object in a array

---

<div class="post-metadata">

**Author:** ![shiv94](https://avatars.discourse-cdn.com/v4/letter/s/bc8723/32.png) [@shiv94](https://discuss.elastic.co/u/shiv94)\
**Post date:** [October 24, 2018, 4:41pm UTC](https://discuss.elastic.co/t/how-to-use-math-operations-in-actions-with-ctx-payload/153673/6 "2018-10-24T16:41:30Z")

</div>

"transform": {  
"script": {  
"source": "doc['\_source.system.filesystem.used.pct'] \* params.multiplier",  
"lang": "painless",  
"params": {  
"multiplier": 100  
}  
}  
}  
I am getting runtime error

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

"payload": {  
"\_shards": {  
"total": 130,  
"failed": 0,  
"successful": 130,  
"skipped": 125  
},  
"hits": {  
"hits": ,  
"total": 35,  
"max\_score": 0  
},  
"took": 32,  
"timed\_out": false,  
"aggregations": {  
"host": {  
"doc\_count\_error\_upper\_bound": 0,  
"sum\_other\_doc\_count": 0,  
"buckets": [  
{  
"doc\_count": 5,  
"top\_hit": {  
"hits": {  
"hits": [  
{  
"\_index": "metricbeat-6.4.1-2018.10.24",  
"\_type": "doc",  
"\_source": {  
"@timestamp": "2018-10-24T18:14:02.128Z",  
"system": {  
"filesystem": {  
"device\_name": "xxx",  
"total": xxx,  
"mount\_point": "/",  
"free\_files": xxx,  
"available": xxx,  
"files": xxx,  
"used": {  
"pct": 0.6516,  
"bytes": 12685934592  
.....  
.....  
}

---

<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:** [October 25, 2018, 6:40am UTC](https://discuss.elastic.co/t/how-to-use-math-operations-in-actions-with-ctx-payload/153673/7 "2018-10-25T06:40:24Z")

</div>

the `doc` notation is not applicate for watcher, it is only used when you want to access doc values in a query. Also, you need to access an array of data, namely `ctx.payload.hits.hits`, which contains the hits of a search response, and each hit has a `_source`, so you need to access this array via a for loop. Please have a look at the examples I posted before.

---

<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 22, 2018, 1:46pm UTC](https://discuss.elastic.co/t/how-to-use-math-operations-in-actions-with-ctx-payload/153673/9 "2018-11-22T13:46:08Z")

</div>

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