Hi @larsxschneider
Can you inspect your data in Influx and/or post here as a .csv? I threw together some sample data and mine looks like this. You will see I am recording the SunState and the energy consumed value at the same time.
_time SunState energy_consumed
2023-05-07T11:11:35.167Z 1 662
2023-05-07T11:12:27.703Z 1 664
2023-05-07T11:13:08.953Z 1 665
2023-05-07T11:14:11.474Z 1 666
2023-05-07T11:14:55.25Z 0 667
2023-05-07T11:16:23.786Z 0 668
2023-05-07T11:17:57.03Z 0 669
2023-05-07T11:19:02.621Z 1 670
2023-05-07T11:19:50.631Z 1 671
2023-05-07T11:20:21.991Z 1 672
2023-05-07T11:21:06.324Z 1 673
2023-05-07T11:23:15.726Z 1 674
2023-05-07T11:24:03.697Z 0 675
2023-05-07T11:25:02.512Z 0 676
2023-05-07T11:25:36.968Z 0 677
2023-05-07T11:26:41.276Z 0 678
2023-05-07T11:28:11.162Z 1 679
Flux query to see everything looks like this:
from(bucket: "RetroEncabulator")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "kWh")
|> filter(fn: (r) => r["_field"] == "SunState" or r["_field"] == "energy_consumed")
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value") // need to pivot data so that energy_consumed and SunState are in the same row
|> yield(name: "consumption_at_night")
gives this:
If one wants to know the consumption when SunState = 0, add this filter:
|> filter(fn: (r) => r["SunState"] == 0)
which gives this:
And then to query the power consumption when the sun is below the horizon (i.e. when SunState = 0), add a difference function (since the values of the power meter are always increasing, we need to know the difference between subsequent values), the same filter function as above, and then a sum() function to get the total energy consumed:
|> difference(nonNegative: false, columns: ["energy_consumed"])
|> filter(fn: (r) => r["SunState"] == 0)
|> sum(column: "energy_consumed")
which gives this:
If you wanted to aggregate by day, you could use an aggregateWindow function like this:
|> aggregateWindow(every: 1d, fn: sum, column:"energy_consumed")
PS: I am not a regular user of InfluxQL, so if that is how you plan to approach this, you might find this article helpful.