02-11-2022 16:08
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

02-11-2022 16:08
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
Hi all! My JS experience is close to zero and I need help for a small app:
My goal is to ping the user with a vibration every X-minutes. If they don't respond for 3 pings then they are asleep. If they want pings to stop, they can just use the button.
Now, when pings stop, I want to start observing the user's sleep (awake or asleep) and I would like to record the timestamps and the sleep.state. What is the easiest way to save the sleep.state observation?
here is my draft:
// console.log("App code started");
import { vibration } from "haptics";
import * as document from "document";
import { HeartRateSensor } from "heart-rate";
import sleep from "sleep";
const hrm = new HeartRateSensor();
var secInterval = 5;
var noResponse = 0;
const myButton = document.getElementById("button");
var stop = false;
if (sleep) {
sleep.addEventListener("change", () => {
console.log(`User sleep state is: ${sleep.state}`);
});
} else {
console.warn("Sleep API not supported on this device, or no permission")
}
// tried clock.ontick but it won't work when screen is off
var pingInterval = setInterval(pingUser, secInterval*1000);
function pingUser(){
myButton.addEventListener("click", (evt) => {
stop = true;
})
if (stop || noResponse>=3){
vibration.stop();
console.log(sleep.state);
// >>>> save sleep state observation
}
else {
noResponse = noResponse + 1;
vibration.start("nudge");
}
}
Answered! Go to the Best Answer.
Accepted Solutions
02-12-2022 01:36
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


02-12-2022 01:36
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Hi @ac_devel - several options spring to mind depending on how you want to view the information;
- write to a file in the watch space, [for pre Versa 2, memory space may become more of an issue],
- write a file for each observation,
- use a companion and save in preferences [there may be limits to the number of entries].
Author | ch, passion for improvement.

02-12-2022 01:36
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


02-12-2022 01:36
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Hi @ac_devel - several options spring to mind depending on how you want to view the information;
- write to a file in the watch space, [for pre Versa 2, memory space may become more of an issue],
- write a file for each observation,
- use a companion and save in preferences [there may be limits to the number of entries].
Author | ch, passion for improvement.

02-15-2022 14:23 - edited 02-15-2022 17:07
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

02-15-2022 14:23 - edited 02-15-2022 17:07
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
I ended up using companion to see the messages but in the future I'll probably use a API communication to send the observation to a server.
