Cannot Compare Last Value to Rolling Mean?

Hey guys,

Been fiddling with an alert that seems like it should be fairly simple to me, but for some reason it is never triggering. My intention is to alert if the latest value of a metric is 2 times greater than the rolling mean for that metric.

My metric, elec_cons_kwh is emitted at one minute intervals, so I am attempting to take the average of the last ten minutes

My tick script is below. Can someone point out what I’m doing wrong, as I’ve been baffled by this for awhile now.

dbrp “agents”.“autogen”

var rolling_mean = stream
|from()
.database(‘agents’)
.measurement(‘agents’)
.groupBy(‘device_id’)
|window()
.periodCount(10)
.everyCount(1)
|mean(‘elec_cons_kwh’)

stream
|from()
.database(‘agents’)
.measurement(‘agents’)
.groupBy(‘device_id’)
|window()
.periodCount(10)
.everyCount(1)
|last(‘elec_cons_kwh’)
.as(‘last’)
|alert()
.id(‘out of bounds electricity consumption alert’)
.crit(lambda: “last” > 2 * “rolling_mean.mean”)
.levelField(‘level’)
.log(’/var/log/kapacitor/alerts.log’)
|influxDBOut()
.database(‘agents’)
.retentionPolicy(‘autogen’)
.measurement(‘alerts’)
.tag(‘anomaly_type’,‘out_of_bounds_measurement’)
.tag(‘anomaly_type’,‘out_of_bounds_measurement’)