# Watcher - need some help

**URL:** https://discuss.elastic.co/t/watcher-need-some-help/90971
**Category:** Elasticsearch
**Created:** [June 27, 2017, 2:06pm UTC](https://discuss.elastic.co/t/watcher-need-some-help/90971 "2017-06-27T14:06:31Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Wayne\_Taylor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wayne_taylor/32/45984_2.png) [@Wayne\_Taylor](https://discuss.elastic.co/u/Wayne_Taylor)
#### Post date: [June 27, 2017, 2:06pm UTC](https://discuss.elastic.co/t/watcher-need-some-help/90971/1 "2017-06-27T14:06:31Z")

</div>

Team,

I have an ES hosted cloud solution loading in all documents from our purchase path including the products, prices etc.

I want to add a watcher that is scheduled to run every 5 minutes and compares to the previous 5 minutes and alert if there was a sharp decrease. Purchases vary throughout the day so this dynamic.

I have the following watcher but getting a null point exception on the scripting for the condition. Appreciate your assistance:

PUT /\_watcher/watch/purchase-decrease-test

> ```
> {
> "trigger" : {
> "schedule" : {
> "interval" : "5m"
> }
> },
> "input" : {
> "search" : {
> "request": {
> "indices": "purchases",
> "body": {
> "size": 0,
> "query": {
> "bool": {
> "filter":
> {
> "range": {
> "datetimestamp_created": {
> "from": "now-10m",
> "to": "now"
> }
> }
> }
> }
> },
> "aggs": {
> "five_min":{
> "filters": {
> "filters": {
> "latest5":{
> "range":{
> "datetimestamp_created": { "gte": "now-5m", "lte": "now"}
> }
> },
> "previous5": {
> "range": {
> "datetimestamp_created": { "gte": "now-10m", "lt": "now-5m"}
> }
> }
> }
> }
> }
> }
> }
> }
> },
> "condition" : {
> "script": {
> "inline": "return ctx.payload.aggregations.five_min.bucket.latest5.value < 0.5 *ctx.payload.aggregations.five_min.buckets.previous5.value"
> }
> },
> "transform" : {},
> "actions" : {
> "email_admin" : {
> "email": {
> "to": "myemail@mydomain.com",
> "subject": "No Purchases found for test",
> "body": ""
> }
> } 
> }
> }
> 
> ```

When I run this is the error:

> "exception": {  
> "type": "script\_exception",  
> "reason": "runtime error",  
> "script\_stack": [  
> "return ctx.payload.aggregations.five\_min.bucket.latest5.value \< 0.5 \*ctx.payload.aggregations.five\_min.buckets.previous5.value",  
> " ^---- HERE"  
> ],  
> "script": "return ctx.payload.aggregations.five\_min.bucket.latest5.value \< 0.5 \*ctx.payload.aggregations.five\_min.buckets.previous5.value",  
> "lang": "painless",  
> "caused\_by": {  
> "type": "null\_pointer\_exception",  
> "reason": null,  
> "stack\_trace": """  
> java.lang.NullPointerException  
> at org.elasticsearch.painless.DefBootstrap$PIC.fallback(DefBootstrap.java:200)  
> at org.elasticsearch.painless.Executable$Script.execute(return ctx.payload.aggregations.five\_min.bucket.latest5.value \< 0.5 \*ctx.payload.aggregations.five\_min.buckets.previous5.value:48)  
> at org.elasticsearch.painless.ScriptImpl.run(ScriptImpl.java:123)  
> at org.elasticsearch.xpack.watcher.condition.ScriptCondition.doExecute(ScriptCondition.java:94)  
> at org.elasticsearch.xpack.watcher.condition.ScriptCondition.execute(ScriptCondition.java:84)  
> at org.elasticsearch.xpack.watcher.execution.ExecutionService.executeInner(ExecutionService.java:391)  
> at org.elasticsearch.xpack.watcher.execution.ExecutionService.execute(ExecutionService.java:275)  
> at org.elasticsearch.xpack.watcher.transport.actions.execute.TransportExecuteWatchAction.masterOperation(TransportExecuteWatchAction.java:136)  
> at org.elasticsearch.xpack.watcher.transport.actions.execute.TransportExecuteWatchAction.masterOperation(TransportExecuteWatchAction.java:64)  
> at org.elasticsearch.action.support.master.TransportMasterNodeAction.masterOperation(TransportMasterNodeAction.java:87)  
> at org.elasticsearch.action.support.master.TransportMasterNodeAction$AsyncSingleAction$2.doRun(TransportMasterNodeAction.java:166)  
> at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:613)  
> at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)  
> at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)  
> at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)  
> at java.lang.Thread.run(Thread.java:745)

> """  
> },  
> "stack\_trace": """  
> ScriptException[runtime error]; nested: NullPointerException;  
> at org.elasticsearch.painless.ScriptImpl.convertToScriptException(ScriptImpl.java:181)  
> at org.elasticsearch.painless.ScriptImpl.run(ScriptImpl.java:128)  
> at org.elasticsearch.xpack.watcher.condition.ScriptCondition.doExecute(ScriptCondition.java:94)  
> at org.elasticsearch.xpack.watcher.condition.ScriptCondition.execute(ScriptCondition.java:84)  
> at org.elasticsearch.xpack.watcher.execution.ExecutionService.executeInner(ExecutionService.java:391)  
> at org.elasticsearch.xpack.watcher.execution.ExecutionService.execute(ExecutionService.java:275)  
> at org.elasticsearch.xpack.watcher.transport.actions.execute.TransportExecuteWatchAction.masterOperation(TransportExecuteWatchAction.java:136)  
> at org.elasticsearch.xpack.watcher.transport.actions.execute.TransportExecuteWatchAction.masterOperation(TransportExecuteWatchAction.java:64)  
> at org.elasticsearch.action.support.master.TransportMasterNodeAction.masterOperation(TransportMasterNodeAction.java:87)  
> at org.elasticsearch.action.support.master.TransportMasterNodeAction$AsyncSingleAction$2.doRun(TransportMasterNodeAction.java:166)  
> at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:613)  
> at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)  
> at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)  
> at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)  
> at java.lang.Thread.run(Thread.java:745)  
> Caused by: java.lang.NullPointerException  
> at org.elasticsearch.painless.DefBootstrap$PIC.fallback(DefBootstrap.java:200)  
> at org.elasticsearch.painless.Executable$Script.execute(return ctx.payload.aggregations.five\_min.bucket.latest5.value \< 0.5 \*ctx.payload.aggregations.five\_min.buckets.previous5.value:48)  
> at org.elasticsearch.painless.ScriptImpl.run(ScriptImpl.java:123)  
> ... 13 more

> """

---

<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: [July 25, 2017, 2:06pm UTC](https://discuss.elastic.co/t/watcher-need-some-help/90971/2 "2017-07-25T14:06:51Z")

</div>

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