Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Versa 2 face not updating steps in real time

I've managed to put together a watch face (showing date, time, battery, heart rate, and steps) that works except for one thing - it won't update the steps unless I push the left button and swipe up to 'today'. Everything else updates without swiping up. I copied all this code from other threads and I'm really happy to get this far. Does anyone know what I'm missing? I can't tell what's different in the code for the other stats that lets them update.

 

import clock from "clock";
import document from "document";
import { preferences } from "user-settings";
import * as util from "../common/utils";

// Update the clock every minute
clock.granularity = "minutes";

// Get a handle on the <text> element
const myLabel = document.getElementById("myLabel");
const myMonth = document.getElementById("myMonth");
const myDay = document.getElementById("myDay");

// Update the <text> element every tick with the current time
clock.ontick = (evt) => {
let today = evt.date;
let hours = today.getHours();
let today = evt.date;
let hours = today.getHours();
let monthnum = today.getMonth();
let day = today.getDate();
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
let monthname = month[monthnum];
if (preferences.clockDisplay === "12h") {
// 12h format
hours = hours % 12 || 12;
} else {
// 24h format
hours = util.zeroPad(hours);
}
let mins = util.zeroPad(today.getMinutes());
myLabel.text = `${hours}:${mins}`;
myMonth.text = `${monthname}`;
myDay.text = `${day}`;
}

const myHRM = document.getElementById("myHRM");
import { HeartRateSensor } from "heart-rate";

if (HeartRateSensor) {
const hrm = new HeartRateSensor({ frequency: 1 });
hrm.addEventListener("reading", () => {

myHRM.text=`${hrm.heartRate}`;
});

hrm.start();
}

import { BodyPresenceSensor } from "body-presence";

if (BodyPresenceSensor) {
const body = new BodyPresenceSensor();
body.addEventListener("reading", () => {
if (!body.present) {
myHRM.text="--"
} else {

}
});
body.start();
}
const mySteps = document.getElementById("mySteps");
const myCalories = document.getElementById("myCalories");
import { me as appbit } from "appbit";
import { today } from "user-activity";

if (appbit.permissions.granted("access_activity")) {
console.log(`${today.adjusted.steps} Steps`);
mySteps.text=`${today.adjusted.steps}`;


}

const myBattery = document.getElementById("myBattery");
import { battery } from "power";
myBattery.text=(Math.floor(battery.chargeLevel) + "%");

Best Answer
0 Votes
6 REPLIES 6

Study your code, and ask yourself:

  • How often does the clock.ontick() code run? (Ie, under what circumstances does it run?)
  • How often does the hrm.addEventListener() code run? (Ie, under what circumstances does it run?)
  • How often does the steps code run? (Ie, under what circumstances does it run?)

Tip: use console.log('some text') in strategic places to help you to see which bits of your code are running when.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

It looks like the clock updates every minute. The heart rate monitor and the battery both have event listener and the steps doesn't so I'm guessing it has something to do with that bit? The steps look like they only update every day, so maybe it will only turn over to zero on it's own. I'm not sure how to tell the steps to update like the other things do.

Best Answer
0 Votes

You're right. A common approach is to update the step count within the ontick() function, because you know that's going to be called every minute.

 

If that's not fast enough for you, you can increase the granularity to 'seconds'.

 

Running that code every second means that you should try to make it as efficient as possible; ie, don't do stuff every second that doesn't need to be done every second. An example is populating month[].

Peter McLennan
Gondwana Software
Best Answer

You can use setInterval too, like this

function refresh_myActivity() {
statSteps.text = today.adjusted.steps 
  statCalories.text = today.adjusted.calories 
  statActiveMinutes.text = today.adjusted.activeMinutes

}


setInterval(refresh_myActivity, 700 /* refresh rate in milliseconds. change to whatever you'd like */);

 

Best Answer

Thanks for that! I'm going to mess with it and see what happens.

Best Answer
0 Votes
If it works, you can chose whose post is a best answer. or just give it a vote 🙂
Best Answer
0 Votes