Migrating to flux math question

Hello,
im currently trying to migrate to flux thanks to the query builder someone so familiar with flux can manage and i got everything working except 2 queries that use math to change the value displayed.

This is the query in question.
I just want to calculate the value *-1

 from(bucket: "telegraf")
    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
     |> filter(fn: (r) => r["_field"] == "value")
     |> filter(fn: (r) => r["topic"] == "openWB/pv/1/W")
     |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
     |> yield(name: "mean")

My old query would be this

SELECT last("value")  *-1 FROM "mqtt_consumer" WHERE ("topic" = 'openWB/pv/W') AND $timeFilter GROUP BY time($__interval) fill(null)

i just could use math ( *-1) oder math ( *1000) but i have a hard time to figure out how to do this in flux.
From what i read i have to use map but i don’t know exactly how.

i have tried adding

|> map(fn: (r) => r._value * -1))

but that fails

hallo, be sure to combine same types

if i multiply my double 7.99 * -1 it returns

" runtime error @45:4-47:4: map: type conflict: int != float"

so changing -1 to -1.0 works ok

|> map(fn: (r) => ({negative : r._value_d1 * -1.0}))

pavel

Thanks worked changing *-1 to *1.0 i never would have thought of this

1 Like