How do I make an INLINE dataview query to calculate duration between go-to-sleep-time the day before and the wake-up-time on a day?

What I’m trying to do

I have the following fields on the daily notes

I want to create an INLINE DATAVIEW QUERY to calculate the sleep duration i.e. the duration between today’s wake-up-time and yesterday’s go-to-sleep-time for a day.

Things I have tried

I have tried the following formula in the query…
sleep-duration:: = (date(dateformat(, "yyyy-MM-dd") + "T" + this.wake-up-time)) - (date(dateformat(, "yyyy-MM-dd") + "T" + go-to-sleep)- dur("1 day"))

but it gives error as follows

I am really new to the dataview and can’t understand the syntax (or sometimes logic).

Please help.

I tried to search for sleep tracking in the forum but couldn’t find results for an inline query.

I wonder if the dashes in the variable names are getting confused for negatives.
can you test with go_to_sleep and wake_up_time instead and see if it works?

I need to think a little bit on how to do such a query, but I do see some issues with your current query:

  • The point that @cheezopath makes regarding dashes/minus sign is a valid one (at least if this was a dataviewjs inline query, which it doesn’t seem to be). But please do test that out
  • In the second part of the query you’re using which I’m not sure has a meaning in this context, so who knows which date you end subtracting the one day from
  • And the part of go-to-sleep if it is/was legal it’s not given that it would refer to yesterdays value of that property
  • Lastly, when doing something like fieldName:: `= query ` you’re setting the fieldName to be the query itself, not the result of the query. This means that if you refer to this field from another note, it will re-execute the query in the contect of the other note, and not use the result of the first run within the note itself.

To make it work you’ll most likely need for the second part to do the date calculation first, use that to form a link, and then retrieve the go-to-sleep from that linked file. This would still not solve the issue of the sleep-duration holding a query, and not the result. That could possibly be countered by doing some magic using the meta-bind plugin, or calling processFrontmatter from the Obsidian API, but it makes your code a lot longer.

I think a better solution would be to include both the timestamp in the current field. Aka have went_to_sleep (where the yesterday is implied) and woke_up_time (from current day). And I would include the full date-time specification directly. Having both dates in the same field, would mean it would be a lot easier to calculate the sleep duration whenever you want to, either in the current note or in another query do multiple dates as you wouldn’t need to persist the result.

The only disadvantage of the latter solution, is that you still wouldn’t be able to simply do graphs of your sleep duration unless some other trickery is involved allowing for this duration to be calculated.

One advantage of this solution though, is that would be a lot easier to add went_to_sleep times after midnight as you then could simply use “todays” date, and not yesterdays date.

Does this make any sense to you?

Coming back to this query, which the OP doesn’t seem to have done since we’ve not seen any response :smiley: , I see there are some typos in the intial query as well.

So assuming the following:

  • The variables are wake-up-time and go-to-sleep-time
  • And that the go-to-sleep-time always is before midnight the previous day, but noted in todays file
  • That the file either is named with a date, or that that date property is set, so that actually has a value

Then the following query should return the amount of sleep:

`= (date(dateformat(, "yyyy-MM-dd") + "T" + this.wake-up-time)) - (date(dateformat(, "yyyy-MM-dd") + "T" + this.go-to-sleep-time) - dur("1 day")) `

Added some missing this. in the latter part, and -time for the go to sleep variant.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.