Hello
I have been googling up and down but can’t seem to find an answer for my problem so hoping someone here can help me.
I use Node-Red and InfluxDB 2.2, Node-Red send kWh and price current hour to InfluxDB.
I’m trying to calculate total kWh used every hour and how much the price is but can’t get it right.
This is the code I have and it output some numbers but they are not quite right… Any help is appreciated
import "date"
today = date.truncate(t: now(), unit: 1d)
from(bucket: "nodered")
|> range(start: today, stop: now())
|> filter(fn: (r) => r["_measurement"] == "tesla")
|> filter(fn: (r) => r["_field"] == "kWh" or r["_field"] == "price")
|> aggregateWindow(every: 1h, fn: last, createEmpty : false, timeSrc:"_time")
|> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
|> map(fn: (r) => ({r with _value: r.kWh * r.price}))
My table look like this
Tried to upload the .csv file but as a new user I can’t do that yet.
0 tesla kWh 0.0485 2022-05-04T18:54:09.902Z 2022-05-05T18:54:09.902Z 2022-05-05T18:12:00.000Z
0 tesla kWh 0.3899 2022-05-04T18:54:09.902Z 2022-05-05T18:54:09.902Z 2022-05-05T18:16:00.000Z
0 tesla kWh 0.0872 2022-05-04T18:54:09.902Z 2022-05-05T18:54:09.902Z 2022-05-05T18:20:00.000Z
0 tesla kWh 0.2992 2022-05-04T18:54:09.902Z 2022-05-05T18:54:09.902Z 2022-05-05T18:24:00.000Z
0 tesla kWh 0.7088 2022-05-04T18:54:09.902Z 2022-05-05T18:54:09.902Z 2022-05-05T18:32:00.000Z
0 tesla kWh 0.961 2022-05-04T18:54:09.902Z 2022-05-05T18:54:09.902Z 2022-05-05T18:36:00.000Z
0 tesla kWh 1.0401 2022-05-04T18:54:09.902Z 2022-05-05T18:54:09.902Z 2022-05-05T18:40:00.000Z
0 tesla kWh 1.3587 2022-05-04T18:54:09.902Z 2022-05-05T18:54:09.902Z 2022-05-05T18:44:00.000Z
0 tesla kWh 1.5698 2022-05-04T18:54:09.902Z 2022-05-05T18:54:09.902Z 2022-05-05T18:48:00.000Z
0 tesla kWh 1.7029 2022-05-04T18:54:09.902Z 2022-05-05T18:54:09.902Z 2022-05-05T18:52:00.000Z
0 tesla kWh 1.9271 2022-05-04T18:54:09.902Z 2022-05-05T18:54:09.902Z 2022-05-05T18:54:09.902Z
1 tesla price 2.9826 2022-05-04T18:54:09.902Z 2022-05-05T18:54:09.902Z 2022-05-05T18:12:00.000Z
1 tesla price 2.9826 2022-05-04T18:54:09.902Z 2022-05-05T18:54:09.902Z 2022-05-05T18:16:00.000Z
1 tesla price 2.9826 2022-05-04T18:54:09.902Z 2022-05-05T18:54:09.902Z 2022-05-05T18:20:00.000Z
1 tesla price 2.9826 2022-05-04T18:54:09.902Z 2022-05-05T18:54:09.902Z 2022-05-05T18:24:00.000Z
1 tesla price 2.9826 2022-05-04T18:54:09.902Z 2022-05-05T18:54:09.902Z 2022-05-05T18:32:00.000Z
1 tesla price 2.9826 2022-05-04T18:54:09.902Z 2022-05-05T18:54:09.902Z 2022-05-05T18:36:00.000Z
1 tesla price 2.9826 2022-05-04T18:54:09.902Z 2022-05-05T18:54:09.902Z 2022-05-05T18:40:00.000Z
1 tesla price 2.9826 2022-05-04T18:54:09.902Z 2022-05-05T18:54:09.902Z 2022-05-05T18:44:00.000Z
1 tesla price 2.9826 2022-05-04T18:54:09.902Z 2022-05-05T18:54:09.902Z 2022-05-05T18:48:00.000Z