Flux query error => data type problems

Hi I’m trying to get a value recalculated but it fails due to type differences.
My inserted data is of the type int and I want to become a float by dividing the sum of the integers.

import "date"
day = date.truncate(t: now(), unit: 1d)

from(bucket: "electricity")
  |> range(start: day)
  |> filter(fn: (r) => 
      r["_measurement"] == "generated" and
      r["_field"] == "AC power" and
      r._value > 0
  )
  |> aggregateWindow(every: 1d, fn: sum, createEmpty: false)
  |> map(fn: (r) => ({ r with _value: r._value / 3600 }))
  |> toFloat()
  |> map(fn: (r) => ({ r with _value: r._value /1000 *0.3 }))

When I try to run this query I get the type error:
type error @14:55-14:58: expected int but found float

edit:
If I change the last line to
|> map(fn: (r) => ({ r with _value: r._value /10000 * 3 }))
I get another error
type error @14:6-14:62: expected int but found float (argument tables)

Thx
Geoffrey

I found the solution. The data type is expecting the constants to be in the same type so the following is my query:

import "date"
day = date.truncate(t: now(), unit: 1d)

from(bucket: "electricity")
  |> range(start: day)
  |> filter(fn: (r) => 
      r["_measurement"] == "generated" and
      r["_field"] == "AC power" and
      r._value > 0
  )
  |> aggregateWindow(every: 1d, fn: sum, createEmpty: false)
  |> toFloat()
  |> map(fn: (r) => ({ r with _value: r._value / 3600.0 / 1000.0 * 0.3 }))

As you can see I converted the constant integers (3600 and 1000) to floats by using a decimal place.
I’m confused as why it doesn’t do that by default tho.

1 Like