# stateChangesOnly() does not work as expected

**URL:** <https://community.influxdata.com/t/statechangesonly-does-not-work-as-expected/17350>\
**Category:** Fluxlang\
**Tags:** influxdb, flux\
**Created:** [December 24, 2020, 7:26am UTC](https://community.influxdata.com/t/statechangesonly-does-not-work-as-expected/17350 "2020-12-24T07:26:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![delapuentem](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/delapuentem/32/8616_2.png) [@delapuentem](https://community.influxdata.com/u/delapuentem)\
**Post date:** [December 24, 2020, 7:26am UTC](https://community.influxdata.com/t/statechangesonly-does-not-work-as-expected/17350/1 "2020-12-24T07:26:44Z")

</div>

I have a task that checks if a value exceeds a threshold and reports by slack.

This task has an interval of 10 minutes, the problem is that it notifies every 10 minutes even with the same status.

I am storing in my bucket a \_level (tag) column with critical and ok values.

But when I use monitor.stateChangesOnly () it never notify by slack when \_level changes from critical to ok or ok to critical. Does monitor.stateChangesOnly () work the same as statechangeonly in kapacitor?

I do not know what I’m doing wrong.

The Slack notification task:

```
import "influxdata/influxdb/monitor"
import "slack"

option task = {name: "notify slack", every: 10m}

toSlack = slack.endpoint(url: "mySecretWebhook", token: "")

notify = from(bucket: "alerts")
    |> range(start: -task.every)
    |> filter(fn: (r) => (r._measurement == "alerts" and r._field == "temp"))     
    |> keep(columns: ["_time", "_value", "_field", "_level", "location", "alertid"])
    |> group(columns: ["location", "_level", "alertid"])
    |> last()

notify
    |> monitor.stateChangesOnly()
    |> toSlack(mapFn: (r) =>
        ({r with channel: "#alertas-influxdb", text: "${r.alertid} ${r.location} ${r._level} Value: " + string(v: r._value) + " Threshold: 18", color: "info"}))()
```

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [December 28, 2020, 6:33pm UTC](https://community.influxdata.com/t/statechangesonly-does-not-work-as-expected/17350/2 "2020-12-28T18:33:55Z")

</div>

Hello @delapuentem,  
Just to confirm, stateChangesOnly is notifying when the level has **not** changed? That’s not right. It should only notify when the state changes as per documentation:

> **[monitor.stateChangesOnly() function | Flux 0.x Documentation](https://docs.influxdata.com/flux/v0.x/stdlib/influxdata/influxdb/monitor/statechangesonly/)**
>
> monitor.stateChangesOnly() takes a stream of tables that contains a \_level column and returns a stream of tables grouped by \_level where each record represents a state change.

Are there values after

```auto
notify
    |> monitor.stateChangesOnly()

```

If your state hasn’t changed I don’t expect to see any values. I tested it for myself on InfluxDB cloud with system plugin. I set all “\_level” values to “critical”:

```auto
import "influxdata/influxdb/monitor"
from(bucket: "my-bucket")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "cpu")
  |> filter(fn: (r) => r["_field"] == "usage_system")
  |> filter(fn: (r) => r["cpu"] == "cpu-total")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> map(fn: (r) => ({ r with
    _level:
      if r._value >= 0.0 then "critical" else "normal" }))
  |> monitor.stateChangesOnly()

```

No values are returned.

What message are you getting to slack?

---

<div class="post-metadata">

**Author:** ![delapuentem](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/delapuentem/32/8616_2.png) [@delapuentem](https://community.influxdata.com/u/delapuentem)\
**Post date:** [January 1, 2021, 9:29am UTC](https://community.influxdata.com/t/statechangesonly-does-not-work-as-expected/17350/3 "2021-01-01T09:29:47Z")

</div>

Hello @Anaisdg , thank you very much for your answer !! Sorry, I’ll try to explain myself better.

I have this table with this data.

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/3/386987b0faa1190584eaa516f7024718187b6fea.png)

I understand that if I apply the stateChangesOnly () function it would return the “crit” with value 14 and the “ok” with the value 2, according to the documentation.

But it doesn’t work as I expect.

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/d/d214b6d9e88318a63b9c7d1a0e86f8e0e4ce0172.png)

```
import "influxdata/influxdb/monitor"

from(bucket: "alertas")
  |> range(start: -1h)
  |> filter(fn: (r) => r["_measurement"] == "alertas")
  |> filter(fn: (r) => r["_field"] == "value")
  |> last()
  |> monitor.stateChangesOnly()

```

I have also tried this, as in the documentation, but it has not given me good results.

```
import "influxdata/influxdb/monitor"

from(bucket: "alertas")
  |> range(start: -1h)
  |> monitor.stateChangesOnly()

```

Also if I try stateChanges(toLevel: “ok”) it returns results. But stateChanges(toLevel: “crit”) doesn’t.

stateChanges(toLevel: “ok”)

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/a/a00d1d0833746535fe0694196b20b86c41760c21.png)

stateChanges(toLevel: “crit”)

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/0/094b705138a846847ef4d54bb2b5b196503cda2b.png)

I am using Version 2.0.3  
Thanks and happy new year 🥳 🥳 !

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [January 4, 2021, 4:37pm UTC](https://community.influxdata.com/t/statechangesonly-does-not-work-as-expected/17350/4 "2021-01-04T16:37:42Z")

</div>

Hello @delapuentem,  
That’s because you’re using `last()` so you’re limiting your data to the one line where the status is “ok”. Please try

```auto
import "influxdata/influxdb/monitor"

from(bucket: "alertas")
  |> range(start: -1h)
  |> filter(fn: (r) => r["_measurement"] == "alertas")
  |> filter(fn: (r) => r["_field"] == "value")
  |> monitor.stateChangesOnly()

```

---

<div class="post-metadata">

**Author:** ![delapuentem](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/delapuentem/32/8616_2.png) [@delapuentem](https://community.influxdata.com/u/delapuentem)\
**Post date:** [January 4, 2021, 4:54pm UTC](https://community.influxdata.com/t/statechangesonly-does-not-work-as-expected/17350/5 "2021-01-04T16:54:28Z")

</div>

@Anaisdg My data:

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/b/b3749ecf2471a8da73d92598abfbe182634fb3bd.png)

With stateChangeOnly() :

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/f/f671c9265b007af6690f8b7c5d314f68d9fa9307.png)

the most recent “ok” status is at 21:27, but it shows me another time. Also, the “crit” status does not show it. I am doing something wrong?

Thanks!

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [January 4, 2021, 5:46pm UTC](https://community.influxdata.com/t/statechangesonly-does-not-work-as-expected/17350/6 "2021-01-04T17:46:42Z")

</div>

Can you please try using the exact same query as above with the pivot() and the group() and then apply the stateChangesOnly function.

---

<div class="post-metadata">

**Author:** ![delapuentem](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/delapuentem/32/8616_2.png) [@delapuentem](https://community.influxdata.com/u/delapuentem)\
**Post date:** [January 4, 2021, 7:12pm UTC](https://community.influxdata.com/t/statechangesonly-does-not-work-as-expected/17350/7 "2021-01-04T19:12:31Z")

</div>

hi @Anaisdg, same result, i tried with yield(“last”) and without yield(“last”) :

Could it be a bug in the monitor.stateChangesOnly() function ?

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/d/dd69c7e7b2aca60c3105951e13ba61f3201d114d.png)

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/3/345ad09d8d08d63f47d7fb70b8f3fc3e16955f09.png)

Thanks !

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [January 4, 2021, 7:25pm UTC](https://community.influxdata.com/t/statechangesonly-does-not-work-as-expected/17350/8 "2021-01-04T19:25:41Z")

</div>

Hello @delapuentem,  
I’m not convinced it is yet. Can you please export this data to csv (please note the export CSV button in the bottom right) and share those 5 points with me? Thank you. From this:

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/b/b3749ecf2471a8da73d92598abfbe182634fb3bd.png)

---

<div class="post-metadata">

**Author:** ![delapuentem](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/delapuentem/32/8616_2.png) [@delapuentem](https://community.influxdata.com/u/delapuentem)\
**Post date:** [January 4, 2021, 7:52pm UTC](https://community.influxdata.com/t/statechangesonly-does-not-work-as-expected/17350/9 "2021-01-04T19:52:41Z")

</div>

@Anaisdg sure!

I have added the .txt extension to the file, as .csv was telling me it was not authorized.

[2021-01-04-20-49\_chronograf\_data.csv.txt](https://community.influxdata.com/uploads/short-url/muF7EqSPWgfkauQafaTAe14xZv1.txt) (840 Bytes)

Thank you very much for your time and your help 🤗 🤗

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [January 4, 2021, 8:15pm UTC](https://community.influxdata.com/t/statechangesonly-does-not-work-as-expected/17350/10 "2021-01-04T20:15:19Z")

</div>

Hello @delapuentem,  
With the data you sent me, the output makes sense and stateChangesOnly() works.

```auto
import "csv"
import "influxdata/influxdb/monitor"
csvData = "#group,false,false,false,false,false,false,false,false,false,false
#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,string,string,string,double,double
#default,_result,,,,,,,,,
,result,table,_time,_start,_stop,_level,_measurement,host,threshold,value
,,0,2021-01-03T20:26:34Z,2021-01-03T19:48:56.599321105Z,2021-01-04T19:48:56.599321105Z,crit,alertas,host1,10,14
,,0,2021-01-03T20:26:49Z,2021-01-03T19:48:56.599321105Z,2021-01-04T19:48:56.599321105Z,crit,alertas,host1,10,12
,,0,2021-01-03T20:26:55Z,2021-01-03T19:48:56.599321105Z,2021-01-04T19:48:56.599321105Z,crit,alertas,host1,10,16
,,0,2021-01-03T20:26:44Z,2021-01-03T19:48:56.599321105Z,2021-01-04T19:48:56.599321105Z,ok,alertas,host1,10,2
,,0,2021-01-03T20:27:02Z,2021-01-03T19:48:56.599321105Z,2021-01-04T19:48:56.599321105Z,ok,alertas,host1,10,4
"
csv.from(csv: csvData)
|> monitor.stateChangesOnly()

```

It only returns the value where the state changes at `2021-01-03T20:26:44.000Z` because that’s when the state changes from “ok” to “crit”.

---

<div class="post-metadata">

**Author:** ![delapuentem](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/delapuentem/32/8616_2.png) [@delapuentem](https://community.influxdata.com/u/delapuentem)\
**Post date:** [January 4, 2021, 8:46pm UTC](https://community.influxdata.com/t/statechangesonly-does-not-work-as-expected/17350/11 "2021-01-04T20:46:22Z")

</div>

But if so, it should show “ok” at 21:27:02 GMT +1

Also, I have inserted a new value “crit”, and it keeps showing me “ok” from 21:26:44 GMT +1, and it should show “crit” right?

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/9/9de97f14e9d80ed0f7c6b26ee2aea078a4eab0f6.png)

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/1/1ae69b94192a42b604a430469954d6bdba8ad2bc.png)

And stateChanges(toLevel: “ok”) show me the “ok” row with the time 21:27:02 GMT +1  
but stateChanges(toLevel: “crit”) show me “No results”

---

<div class="post-metadata">

**Author:** ![fluxator](https://avatars.discourse-cdn.com/v4/letter/f/df705f/32.png) [@fluxator](https://community.influxdata.com/u/fluxator)\
**Post date:** [February 5, 2021, 9:19am UTC](https://community.influxdata.com/t/statechangesonly-does-not-work-as-expected/17350/12 "2021-02-05T09:19:27Z")

</div>

Hi @Anaisdg  
I also think there is some strange behaviours with the function “stateChangesOnly()” and “stateChanges()”.

**My UseCase:** I monitor VMs and I want to get the timestamp each time the VM was moved to another host (each the field “host” changed).  
So, if I understand the [documentation](https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/monitor/statechangesonly/) right, I can use the this function.

So with my data I want each row thats marked:

 ![GetImageAttachment](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/c/cea0adc5c8a25dbe30250fe54560a45e19449e75.png)

But I get “No Results” with following query:  
import “influxdata/influxdb/monitor”

```
from(bucket: "telegraf") 
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop) 
  |> filter(fn: (r) => r._measurement == "hyperv_vid" and 
    r._field == "Physical_Pages_Allocated" and 
    r.instance == "TestRHE01" 
  ) 
  |> group() 
  |> duplicate(as: "_level", column: "host") 
  |> monitor.stateChangesOnly()   

```

Can you help me, or is this a bug?

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [February 5, 2021, 4:53pm UTC](https://community.influxdata.com/t/statechangesonly-does-not-work-as-expected/17350/13 "2021-02-05T16:53:55Z")

</div>

Hello @fluxator,  
Hmm that is quite odd. Can you please export the data that you shared in a screen shot to annotated csv with the button in the UI near the submit button in the Data Explorer?

---

<div class="post-metadata">

**Author:** ![fluxator](https://avatars.discourse-cdn.com/v4/letter/f/df705f/32.png) [@fluxator](https://community.influxdata.com/u/fluxator)\
**Post date:** [February 8, 2021, 6:57am UTC](https://community.influxdata.com/t/statechangesonly-does-not-work-as-expected/17350/14 "2021-02-08T06:57:51Z")

</div>

Thanks for your reply, unfortunaltey I can’t upload my csv. That’s why I paste the content here:

```
#group,false,false,true,true,false,false,false,false,false,false,false,false 
#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,double,string,string,string,string,string,string 
#default,_result,,,,,,,,,,, 
,result,table,_start,_stop,_time,_value,_field,_measurement,host,instance,objectname,_level 
,,0,2021-02-05T07:43:00Z,2021-02-05T07:47:00Z,2021-02-05T07:43:00Z,1050624,Physical_Pages_Allocated,hyperv_vid,FPPW9406,TestRHE01,Hyper-V VM Vid Partition,FPPW9406 
,,0,2021-02-05T07:43:00Z,2021-02-05T07:47:00Z,2021-02-05T07:43:31Z,1050624,Physical_Pages_Allocated,hyperv_vid,FPPW9406,TestRHE01,Hyper-V VM Vid Partition,FPPW9406 
,,0,2021-02-05T07:43:00Z,2021-02-05T07:47:00Z,2021-02-05T07:45:01Z,1050624,Physical_Pages_Allocated,hyperv_vid,FPPW9406,TestRHE01,Hyper-V VM Vid Partition,FPPW9406 
,,0,2021-02-05T07:43:00Z,2021-02-05T07:47:00Z,2021-02-05T07:46:31Z,1050624,Physical_Pages_Allocated,hyperv_vid,FPPW9406,TestRHE01,Hyper-V VM Vid Partition,FPPW9406 
,,0,2021-02-05T07:43:00Z,2021-02-05T07:47:00Z,2021-02-05T07:44:01Z,1050624,Physical_Pages_Allocated,hyperv_vid,FPPW9407,TestRHE01,Hyper-V VM Vid Partition,FPPW9407 
,,0,2021-02-05T07:43:00Z,2021-02-05T07:47:00Z,2021-02-05T07:44:30Z,1050624,Physical_Pages_Allocated,hyperv_vid,FPPW9407,TestRHE01,Hyper-V VM Vid Partition,FPPW9407 
,,0,2021-02-05T07:43:00Z,2021-02-05T07:47:00Z,2021-02-05T07:45:31Z,1050624,Physical_Pages_Allocated,hyperv_vid,FPPW9407,TestRHE01,Hyper-V VM Vid Partition,FPPW9407 
,,0,2021-02-05T07:43:00Z,2021-02-05T07:47:00Z,2021-02-05T07:46:00Z,1050624,Physical_Pages_Allocated,hyperv_vid,FPPW9407,TestRHE01,Hyper-V VM Vid Partition,FPPW9407
```

---

<div class="post-metadata">

**Author:** ![fluxator](https://avatars.discourse-cdn.com/v4/letter/f/df705f/32.png) [@fluxator](https://community.influxdata.com/u/fluxator)\
**Post date:** [February 15, 2021, 9:47am UTC](https://community.influxdata.com/t/statechangesonly-does-not-work-as-expected/17350/15 "2021-02-15T09:47:03Z")

</div>

@Anaisdg are you able to reproduce the issue?  
Maybe my CSV is not good to reproduce the issue because of the field “\_level”.

Here is a new CSV with the same data but without the “\_level”:

> #group,false,false,true,true,false,false,false,false,false,false,false  
> #datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,double,string,string,string,string,string  
> #default,\_result,  
> ,result,table,\_start,\_stop,\_time,\_value,\_field,\_measurement,host,instance,objectname  
> ,0,2021-02-05T07:43:00Z,2021-02-05T07:47:00Z,2021-02-05T07:43:00Z,1050624,Physical\_Pages\_Allocated,hyperv\_vid,FPPW9406,TestRHE01,Hyper-V VM Vid Partition  
> ,0,2021-02-05T07:43:00Z,2021-02-05T07:47:00Z,2021-02-05T07:43:31Z,1050624,Physical\_Pages\_Allocated,hyperv\_vid,FPPW9406,TestRHE01,Hyper-V VM Vid Partition  
> ,0,2021-02-05T07:43:00Z,2021-02-05T07:47:00Z,2021-02-05T07:45:01Z,1050624,Physical\_Pages\_Allocated,hyperv\_vid,FPPW9406,TestRHE01,Hyper-V VM Vid Partition  
> ,0,2021-02-05T07:43:00Z,2021-02-05T07:47:00Z,2021-02-05T07:46:31Z,1050624,Physical\_Pages\_Allocated,hyperv\_vid,FPPW9406,TestRHE01,Hyper-V VM Vid Partition  
> ,0,2021-02-05T07:43:00Z,2021-02-05T07:47:00Z,2021-02-05T07:44:01Z,1050624,Physical\_Pages\_Allocated,hyperv\_vid,FPPW9407,TestRHE01,Hyper-V VM Vid Partition  
> ,0,2021-02-05T07:43:00Z,2021-02-05T07:47:00Z,2021-02-05T07:44:30Z,1050624,Physical\_Pages\_Allocated,hyperv\_vid,FPPW9407,TestRHE01,Hyper-V VM Vid Partition  
> ,0,2021-02-05T07:43:00Z,2021-02-05T07:47:00Z,2021-02-05T07:45:31Z,1050624,Physical\_Pages\_Allocated,hyperv\_vid,FPPW9407,TestRHE01,Hyper-V VM Vid Partition  
> ,0,2021-02-05T07:43:00Z,2021-02-05T07:47:00Z,2021-02-05T07:46:00Z,1050624,Physical\_Pages\_Allocated,hyperv\_vid,FPPW9407,TestRHE01,Hyper-V VM Vid Partition

---

<div class="post-metadata">

**Author:** ![fluxator](https://avatars.discourse-cdn.com/v4/letter/f/df705f/32.png) [@fluxator](https://community.influxdata.com/u/fluxator)\
**Post date:** [February 25, 2021, 7:23am UTC](https://community.influxdata.com/t/statechangesonly-does-not-work-as-expected/17350/16 "2021-02-25T07:23:25Z")

</div>

@Anaisdg any news on this?

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [February 25, 2021, 4:40pm UTC](https://community.influxdata.com/t/statechangesonly-does-not-work-as-expected/17350/17 "2021-02-25T16:40:30Z")

</div>

Hello @fluxator,  
I’m sorry for the delay. I am based in Texas, and didn’t have power for 10 days.  
It looks like a bug to me, sorry to say. Can you please submit an issue

> **[GitHub - influxdata/flux: Flux is a lightweight scripting language for...](https://github.com/influxdata/flux)**
>
> Flux is a lightweight scripting language for querying databases (like InfluxDB) and working with data. It's part of InfluxDB 1.7 and 2.0, but can be run independently of those. - GitHub - influ...

Thank you.

---

<div class="post-metadata">

**Author:** ![delapuentem](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/delapuentem/32/8616_2.png) [@delapuentem](https://community.influxdata.com/u/delapuentem)\
**Post date:** [October 26, 2021, 3:05pm UTC](https://community.influxdata.com/t/statechangesonly-does-not-work-as-expected/17350/18 "2021-10-26T15:05:48Z")

</div>

For those who want to follow this topic, I have recently returned to this without results and I have opened a thread where @Anaisdg indicates.

[stateChangesOnly() does not work as expected · Issue #4179 · influxdata/flux (github.com)](https://github.com/influxdata/flux/issues/4179)

Thanks.

---

<div class="post-metadata">

**Author:** ![delapuentem](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/delapuentem/32/8616_2.png) [@delapuentem](https://community.influxdata.com/u/delapuentem)\
**Post date:** [October 29, 2021, 6:53am UTC](https://community.influxdata.com/t/statechangesonly-does-not-work-as-expected/17350/19 "2021-10-29T06:53:42Z")

</div>

After opening an issue on Github, I found the solution thanks to @wolffcm in [this post](https://github.com/influxdata/flux/issues/4179). And it is adding `|> sort (columns: ["_time"])` to the query.

It would look like this:

```auto
import "influxdata/influxdb/monitor"

from(bucket: "monitoring")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
    |> filter(fn: (r) =>
        (r._measurement == "http_response" and r._field == "result_code"))
    |> filter(fn: (r) =>
        (r["server"] == "https://www.mywebpage.org"))
    |> keep(columns: ["_time", "_value", "_field", "server"])
    |> group(columns: ["server"])
    |> map(fn: (r) => ({ r with
    _level:
      if r._value > 0 then "crit" else "ok" }))
    |> sort(columns: ["_time"])      
    |> monitor.stateChangesOnly()

```

And so it returns only the last change change of each state like the documentation.

Many thanks also to @Anaisdg for the help! 🥳🥳😄😄

---

<div class="post-metadata">

**Author:** ![fluxator](https://avatars.discourse-cdn.com/v4/letter/f/df705f/32.png) [@fluxator](https://community.influxdata.com/u/fluxator)\
**Post date:** [November 24, 2021, 1:13pm UTC](https://community.influxdata.com/t/statechangesonly-does-not-work-as-expected/17350/20 "2021-11-24T13:13:19Z")

</div>

Unfortunately this does not work for my usecase ☹ But thanks anyway 🙂

[Next page](https://community.influxdata.com/t/statechangesonly-does-not-work-as-expected/17350.md?page=2)
