Formula not working as intended

Hi

I’ve got the following formula for tracking my BT headphones battery level in a widget.

It works fine when the device is connected but when it isn’t it is just returning blank rather than the local variable that should contain the last known charge level.

What have I done wrong?
Cheers

$lv("lastbtbatterylevel", if(nc(abatt, 0) >-1, nc(abatt, 0), lv("lastbtbatterylevel")))$

$if(nc(abatt, 0) > -1, nc(abatt, 0), lv("lastbtbatterylevel"))$

Hi there,

As far as I know, a local variable does not persistently keep its value.

It would be better to use global variable, and update its value through a Flow.

I hope this would help.
:smiling_face::+1:

Ah, ok, makes sense, I’ll try gv.

Many thanks

TIPS
Create a Flow with a cron trigger (let’s say, every minute), to retrieve the battery level of a Bluetooth device, then store the retrieved value to a global variable (but only if the value is ≥ 0).

Then get the value of that global variable and display or use it as you like.

I hope this would help.
:smiling_face::+1:

I’d rather use OS events (which I would assume is how KWGT gets the battery info) rather than constantly poll for it (battery drain would be a concern)

Do you know how to read gv data from within a battery bar, the reference I see on the site just refers to reading gv from a komponent?

Which reference did you mean, by the way?

IMO, using a Flow with cron trigger doesn’t drain the battery.

CMIIW by the way.
:pray::smiling_face:

I was referring to this: How to link komponent and root global variables | Kustom HQ

And with regards to a cron job running every minute, that would mean your phone wont be hitting deep sleep so I would imagine it would impact battery life.

I tried this…

The Flow

The Progress element

I hope this would help.
:smiling_face::+1:

Apologies, when you mentioned cron jobs I didnt know that was built into KWGT via Flows. :slight_smile:

I’ve followed your example and the setting of the gv is fine, but I’m not seeing how to read that value in my battery bar - if I use a standard gv formula its empty (but the actual value is 100 as shown in the flows UI) but if I select the Battery Bar’s Amount property and hit the globe I dont have the option to select my battery gv, what have I missed?

Thanks for your help!

What does your Battery Bar look like?

Does it look like this?

No, it’s a battery bar but I linked it to the global var in the Amount prop but its just empty.

Weirdly it’s started working. I did play about and got working a version of the flow that writes the global variable to a local file but after I removed that action the flow still works. Weird why it was misbehaving initially.