# Painless script condition in watcher

**URL:** <https://discuss.elastic.co/t/painless-script-condition-in-watcher/85496>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [May 12, 2017, 1:53am UTC](https://discuss.elastic.co/t/painless-script-condition-in-watcher/85496 "2017-05-12T01:53:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![higee](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/higee/32/22343_2.png) [@higee](https://discuss.elastic.co/u/higee)\
**Post date:** [May 12, 2017, 1:53am UTC](https://discuss.elastic.co/t/painless-script-condition-in-watcher/85496/1 "2017-05-12T01:53:05Z")

</div>

Hi I'm trying to make an alert system that matches following condition.

Each document has following fields.

- status # either 'incomplete' or 'complete'
- insert\_time # timestamp when the document has been inserted

And the condition when I want alert to be sent is as follows.  
select documents

1. whose time difference between 'insert\_time' and current\_time stamp is greater than 20minutes
2. whose status is incomplete.

My condition code is as follows.  
( I haven't included 2) part here yet)

> "condition": {  
> "compare": {  
> "((new Date().getTime()/1000)-doc['this\_day'].value)/60" :  
> { "gt" : 20  
> }  
> }  
> }

Alarm itself is working properly every one minute but not getting email. (not 'firing')  
So I'm wondering what I've done wrong.

 ![](https://us1.discourse-cdn.com/elastic/original/3X/4/9/4969f22ead81a6641ba534a36de5ddea2d2f63e5.png)

Thx in advance.

Best

Gee

---

<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:** [May 15, 2017, 9:13pm UTC](https://discuss.elastic.co/t/painless-script-condition-in-watcher/85496/2 "2017-05-15T21:13:27Z")

</div>

Hey,

two things:

- You are using a compare condition, which does not use scripting
- You dont need a `now` date, you can use the scheduled time of the watch and use this for the comparison

You should also use the execute watch API and see if/how your watch gets executed.

--Alex

---

<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:** [June 12, 2017, 9:24pm UTC](https://discuss.elastic.co/t/painless-script-condition-in-watcher/85496/3 "2017-06-12T21:24:03Z")

</div>

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