Please explain If statments in Flux to me

I have been trying to figure out If Statments in FluxLang for the last 2 days and I have gotten nowhere.

I know I must be messing up the syntax for Flux but I also want to know if I am even in the correct train of thought and if Flux can do this.

I would like to get the first and last data point of today, store it in a variable and then compare them together.

This is what I have tried :

firstVol = from(bucket: "HomeTank")
  |> range(start: today())
  |> filter(fn: (r) => r["_measurement"] == "Tank1")
  |> filter(fn: (r) => r["_field"] == "Volume")
  |> first()
  |> map(fn: (r) => ({ _value : r._value}))

lastVol = from(bucket: "HomeTank")
  |> range(start: today())
  |> filter(fn: (r) => r["_measurement"] == "Tank1")
  |> filter(fn: (r) => r["_field"] == "Volume")
  |> last()
  |> map(fn: (r) => ({ _value : r._value}))

I think this gives me variables that I can then use to compare with each other like this :

if firstVol < lastVol + 500 then "Tank was filled up"
else "Nothing filled up"

From what I understand mapping the variables gives me a value without the time stamp so I should be able to compair them ?
Or am I thinking too closely to Python or other programming languages for this usage ?

Any pointers would be of great help, this is a far way off from what I am used to in my projects.

Hello @Duck999,
You’re so close! This is a common misconception with Flux. Even when applying bare selectors to your data like first() or last() you still return a table stream not a single value.
To return a single value, use the findRecord function like so:

data = from(bucket: "HomeTank")
  |> range(start: today())
  |> filter(fn: (r) => r["_measurement"] == "Tank1")
  |> filter(fn: (r) => r["_field"] == "Volume")

firstVol = data
  |> first()
  |> findRecord(
  fn: (key) => true),
  idx: 0
)
lastVol = data
  |> last()
  |> findRecord(
  fn: (key) => true),
  idx: 0
)

if firstVol._value < lastVol._value + 500 then "Tank was filled up"
else "Nothing filled up"

Please let me know if that does the trick.

Hi first, thank for your time, this has been a bit of a learning curve for me.

I tried the code you gave me and while it makes sense to me I get an error :

invalid: error in query specification while starting program: this Flux script returns no streaming data. Consider adding a "yield" or invoking streaming functions directly, without performing an assignment

I am using this in Grafana if that makes a difference ? ( V8.0.5 )

I tried using a yield statement like this

if firstVol._value < lastVol._value + 500.0 then output = "Tank was filled up"
else output = "Nothing filled up"

yield(name:output)

But the If statement did not like this at all.

invalid: compilation failed: error at @20:6-20:9: expected ELSE, got ASSIGN (=) at 19:54

Also please let me know if there was maybe a bit of a typo in your code because I had to change it a little for it to be happy.
This

firstVol = data
  |> first()
  |> findRecord(
  fn: (key) => true),
  idx: 0
)

To this :

firstVol = data
  |> first()
  |> findRecord(
  fn: (key) => true,
  idx: 0
)

Just an extra close bracket on the "fn: (key) " line, that I am sure could have slipped in by mistake but I want to make sure I am not messing anything up.

Lastly do you know of a basics lesson or Tutorial in flux that I can go over ?
Like baby steps so I can understand exactly how things fit together, I tried finding it online but I did not have much luck, hoping you know of a better “starting out” resource.
Preferably one that allows me to code in the Grafana environment so I can use it in my Dashboards.

Ok I figured out how I was using yield wrong…

It should be used like this :

calc |> yield()

But that is with a different method of doing this thing.

I am however still failing hard in getting things to work, I have read the Influx Docs and find they dont work correctly in Grafana for some reason.

Also as for that typo error I thought, it does say exactly that in the docs, but then in the examples it corrects it.
Not sure it that needs to be changed in the documents ?

Thanks again for your time.