Get cumulativesum value from API

Hey everyone,

I’m new in use of influx. I’ve been working on a way to get the cumulativesum of a sensor.

I’ve successfully managed to get a cell with this value but I want to send it to another self-made Web interface. Perhaps is it possible thru a task but I haven’t seen how to do it.

So I search to get this value from API. I’ve successfully achieve a get request to this cell but the value isn’t store in result.

Is it a way to do it?

@GhostAlex67 Does your self-made web interface have an HTTP API? If it does, you can extract the value as a scalar value and send it to your API:

import "http"

data = from(...)
    |> range(...)
    |> filter(...)
    |> sum()

total = (data |> findColumn(fn: (key) => true, column: "_value"))[0]

http.post(
    url: "http://your-device-api-url.com",
    data: bytes(v: total),
)

This doesn’t necessarily have to be run as a task, unless you want it to run automatically on regular intervals.

Hey,

Yes my Web app has an API, and yes also I have to use a fresh value regularly.

This can be done exactly the same way in task?

Thks for your fast answer!

@GhostAlex67 Yep, this can be done as a task. A task is just a scheduled Flux query, so anything you can do in a query, you can do as part of a task.

Hy, I tried to use your snap.
Here is my modified one

import "http"
data = from(bucket: "test")
  |> range(start: 1617365977, stop: now())
  |> filter(fn: (r) => r["_measurement"] == "Heat")
  |> filter(fn: (r) => r["_field"] == "getConsum")
  |> cumulativeSum(columns: ["_value"])
  |> toInt()
  total = (data |> findColumn(fn: (key) => true, column: "_value"))[0]
  http.post(
    url: "http://xxxxxxxxxxxxxx/json.htm?type=command&value=",
    data: total,
)

I’ve added an int conversion because my API cannot work with float. but when I submit it I got :

An internal error has occurred - check server logs

Like I said in the begining, I’m new in Influx usage so I don’t know were find this logs… But peharps something is wrong with my query.

@GhostAlex67 With http.post() the value of data parameter needs to be a bytes value. Use bytes() to convert it.

Something else I also note about your query is your use of cumulativeSum(). As is, this function isn’t going to get you the value you want. This function increments each value based on the previous row value and preserves all the rows. So the row with the value you actually want is the last row in the table. So you’d have to use last() to just return that row. I’d suggest using sum() instead of cumulativeSum() since you don’t care about the sum over time; you just care about the final sum:

import "http"

data = from(bucket: "test")
    |> range(start: 1617365977, stop: now())
    |> filter(fn: (r) => r["_measurement"] == "Heat")
    |> filter(fn: (r) => r["_field"] == "getConsum")
    |> sum()
    |> toInt()

total = (data |> findColumn(fn: (key) => true, column: "_value"))[0]

http.post(
    url: "http://xxxxxxxxxxxxxx/json.htm?type=command&value=",
    data: bytes(v: total),
)

Ok, thanks for your advise.
I implement with your suggestion but I’ve got an other message :

error calling function "bytes" @11:11-11:26: cannot convert int to bytes

Oh right, you can only convert strings to bytes. Try this:

import "http"

data = from(bucket: "test")
    |> range(start: 1617365977, stop: now())
    |> filter(fn: (r) => r["_measurement"] == "Heat")
    |> filter(fn: (r) => r["_field"] == "getConsum")
    |> sum()
    |> map(fn: () => ({_value: string(v: int(v: r._value))}))

total = (data |> findColumn(fn: (key) => true, column: "_value"))[0]

http.post(
    url: "http://xxxxxxxxxxxxxx/json.htm?type=command&value=",
    data: bytes(v: total),
)

Hy still had error…

error @7:47-7:48: undefined identifier r error @7:6-7:60: missing required argument r (argument fn)

But I work around with an http.get way:

import "experimental/http"
data = from(bucket: "test")
  |> range(start: 1617365977, stop: now())
  |> filter(fn: (r) => r["_measurement"] == "Heat")
  |> filter(fn: (r) => r["_field"] == "getConsum")
  |> sum() 
  |> toInt()
  |> yield(name: "total")
total = (data |> findColumn(fn: (key) => true, column: "_value"))[0]
response = http.get(url:"http://xxxxxxxxxxxxxxxxxxxxx/json.htm?type=commandsvalue="+string(v: total))