# Watcher -\> How to return a value in a transform

**URL:** https://discuss.elastic.co/t/watcher-how-to-return-a-value-in-a-transform/314194
**Category:** Elasticsearch
**Tags:** painless
**Created:** [September 12, 2022, 12:39pm UTC](https://discuss.elastic.co/t/watcher-how-to-return-a-value-in-a-transform/314194 "2022-09-12T12:39:01Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![StephaneRodier](https://avatars.discourse-cdn.com/v4/letter/s/ed8c4c/32.png) [@StephaneRodier](https://discuss.elastic.co/u/StephaneRodier)
#### Post date: [September 12, 2022, 12:39pm UTC](https://discuss.elastic.co/t/watcher-how-to-return-a-value-in-a-transform/314194/1 "2022-09-12T12:39:01Z")

</div>

Hello, I am trying to return a mathematical result with the number of hits of two input chains which would return a percentage, but I am unable to get the value and use it in the actions email. It always returns a "0".

Any help would be appreciated.

```auto
{
  "trigger": {
    "schedule": {
      "interval": "1m"
    }
  },
  "input": {
    "chain": {
      "inputs": [
        {
          "completedCalls": {
            "search": {
              "request": {
                "search_type": "query_then_fetch",
                "indices": [
                  "index1"
                ],
                "rest_total_hits_as_int": true,
                "body": {
                 "size": 0,
                  "query": {
                    "bool": {
                      "should": [
                        {
                          "match": {
                            "cdr.terminationCause": "1"
                          }
                        },
                        {
                          "match": {
                            "cdr.terminationCause": "16"
                          }
                        },
                        {
                          "match": {
                            "cdr.terminationCause": "17"
                          }
                        },
                        {
                          "match": {
                            "cdr.terminationCause": "19"
                          }
                        },
                        {
                          "match": {
                            "cdr.terminationCause": "20"
                          }
                        }
                      ],
                      "minimum_should_match": 1,
                      "filter": {
                        "range": {
                          "@timestamp": {
                            "gte": "now-1m"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        },
        {
          "failedCalls": {
            "search": {
              "request": {
                "search_type": "query_then_fetch",
                "indices": [
                  "uc-voice-broadsoft-cdr"
                ],
                "rest_total_hits_as_int": true,
                "body": {
                 "size": 0,
                  "query": {
                    "bool": {
                      "must_not": [
                        {
                          "match": {
                            "cdr.terminationCause": "1"
                          }
                        },
                        {
                          "match": {
                            "cdr.terminationCause": "16"
                          }
                        },
                        {
                          "match": {
                            "cdr.terminationCause": "17"
                          }
                        },
                        {
                          "match": {
                            "cdr.terminationCause": "19"
                          }
                        },
                        {
                          "match": {
                            "cdr.terminationCause": "20"
                          }
                        }
                      ],
                      "filter": {
                        "range": {
                          "@timestamp": {
                            "gte": "now-1m"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      ]
    }
  },
  "transform": {
    "script": {
      "source": """
        ctx.payload.result = ((ctx.payload.failedCalls.hits.total / ctx.payload.completedCalls.hits.total) * 100);
        return ctx.payload;
      """,
      "lang": "painless"
    }
  },
  "actions": {
    "condition1_email": {
      "email": {
        "profile": "standard",
        "to": [
          "email@email.ca"
        ],
        "subject": "Watcher",
        "body": {
          "text": """Detected {{ctx.payload.result}}% failures in the last minute and requires investigation.
"""
        }
      }
    }
  }
}

```

Transform results:

```auto
    "transform": {
      "type": "script",
      "status": "success",
      "payload": {
        "result": 0,
        "failedCalls": {
          "_shards": {
            "total": 46,
            "failed": 0,
            "successful": 46,
            "skipped": 0
          },
          "hits": {
            "hits": [],
            "total": 550,
            "max_score": null
          },
          "took": 680,
          "timed_out": false
        },
        "completedCalls": {
          "_shards": {
            "total": 46,
            "failed": 0,
            "successful": 46,
            "skipped": 0
          },
          "hits": {
            "hits": [],
            "total": 10000,
            "max_score": null
          },
          "took": 883,
          "timed_out": false
        }
      }
    },

```

---

<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 10, 2022, 12:39pm UTC](https://discuss.elastic.co/t/watcher-how-to-return-a-value-in-a-transform/314194/2 "2022-10-10T12:39:27Z")

</div>

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