Multiply one variable by another

Hello,

First I try to multiply the hourly electricity rate with the consumption.

At the changeover node-red changes the rate by changing the data

So, for example, at 8:00 a.m. I’ll have the changeover to a type 2 schedule that won’t change for several hours.
This is the value I’d like to multiply all my consumption data with.
the chronological reference is potentially problematic and I hope it can be resolved

But for the moment I can’t.

In a second phase, I’d like to add up these values according to a window (hour, day, month), maybe do some cumulation, etc.

value1 = from(bucket: "TEST")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "Tarif")
  |> filter(fn: (r) => r["_field"] == "value")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> yield(name: "value1")

value2 =from(bucket: "GIN")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "Calcul")
  |> filter(fn: (r) => r["_field"] == "Clim")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> yield(name: "value2")

// Effectuer une jointure sur le temps
combined = join(
  tables: {value1: value1, value2: value2},
  on: ["_time"]
)

// Multiplier les values correspondantes

result = combined
  |> map(fn: (r) => ({
    _time: r._time,
    _value: r._value_value1_mean * r._value_value2_mean  
  }))

result |> yield(name: "Result")

tablevaluer2 _measurementgroupstring _fieldgroupstring _valueno groupdouble _timeno groupdateTime:RFC3339
0 Calcul Clim 1 2024-06-18T13:31:00.000Z
0 Calcul Clim 1 2024-06-18T14:01:00.000Z
0 Calcul Clim 1 2024-06-18T14:31:00.000Z
0 Calcul Clim 1 2024-06-18T14:51:00.000Z
tablevalue1 _measurementgroupstring _fieldgroupstring _valueno groupdouble _timeno groupdateTime:RFC3339
0 Tarif value 0.18 2024-06-18T08:00:00.000Z

Thanks for the help.

@Yohan This one is going to be a bit tricky. I think you’re going to have to define a function that, based on a time value, looks up the tarif to apply. Joining on time won’t work here because you the timestamps in your data don’t match the timestamp in your tarif data.

How consistent is the tarif data? Does it follow a set schedule every day? Does the tarif amount change each day? For example from 8am - 8pm on Monday, the tarif is .25, but on Tuesdays it’s .30?

@scott
There’s a difference between weekends and weekdays, and there’s also a difference in working hours during the week. Nighttime is not the same as daytime, for example.

edit :
Can we create If functions in the Flux script?

For example, this is what I want to do with occupancy. If present then taken into account, otherwise not.

The same goes for rates. If it’s an off-peak rate, then we calculate the cost, otherwise not.

data1 = from(bucket: "your_bucket")
  |> range(start: -1h)
  |> filter(fn: (r) => r._measurement == "first_measurement" and r._field == "your_variable_field")

data2 = from(bucket: "your_bucket")
  |> range(start: -1h)
  |> filter(fn: (r) => r._measurement == "second_measurement" and r._field == "value_field")

joinedData = join(
  tables: {d1: data1, d2: data2},
  on: ["_time"],
  method: "inner"
)

filteredData = joinedData
  |> filter(fn: (r) => r._value_d1 == 1)

result = filteredData
  |> map(fn: (r) => ({_time: r._time, _value: r._value_d2}))

result

I found I could use tags to differentiate rates. Without retro action of course.

Now I need to find out how I can multiply the consumption by the cost according to the tariff. And this according to the updated tariff.

Because I don’t want to multiply by last() every time, otherwise my old calculations will be based on the new rate.

@Yohan So with the addition of the tag, what does your schema look like?

My schema looks like this:

Bucket → Category (Energy, Temperatures, etc) → Load concerned/Location → Tags (Like “Tarif” (in this tag you can find off-peak time, peak time), or “Occupancy” (in this one : Presence, Absence).

Is this your question?

I just mean in the measurement you’re querying, what tags and fields exist?