--- title: StateDurationNode (Kapacitor TICKscript node) description: StateDurationNode computes the duration of a given state (defined using a lambda expression). note: Auto generated by tickdoc menu: kapacitor_1_4: name: StateDurationNode identifier: state_duration_node weight: 270 parent: TICKscript nodes --- ### Constructor | Chaining Method | Description | |:---------|:---------| | **[stateDuration](#descr) ( `expression` `ast.LambdaNode`)** | Create a node that tracks duration in a given state. | ### Property Methods | Setters | Description | |:---|:---| | **[as](#as) ( `value` `string`)** | The new name of the resulting duration field. Default: 'state_duration' | | **[unit](#unit) ( `value` `time.Duration`)** | The time unit of the resulting duration value. Default: 1s. | ### Chaining Methods [Alert](/kapacitor/v1.4/nodes/state_duration_node/#alert), [Barrier](/kapacitor/v1.4/nodes/state_duration_node/#barrier), [Bottom](/kapacitor/v1.4/nodes/state_duration_node/#bottom), [Combine](/kapacitor/v1.4/nodes/state_duration_node/#combine), [Count](/kapacitor/v1.4/nodes/state_duration_node/#count), [CumulativeSum](/kapacitor/v1.4/nodes/state_duration_node/#cumulativesum), [Deadman](/kapacitor/v1.4/nodes/state_duration_node/#deadman), [Default](/kapacitor/v1.4/nodes/state_duration_node/#default), [Delete](/kapacitor/v1.4/nodes/state_duration_node/#delete), [Derivative](/kapacitor/v1.4/nodes/state_duration_node/#derivative), [Difference](/kapacitor/v1.4/nodes/state_duration_node/#difference), [Distinct](/kapacitor/v1.4/nodes/state_duration_node/#distinct), [Ec2Autoscale](/kapacitor/v1.4/nodes/state_duration_node/#ec2autoscale), [Elapsed](/kapacitor/v1.4/nodes/state_duration_node/#elapsed), [Eval](/kapacitor/v1.4/nodes/state_duration_node/#eval), [First](/kapacitor/v1.4/nodes/state_duration_node/#first), [Flatten](/kapacitor/v1.4/nodes/state_duration_node/#flatten), [GroupBy](/kapacitor/v1.4/nodes/state_duration_node/#groupby), [HoltWinters](/kapacitor/v1.4/nodes/state_duration_node/#holtwinters), [HoltWintersWithFit](/kapacitor/v1.4/nodes/state_duration_node/#holtwinterswithfit), [HttpOut](/kapacitor/v1.4/nodes/state_duration_node/#httpout), [HttpPost](/kapacitor/v1.4/nodes/state_duration_node/#httppost), [InfluxDBOut](/kapacitor/v1.4/nodes/state_duration_node/#influxdbout), [Join](/kapacitor/v1.4/nodes/state_duration_node/#join), [K8sAutoscale](/kapacitor/v1.4/nodes/state_duration_node/#k8sautoscale), [KapacitorLoopback](/kapacitor/v1.4/nodes/state_duration_node/#kapacitorloopback), [Last](/kapacitor/v1.4/nodes/state_duration_node/#last), [Log](/kapacitor/v1.4/nodes/state_duration_node/#log), [Max](/kapacitor/v1.4/nodes/state_duration_node/#max), [Mean](/kapacitor/v1.4/nodes/state_duration_node/#mean), [Median](/kapacitor/v1.4/nodes/state_duration_node/#median), [Min](/kapacitor/v1.4/nodes/state_duration_node/#min), [Mode](/kapacitor/v1.4/nodes/state_duration_node/#mode), [MovingAverage](/kapacitor/v1.4/nodes/state_duration_node/#movingaverage), [Percentile](/kapacitor/v1.4/nodes/state_duration_node/#percentile), [Sample](/kapacitor/v1.4/nodes/state_duration_node/#sample), [Shift](/kapacitor/v1.4/nodes/state_duration_node/#shift), [Sideload](/kapacitor/v1.4/nodes/state_duration_node/#sideload), [Spread](/kapacitor/v1.4/nodes/state_duration_node/#spread), [StateCount](/kapacitor/v1.4/nodes/state_duration_node/#statecount), [StateDuration](/kapacitor/v1.4/nodes/state_duration_node/#stateduration), [Stats](/kapacitor/v1.4/nodes/state_duration_node/#stats), [Stddev](/kapacitor/v1.4/nodes/state_duration_node/#stddev), [Sum](/kapacitor/v1.4/nodes/state_duration_node/#sum), [SwarmAutoscale](/kapacitor/v1.4/nodes/state_duration_node/#swarmautoscale), [Top](/kapacitor/v1.4/nodes/state_duration_node/#top), [Union](/kapacitor/v1.4/nodes/state_duration_node/#union), [Where](/kapacitor/v1.4/nodes/state_duration_node/#where), [Window](/kapacitor/v1.4/nodes/state_duration_node/#window)

### Description Compute the duration of a given state. The state is defined using a lambda expression. For each consecutive point for which the expression evaluates as true, the state duration will be incremented by the duration between points. When a point evaluates as false, the state duration is reset. The state duration will be added as an additional field to each point. If the expression evaluates as false, the value will be `-1`. If the expression generates an error during evaluation, the point is discarded, and does not affect the state duration. Example: ```javascript stream |from() .measurement('cpu') |where(lambda: "cpu" == 'cpu-total') |groupBy('host') |stateDuration(lambda: "usage_idle" <= 10) .unit(1m) |alert() // Warn after 1 minute .warn(lambda: "state_duration" >= 1) // Critical after 5 minutes .crit(lambda: "state_duration" >= 5) ``` Note that as the first point in the given state has no previous point, its state duration will be 0.
Properties ---------- Property methods modify state on the calling node. They do not add another node to the pipeline, and always return a reference to the calling node. Property methods are marked using the `.` operator. ### As The new name of the resulting duration field. Default: 'state_duration' ```javascript stateDuration.as(value string) ``` ### Unit The time unit of the resulting duration value. Default: 1s. ```javascript stateDuration.unit(value time.Duration) ``` Chaining Methods ---------------- Chaining methods create a new node in the pipeline as a child of the calling node. They do not modify the calling node. Chaining methods are marked using the `|` operator. ### Alert Create an alert node, which can trigger alerts. ```javascript stateDuration|alert() ``` Returns: [AlertNode](/kapacitor/v1.4/nodes/alert_node/) ### Barrier Create a new Barrier node that emits a BarrierMessage periodically One BarrierMessage will be emitted every period duration ```javascript stateDuration|barrier() ``` Returns: [BarrierNode](/kapacitor/v1.4/nodes/barrier_node/) ### Bottom Select the bottom `num` points for `field` and sort by any extra tags or fields. ```javascript stateDuration|bottom(num int64, field string, fieldsAndTags ...string) ``` Returns: [InfluxQLNode](/kapacitor/v1.4/nodes/influx_q_l_node/) ### Combine Combine this node with itself. The data is combined on timestamp. ```javascript stateDuration|combine(expressions ...ast.LambdaNode) ``` Returns: [CombineNode](/kapacitor/v1.4/nodes/combine_node/) ### Count Count the number of points. ```javascript stateDuration|count(field string) ``` Returns: [InfluxQLNode](/kapacitor/v1.4/nodes/influx_q_l_node/) ### CumulativeSum Compute a cumulative sum of each point that is received. A point is emitted for every point collected. ```javascript stateDuration|cumulativeSum(field string) ``` Returns: [InfluxQLNode](/kapacitor/v1.4/nodes/influx_q_l_node/) ### Deadman Helper function for creating an alert on low throughput, a.k.a. deadman's switch. - Threshold -- trigger alert if throughput drops below threshold in points/interval. - Interval -- how often to check the throughput. - Expressions -- optional list of expressions to also evaluate. Useful for time of day alerting. Example: ```javascript var data = stream |from()... // Trigger critical alert if the throughput drops below 100 points per 10s and checked every 10s. data |deadman(100.0, 10s) //Do normal processing of data data... ``` The above is equivalent to this Example: ```javascript var data = stream |from()... // Trigger critical alert if the throughput drops below 100 points per 10s and checked every 10s. data |stats(10s) .align() |derivative('emitted') .unit(10s) .nonNegative() |alert() .id('node \'stream0\' in task \'{{ .TaskName }}\'') .message('{{ .ID }} is {{ if eq .Level "OK" }}alive{{ else }}dead{{ end }}: {{ index .Fields "emitted" | printf "%0.3f" }} points/10s.') .crit(lambda: "emitted" <= 100.0) //Do normal processing of data data... ``` The `id` and `message` alert properties can be configured globally via the 'deadman' configuration section. Since the [AlertNode](/kapacitor/v1.4/nodes/alert_node/) is the last piece it can be further modified as usual. Example: ```javascript var data = stream |from()... // Trigger critical alert if the throughput drops below 100 points per 10s and checked every 10s. data |deadman(100.0, 10s) .slack() .channel('#dead_tasks') //Do normal processing of data data... ``` You can specify additional lambda expressions to further constrain when the deadman's switch is triggered. Example: ```javascript var data = stream |from()... // Trigger critical alert if the throughput drops below 100 points per 10s and checked every 10s. // Only trigger the alert if the time of day is between 8am-5pm. data |deadman(100.0, 10s, lambda: hour("time") >= 8 AND hour("time") <= 17) //Do normal processing of data data... ``` ```javascript stateDuration|deadman(threshold float64, interval time.Duration, expr ...ast.LambdaNode) ``` Returns: [AlertNode](/kapacitor/v1.4/nodes/alert_node/) ### Default Create a node that can set defaults for missing tags or fields. ```javascript stateDuration|default() ``` Returns: [DefaultNode](/kapacitor/v1.4/nodes/default_node/) ### Delete Create a node that can delete tags or fields. ```javascript stateDuration|delete() ``` Returns: [DeleteNode](/kapacitor/v1.4/nodes/delete_node/) ### Derivative Create a new node that computes the derivative of adjacent points. ```javascript stateDuration|derivative(field string) ``` Returns: [DerivativeNode](/kapacitor/v1.4/nodes/derivative_node/) ### Difference Compute the difference between points independent of elapsed time. ```javascript stateDuration|difference(field string) ``` Returns: [InfluxQLNode](/kapacitor/v1.4/nodes/influx_q_l_node/) ### Distinct Produce batch of only the distinct points. ```javascript stateDuration|distinct(field string) ``` Returns: [InfluxQLNode](/kapacitor/v1.4/nodes/influx_q_l_node/) ### Ec2Autoscale Create a node that can trigger autoscale events for a ec2 autoscalegroup. ```javascript stateDuration|ec2Autoscale() ``` Returns: [Ec2AutoscaleNode](/kapacitor/v1.4/nodes/ec2_autoscale_node/) ### Elapsed Compute the elapsed time between points ```javascript stateDuration|elapsed(field string, unit time.Duration) ``` Returns: [InfluxQLNode](/kapacitor/v1.4/nodes/influx_q_l_node/) ### Eval Create an eval node that will evaluate the given transformation function to each data point. A list of expressions may be provided and will be evaluated in the order they are given. The results are available to later expressions. ```javascript stateDuration|eval(expressions ...ast.LambdaNode) ``` Returns: [EvalNode](/kapacitor/v1.4/nodes/eval_node/) ### First Select the first point. ```javascript stateDuration|first(field string) ``` Returns: [InfluxQLNode](/kapacitor/v1.4/nodes/influx_q_l_node/) ### Flatten Flatten points with similar times into a single point. ```javascript stateDuration|flatten() ``` Returns: [FlattenNode](/kapacitor/v1.4/nodes/flatten_node/) ### GroupBy Group the data by a set of tags. Can pass literal * to group by all dimensions. Example: ```javascript |groupBy(*) ``` ```javascript stateDuration|groupBy(tag ...interface{}) ``` Returns: [GroupByNode](/kapacitor/v1.4/nodes/group_by_node/) ### HoltWinters Compute the holt-winters (/{{< latest "influxdb" "v1" >}}/query_language/functions/#holt-winters) forecast of a data set. ```javascript stateDuration|holtWinters(field string, h int64, m int64, interval time.Duration) ``` Returns: [InfluxQLNode](/kapacitor/v1.4/nodes/influx_q_l_node/) ### HoltWintersWithFit Compute the holt-winters (/{{< latest "influxdb" "v1" >}}/query_language/functions/#holt-winters) forecast of a data set. This method also outputs all the points used to fit the data in addition to the forecasted data. ```javascript stateDuration|holtWintersWithFit(field string, h int64, m int64, interval time.Duration) ``` Returns: [InfluxQLNode](/kapacitor/v1.4/nodes/influx_q_l_node/) ### HttpOut Create an HTTP output node that caches the most recent data it has received. The cached data is available at the given endpoint. The endpoint is the relative path from the API endpoint of the running task. For example, if the task endpoint is at `/kapacitor/v1/tasks/` and endpoint is `top10`, then the data can be requested from `/kapacitor/v1/tasks//top10`. ```javascript stateDuration|httpOut(endpoint string) ``` Returns: [HTTPOutNode](/kapacitor/v1.4/nodes/http_out_node/) ### HttpPost Creates an HTTP Post node that POSTS received data to the provided HTTP endpoint. HttpPost expects 0 or 1 arguments. If 0 arguments are provided, you must specify an endpoint property method. ```javascript stateDuration|httpPost(url ...string) ``` Returns: [HTTPPostNode](/kapacitor/v1.4/nodes/http_post_node/) ### InfluxDBOut Create an influxdb output node that will store the incoming data into InfluxDB. ```javascript stateDuration|influxDBOut() ``` Returns: [InfluxDBOutNode](/kapacitor/v1.4/nodes/influx_d_b_out_node/) ### Join Join this node with other nodes. The data is joined on timestamp. ```javascript stateDuration|join(others ...Node) ``` Returns: [JoinNode](/kapacitor/v1.4/nodes/join_node/) ### K8sAutoscale Create a node that can trigger autoscale events for a kubernetes cluster. ```javascript stateDuration|k8sAutoscale() ``` Returns: [K8sAutoscaleNode](/kapacitor/v1.4/nodes/k8s_autoscale_node/) ### KapacitorLoopback Create an kapacitor loopback node that will send data back into Kapacitor as a stream. ```javascript stateDuration|kapacitorLoopback() ``` Returns: [KapacitorLoopbackNode](/kapacitor/v1.4/nodes/kapacitor_loopback_node/) ### Last Select the last point. ```javascript stateDuration|last(field string) ``` Returns: [InfluxQLNode](/kapacitor/v1.4/nodes/influx_q_l_node/) ### Log Create a node that logs all data it receives. ```javascript stateDuration|log() ``` Returns: [LogNode](/kapacitor/v1.4/nodes/log_node/) ### Max Select the maximum point. ```javascript stateDuration|max(field string) ``` Returns: [InfluxQLNode](/kapacitor/v1.4/nodes/influx_q_l_node/) ### Mean Compute the mean of the data. ```javascript stateDuration|mean(field string) ``` Returns: [InfluxQLNode](/kapacitor/v1.4/nodes/influx_q_l_node/) ### Median Compute the median of the data. Note, this method is not a selector, if you want the median point use `.percentile(field, 50.0)`. ```javascript stateDuration|median(field string) ``` Returns: [InfluxQLNode](/kapacitor/v1.4/nodes/influx_q_l_node/) ### Min Select the minimum point. ```javascript stateDuration|min(field string) ``` Returns: [InfluxQLNode](/kapacitor/v1.4/nodes/influx_q_l_node/) ### Mode Compute the mode of the data. ```javascript stateDuration|mode(field string) ``` Returns: [InfluxQLNode](/kapacitor/v1.4/nodes/influx_q_l_node/) ### MovingAverage Compute a moving average of the last window points. No points are emitted until the window is full. ```javascript stateDuration|movingAverage(field string, window int64) ``` Returns: [InfluxQLNode](/kapacitor/v1.4/nodes/influx_q_l_node/) ### Percentile Select a point at the given percentile. This is a selector function, no interpolation between points is performed. ```javascript stateDuration|percentile(field string, percentile float64) ``` Returns: [InfluxQLNode](/kapacitor/v1.4/nodes/influx_q_l_node/) ### Sample Create a new node that samples the incoming points or batches. One point will be emitted every count or duration specified. ```javascript stateDuration|sample(rate interface{}) ``` Returns: [SampleNode](/kapacitor/v1.4/nodes/sample_node/) ### Shift Create a new node that shifts the incoming points or batches in time. ```javascript stateDuration|shift(shift time.Duration) ``` Returns: [ShiftNode](/kapacitor/v1.4/nodes/shift_node/) ### Sideload Create a node that can load data from external sources ```javascript stateDuration|sideload() ``` Returns: [SideloadNode](/kapacitor/v1.4/nodes/sideload_node/) ### Spread Compute the difference between `min` and `max` points. ```javascript stateDuration|spread(field string) ``` Returns: [InfluxQLNode](/kapacitor/v1.4/nodes/influx_q_l_node/) ### StateCount Create a node that tracks number of consecutive points in a given state. ```javascript stateDuration|stateCount(expression ast.LambdaNode) ``` Returns: [StateCountNode](/kapacitor/v1.4/nodes/state_count_node/) ### StateDuration Create a node that tracks duration in a given state. ```javascript stateDuration|stateDuration(expression ast.LambdaNode) ``` Returns: [StateDurationNode](/kapacitor/v1.4/nodes/state_duration_node/) ### Stats Create a new stream of data that contains the internal statistics of the node. The interval represents how often to emit the statistics based on real time. This means the interval time is independent of the times of the data points the source node is receiving. ```javascript stateDuration|stats(interval time.Duration) ``` Returns: [StatsNode](/kapacitor/v1.4/nodes/stats_node/) ### Stddev Compute the standard deviation. ```javascript stateDuration|stddev(field string) ``` Returns: [InfluxQLNode](/kapacitor/v1.4/nodes/influx_q_l_node/) ### Sum Compute the sum of all values. ```javascript stateDuration|sum(field string) ``` Returns: [InfluxQLNode](/kapacitor/v1.4/nodes/influx_q_l_node/) ### SwarmAutoscale Create a node that can trigger autoscale events for a docker swarm cluster. ```javascript stateDuration|swarmAutoscale() ``` Returns: [SwarmAutoscaleNode](/kapacitor/v1.4/nodes/swarm_autoscale_node/) ### Top Select the top `num` points for `field` and sort by any extra tags or fields. ```javascript stateDuration|top(num int64, field string, fieldsAndTags ...string) ``` Returns: [InfluxQLNode](/kapacitor/v1.4/nodes/influx_q_l_node/) ### Union Perform the union of this node and all other given nodes. ```javascript stateDuration|union(node ...Node) ``` Returns: [UnionNode](/kapacitor/v1.4/nodes/union_node/) ### Where Create a new node that filters the data stream by a given expression. ```javascript stateDuration|where(expression ast.LambdaNode) ``` Returns: [WhereNode](/kapacitor/v1.4/nodes/where_node/) ### Window Create a new node that windows the stream by time. NOTE: Window can only be applied to stream edges. ```javascript stateDuration|window() ``` Returns: [WindowNode](/kapacitor/v1.4/nodes/window_node/)