# How do you extract a Watcher's 'state', 'status' or 'execution\_state' from ctx

**URL:** <https://discuss.elastic.co/t/how-do-you-extract-a-watchers-state-status-or-execution-state-from-ctx/309952>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [July 18, 2022, 8:40pm UTC](https://discuss.elastic.co/t/how-do-you-extract-a-watchers-state-status-or-execution-state-from-ctx/309952 "2022-07-18T20:40:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ameindel](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@ameindel](https://discuss.elastic.co/u/ameindel)\
**Post date:** [July 18, 2022, 8:40pm UTC](https://discuss.elastic.co/t/how-do-you-extract-a-watchers-state-status-or-execution-state-from-ctx/309952/1 "2022-07-18T20:40:54Z")

</div>

Hello there!

I'm currently trying to create a Watcher that has a 'condition' that will resolve to true when the Watcher execution fails (checks for a "failed" state). I have a Teams webhook that works, and I would like that webhook to be invoked if the Watcher, when run, fails for whatever reason.

So far I've tried combinations of:

ctx.payload.hits.hits.0.\_source.state  
ctx.payload.hits.hits.0.\_source.result.input.status  
ctx.payload.hits.hits.0.fields.status.execution\_state

Any guidance would be greatly appreciated.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/6/7/679720b3b703dd21587cbb7d00c272b49274476d.png)

Thanks in advance,  
Adam

---

<div class="post-metadata">

**Author:** ![ameindel](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@ameindel](https://discuss.elastic.co/u/ameindel)\
**Post date:** [July 20, 2022, 12:55pm UTC](https://discuss.elastic.co/t/how-do-you-extract-a-watchers-state-status-or-execution-state-from-ctx/309952/2 "2022-07-20T12:55:31Z")

</div>

Additional context - I should mention that this is an HTTP input-only Watcher that does not utilize any searches. It simply makes an HTTP GET command to an endpoint. The goal is to eventually create a Dashboard that will retrieve the payload data stored in the .watcher-history index.

I'd like Watcher 'condition' logic to resolve to true if, say a GET to the endpoint fails (which results in a 'state' being set to 'failed').

I've noticed that my HTTP Watcher's response has a null value under 'resolved\_values' for 'state'. When I do a 'normal' Watcher that utilizes a search, I get a value back for that field. This leads me to believe that an HTTP Watcher that doesn't use a search has no execution context (ctx)?

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

So my refined question I suppose would be - is there a way to extract out the 'state' from an HTTP input-only Watcher?

---

<div class="post-metadata">

**Author:** ![richcollier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/richcollier/32/115035_2.png) [@richcollier](https://discuss.elastic.co/u/richcollier)\
**Post date:** [July 28, 2022, 11:32am UTC](https://discuss.elastic.co/t/how-do-you-extract-a-watchers-state-status-or-execution-state-from-ctx/309952/3 "2022-07-28T11:32:48Z")

</div>

An HTTP input does not product the `hits` array like the `search` input does.

Look at this simple example:

```auto
POST _watcher/watch/_execute
{
  "watch": {
    "trigger": {
      "schedule": {
        "interval": "1m"
      }
    },
    "input": {
      "http": {
        "request": {
          "host": "reqres.in",
          "scheme": "https",
          "port": 443,
          "path": "/api/users/2"
        }
      }
    },
    "condition": {
      "compare": {
        "ctx.payload.data.id": {
          "eq": 2
        }
      }
    },
    "actions": {
      "log": {
        "logging": {
          "text": "found user and their email is {{ctx.payload.data.email}}"
        }
      }
    }
  }
}

```

Note that the result is stored in `ctx.payload` and that in my case, the interesting bits are in the `data` section:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/1/c/1c8c9bd223f7e38a29fb5932cfc547a49b8873b7.png)

In this example, I just spit out the user's email address gleaned from the API call

---

<div class="post-metadata">

**Author:** ![ameindel](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@ameindel](https://discuss.elastic.co/u/ameindel)\
**Post date:** [July 28, 2022, 6:48pm UTC](https://discuss.elastic.co/t/how-do-you-extract-a-watchers-state-status-or-execution-state-from-ctx/309952/4 "2022-07-28T18:48:54Z")

</div>

Hi, Rich.

Thank you very much for your response. This cleared up some misunderstandings I had on how a Watcher-involved ctx is populated, depending on the context of usage (search input or http input).

To get around my original issue, I simply created another Watcher (to watch the original Watcher). This new Watcher utilizes a search input, which returns the latest execution document from the .watcher-history index, and it digs for the 'status' successfully.

Thanks again!

---

<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:** [August 25, 2022, 6:49pm UTC](https://discuss.elastic.co/t/how-do-you-extract-a-watchers-state-status-or-execution-state-from-ctx/309952/5 "2022-08-25T18:49:02Z")

</div>

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