How can I compare current value in my stream with previous value in tickscript?

Can you remove the following ?

|window()
.period(10m)
.every(10m)
.align()

@MarcV I removed the code you mentioned. I used derivative. I still didn’t receive any email alert? Could you test it on your side? I am totally new to this, so I am struggling a bit. But from what I read in documentation, won’t the code I posted work?

@MarcV The derivative function is not picking up the values and generating alerts. I sent a QCOUNT of 600 and then later I sent QCOUNT of 650. The current value 650 is greater than previous value 600 by 50. It is supposed to generate alert. But it didn’t send any email alert.

How do I go about this?

Hi , in your var message you use index .Tags “value” but value is a field …

I would simplify the alertnode and put only
|log() after the .crit line and see if it works.
Do you have data in the alerts measurement ?

@MarcV I tried it by removing it as well. I gave just a simple string in message but it still isn’t working as per the use case I mentioned. I am wondering if it’s achievable.

This is the updated script and it still isn’t generating alerts.

var db = 'telegraf'

var rp = 'autogen'

var measurement = 'E Plus'

var groupBy = []

var whereFilter = lambda: ("domain" == 'pgames') AND ("statsName" == 'Q_Count')

var name = 'QCOUNT0320-1'

var idVar = name

var message = 'subject'

var idTag = 'alertID'

var levelTag = 'level'

var messageField = 'message'

var durationField = 'duration'

var outputDB = 'chronograf'

var outputRP = 'autogen'

var outputMeasurement = 'alerts'

var triggerType = 'threshold'

var details = 'message'

var crit = 0

var data = stream
    |from()
        .database(db)
        .retentionPolicy(rp)
        .measurement(measurement)
        .groupBy(groupBy)
        .where(whereFilter)
    |derivative('QCOUNT')
        .as('value')

var trigger = data
    |alert()
        .crit(lambda: float("value") > crit)
        .stateChangesOnly()
        .message(message)
        .id(idVar)
        .idTag(idTag)
        .levelTag(levelTag)
        .messageField(messageField)
        .durationField(durationField)
        .details(details)
        .email()
        .to('karthik@gmail.com')

trigger
    |eval(lambda: float("value"))
        .as('value')
        .keep()
    |influxDBOut()
        .create()
        .database(outputDB)
        .retentionPolicy(outputRP)
        .measurement(outputMeasurement)
        .tag('alertName', name)
        .tag('triggerType', triggerType)

trigger
    |httpOut('output')

What is the output of : kapacitor show task ?( replace task with your taskname
Can you use a measurement without a space in it ?

I have tried with a measurement EPlus and I get an alert …
I also removed : .stateChangesOnly() because otherwise you will get only an alert the first time that QCOUNT is higher than the previous value …

@MarcV I am receiving mails after implementing what you said. Just one problem is I have set the time interval in units to 300 seconds.

Below is the implementation.

|derivative('QCOUNT')
        .unit(300s)
        .as('value') 

What does the unit for derivative do? I need to get the alert for data in real time. That means the influxDB gets populated with QCOUNT at irregular intervals. So I need to generate alert based on that. So what should I set the value of unit to?

Hi that is good news ,
You can keep the default unit of 1s or set it higher as you did , it is

 The time unit of the resulting derivative         
 value. Default: 1s

The derivative will be smaller if the time between the points increases
but will be still > 0 …

DerivativeNode

Computes the derivative via: (current - previous ) / ( time_difference / unit)

The derivative is computed for each point, and because of boundary conditions the first point is dropped.

Just for information and completeness there is also the . …Difference()

Hi @MarcV Quick question, does this Difference() node give the difference between two adjacent values? What does it exactly compute?

@MarcV Also the script I provided doesn’t seem to be working for odd values. Any idea why?

Hi,
If you click on the link you will see this description

  Compute the difference between points       
  independent of elapsed time.

I will test that , are you still using 300s for the unit ?

@MarcV I am using 1s as unit. Not 300 anymore. However, the alerts are not being generated all the time.

Like I sent data into influxDB 5 times with values, 271, 287, 295, 314, 343 and I got an email alert only for 295 and 314. I understand I won’t get alert for 271 because it’s the first value. But I don’t know why I didn’t receive anything for 287 and 343.

Can you verify?

Hi here it works ,
I am using Kapacitor version 1.5.2

my script :

var message = 'ALERT for : {{ index .Fields "value" }} DERIVATIVE = {{ index .Fields "derivative" }}'
var messageField = 'message'
stream
 |from()
   .measurement('numbers')
 |derivative('value')
   .as('derivative')
 |alert()
  .warn(lambda:"derivative" > 0 )
  .message(message)
  .messageField(messageField)
|influxDBOut()
  .database('telegraf')
  .measurement('kapa_alerts')


ts=2019-03-22T09:58:03.261+01:00 lvl=debug msg="alert triggered" service=kapacitor task_master=main task=taskdeadman node=influxdb_out4 level=WARNING id=numbers:nil event_message="ALERT for : 287 DERIVATIVE = 86.2791161139746" data="&{numbers map[] [time derivative value] [[2019-03-22 08:58:03.26069044 +0000 UTC 86.2791161139746 287]]}"
ts=2019-03-22T09:58:05.602+01:00 lvl=debug msg="alert triggered" service=kapacitor task_master=main task=taskdeadman node=influxdb_out4 level=WARNING id=numbers:nil event_message="ALERT for : 295 DERIVATIVE = 3.417093135943251" data="&{numbers map[] [time derivative value] [[2019-03-22 08:58:05.601861629 +0000 UTC 3.417093135943251 295]]}"
ts=2019-03-22T09:58:07.334+01:00 lvl=debug msg="alert triggered" service=kapacitor task_master=main task=taskdeadman node=influxdb_out4 level=WARNING id=numbers:nil event_message="ALERT for : 314 DERIVATIVE = 10.974586307597365" data="&{numbers map[] [time derivative value] [[2019-03-22 08:58:07.333134177 +0000 UTC 10.974586307597365 314]]}"
ts=2019-03-22T09:58:09.613+01:00 lvl=debug msg="alert triggered" service=kapacitor task_master=main task=taskdeadman node=influxdb_out4 level=WARNING id=numbers:nil event_message="ALERT for : 343 DERIVATIVE = 12.721460603778104" data="&{numbers map[] [time derivative value] [[2019-03-22 08:58:09.612746629 +0000 UTC 12.721460603778104 343]]}"

> select * from kapa_alerts order by desc limit 5
name: kapa_alerts
time                           derivative         message                                         value
----                           ----------         -------                                         -----
2019-03-22T08:58:09.612746629Z 12.721460603778104 ALERT for : 343 DERIVATIVE = 12.721460603778104 343
2019-03-22T08:58:07.333134177Z 10.974586307597365 ALERT for : 314 DERIVATIVE = 10.974586307597365 314
2019-03-22T08:58:05.601861629Z 3.417093135943251  ALERT for : 295 DERIVATIVE = 3.417093135943251  295
2019-03-22T08:58:03.26069044Z  86.2791161139746   ALERT for : 287 DERIVATIVE = 86.2791161139746   287