I want to measuere how long we are using our smartphones during the day,
so eveytime the smartphone is active a ‘1’ is sent to the influx database, and when the phone is not active anymore, a value ‘0’ is sent., so i want the sum of all the times (in seconds) during a certain timeperiod (1 day) t that the value is ‘1’: for example
10:15:05 → 1
10:16:05 → 0
10:30:30 → 1
10:30:45 → 0
Next, add a filter statement after the elapsed function to filter out only those whose _value = 0. This is because the “active” time (when the state 1 goes to state 0) is captured in those records where _value = 0. If you wanted to know the time when the phone is NOT active, you would set the filter where _value = 1.
Thank you a lot for this!
This works great! This is what I wanted.
If I want to see for each day how much the smartphone was active I guess I have to use the:
'aggregateWindow" function?
let’s say I have the range set to ‘last week’, and i want to see for every day what the useage of the phone was, i will need to use this i think?
|> aggregateWindow(every: 1d , fn:last, createEmpty: false)
but this does not seems to work. I am not sure where to put this function.
thank you a lot for this!
I thought of it when i was in bed to do it this way! But great to hear I was thinking the correct way to do it.
I sitll have a follow up queston… (i allready tried several things but I am not able to do it…)
now i have for each day the total seconds a phone was used.
but if i want to convert this to ‘hours’…
i was thinking about using this function:
but the r.elapsed value is not an integer/float so it’s not possible to do this kind of operation with it…
i also tried using the int() or float() fuction for the r.elapsed… but this does not work
Hi @Thomas_Decock
I think this will work. There may be a more elegant way to just have one single column called elapsed which has the float value, but I created a second column called elapsedFloat and then divided by 3600.0 to get the decimal hours.
Hello, this exact solution is what I have also been searching for, however I am using influxdb cloud serverless, so querying with flux is not available, is there an SQL solution?
Wow!
Thanks for your educational help here.
I’ve learned a lot.
I have a question though.
When the state is 1 currently it seems that this query would not capture the active time. (the current chunk that did not go to 0 yet).
How can I add that to the result?
Many thanks!
It has been a while since I crafted that query, so I no longer have the test data lying around, but I am sure there is a way to capture the current state (up to the time that the query is run) when that value is “1”. What happens if you modify the aggregateWindow function to include timeSrc: "start" ?