Took the liberty of making a small sample project you can check out in the zipfile. This is one way to go about it, I'm no master, so there might be smarter ways of going about it.
It evolves around the game tracking the global property "sys_batterytime".
The "startup" script puts the value to 100, shows the widget and give the player 20 batteries.
The batteries activate the script "sys_usebattery", when used it adds 250 to the sys_batterytime global property (which one could call a variable, because its actual number varies).
I changed the keybind "F" to activate the script "sys_turnonflashlight". Which does a lot of things. First it checks if the flashlight is on, and turns it off if it is, by checking the global boolean property IsFlashlightOn. Then checks if sys_batterytime is bigger than 0, and gives a "need more batteries" speech if not. Otherwise it attaches the flashlight, sets IsFlashlightOn to true and continuously drain 0.2 from sys_batterytime every 0.001 seconds in the "while loop" until sys_batterytime reaches zero. When reaching zero it simple detaches the flashlight and puts IsFlashlightOn to False
The only thing a bit complicated is how it "turns off" the flashlight if you push F before the battery runs out. Because the loop that drains the sys_batterytime doesn't stop until it see it has reached zero. So when turning the off the flashlight, it quickly saves the sys_batterytime in a variable, then changes it to zero, thus stopping the loop draining, then uses the variable to set the sys_batterytime back to what it was when you stopped using it.
(Which is why the widget flicker for one frame, when I quickly turn the flashlight on and off in the video, because the value is actually zero for a millisecond. I'm not smart enough yet to end the while loops without crashing the game otherwise.)
Good luck!