Take a look at this syntax
select integral(field,1s)
Or here’s how I do this (in Python):
q=“select mean(rpm) from compressor where mac =’{}’ and time > ‘{}’ and time < ‘{}’ group by time(2m)”.format(asset_id,ifx_tk,ifx_et)
l=list(iclient.query(q).get_points())
if len(l) > 0:
print(“Hours: {}”.format(hours))
adder = sum(1 for i in l if i[‘mean’] is not None)
hours = float(adder)/30.0 + hours
print("-Hours: {} +{} minutes".format(hours,adder))
else:
pass
And let me explain. By selecting mean(rpm) I can specify time(), in my case 2 minutes. But you can choose any interval you like. The more intervals you choose, the more accurate your result, but at the expense of more memory/compute time.
So if you just store the value when it changes (0 for Off, 1 for On).
select mean(pump_state) from your measurement where id = ‘pump1’ group by time (1s)
This will give you a result set with values every 1 second. Then you can count up the values in your result set that are non-zero and that is the number of seconds that the pump was on in the time period. Of course it is an approximation because the resolution is only to 1 second. So you might miss just a bit less than a second at each end of the interval.
Note that if you are examining quite large intervals of time, the number of values with 1-second resolution is quite large. I have that situation from time to time. So I put the above in a loop and do it in one-hour chunks each time (and I am using 2-minute interval). Then I advance the start time and the end time by one hour (up to the end of the interval).