# Battery percentage display

**URL:** <https://forum.inductiveautomation.com/t/battery-percentage-display/25993>\
**Category:** Ignition\
**Created:** [June 20, 2019, 5:23pm UTC](https://forum.inductiveautomation.com/t/battery-percentage-display/25993 "2019-06-20T17:23:18Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![rmore016](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@rmore016](https://forum.inductiveautomation.com/u/rmore016)\
**Post date:** [June 20, 2019, 5:23pm UTC](https://forum.inductiveautomation.com/t/battery-percentage-display/25993/1 "2019-06-20T17:23:18Z")

</div>

Is there a way to retrieve and display the percentage of the mobile device that is being used. I am attempting to display this percentage in a window, advice on this would be helpful. Thanks

---

<div class="post-metadata">

**Author:** ![jlivingston](https://avatars.discourse-cdn.com/v4/letter/j/779978/32.png) [@jlivingston](https://forum.inductiveautomation.com/u/jlivingston)\
**Post date:** [June 20, 2019, 6:49pm UTC](https://forum.inductiveautomation.com/t/battery-percentage-display/25993/2 "2019-06-20T18:49:12Z")

</div>

Re: Battery Life Retrieval- In a few words, that depends on whether you are talking about one specific device or a variety. For Instance, On a system that I use: Start Menu\>WindowsSystem\>Control Panel\>All Control Panel Items\>Power Options would drill down to where I would choose Dell Extended Battery Life Options and get a display window popup with a Battery Status Tab including a Bargraph. This is great, but you would have to set-up a remote-control type client which some folks might object to. For instance, the Battery Status Tab is accompanied by a Battery Health Tab within the Battery Meter Popup. From the Battery Health Tab, it might be possible to order a new battery online from Dell ( Using their name from your application ). Now while that may seem unlikely, giving you that authority might be easier for them than it would be for you to set up that level of access to their Windows PC. This might be a bit easier on an Android mobile device, provided that all of the ones you wanted to access had the same firmware levels ( essentially all the same hardware ). It might work until someone upgrades the Android OS or just a specific part of the firmware. Thus I would discourage it unless you have a great amount of time and money to engage in the project.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [June 20, 2019, 8:03pm UTC](https://forum.inductiveautomation.com/t/battery-percentage-display/25993/3 "2019-06-20T20:03:40Z")

</div>

you can use this in the windows command prompt

```auto
WMIC PATH Win32_Battery Get EstimatedChargeRemaining

```

I have no idea if you can execute this in an Ignition Client and return the value, but might be worth trying. If you could do this, you could write the returned value to a tag then bind to a graphic.

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [June 21, 2019, 12:53am UTC](https://forum.inductiveautomation.com/t/battery-percentage-display/25993/4 "2019-06-21T00:53:36Z")

</div>

> [@rmore016](#):
>
> mobile device

Is this question referring to Perspective app, Perspective browser, or something else?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [June 21, 2019, 3:18pm UTC](https://forum.inductiveautomation.com/t/battery-percentage-display/25993/5 "2019-06-21T15:18:21Z")

</div>

> [@rmore016](#):
>
> Is there a way to retrieve and display the percentage of the mobile device that is being used. I am attempting to display this percentage in a window, advice on this would be helpful. Thanks

At least for now, the answer to this question is "no". I _could_ see us adding something like this as a value-add to the mobile applications, but [browser support](https://caniuse.com/#feat=battery-status) for fetching battery status is limited, and we don't like to add features that only work on one platform.

This sounds like a good post for [ideas.inductiveautomation.com](http://ideas.inductiveautomation.com), also - the more community interest, the sooner we're likely to implement something like this.

---

<div class="post-metadata">

**Author:** ![rmore016](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@rmore016](https://forum.inductiveautomation.com/u/rmore016)\
**Post date:** [June 25, 2019, 5:37pm UTC](https://forum.inductiveautomation.com/t/battery-percentage-display/25993/6 "2019-06-25T17:37:40Z")

</div>

Okay, thanks for letting me know

---

<div class="post-metadata">

**Author:** ![rmore016](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@rmore016](https://forum.inductiveautomation.com/u/rmore016)\
**Post date:** [June 25, 2019, 5:38pm UTC](https://forum.inductiveautomation.com/t/battery-percentage-display/25993/7 "2019-06-25T17:38:20Z")

</div>

I wanted to try something through this route. Thank you for the suggestion

---

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [June 27, 2019, 12:00pm UTC](https://forum.inductiveautomation.com/t/battery-percentage-display/25993/8 "2019-06-27T12:00:04Z")

</div>

I’ve put this inside actionPerformed event of the Timer component (15seconds) in Vision (not Perspective):

```auto
def bateryremaining():
	import subprocess
	proc = subprocess.Popen('powershell.exe -noprofile -command "(Get-WmiObject win32_battery).estimatedChargeRemaining"', stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
	tmp = proc.stdout.read()
	print "estimatedChargeRemaining: ", tmp
	system.tag.write("[Client]bateryRemaining", tmp)
system.util.invokeAsynchronous(bateryremaining)

```

Running client on Windows 10 Pro tablet  
and it is working (Ignition v7.9.11)…

---

<div class="post-metadata">

**Author:** ![ankitkarwa21](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@ankitkarwa21](https://forum.inductiveautomation.com/u/ankitkarwa21)\
**Post date:** [August 25, 2020, 9:51pm UTC](https://forum.inductiveautomation.com/t/battery-percentage-display/25993/9 "2020-08-25T21:51:01Z")

</div>

This method works in v8. However, I am getting inaccurate info using this.  
Value returned doesnt match the value indicated on windows taskbar battery UI.  
Does perspective have better support for battery status?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 25, 2020, 10:50pm UTC](https://forum.inductiveautomation.com/t/battery-percentage-display/25993/10 "2020-08-25T22:50:39Z")

</div>

No, support hasn’t changed since my last answer. Also, the value is likely inaccurate because you’re running that script on the gateway, and therefore returning the value that process will return on the gateway, _not_ the actual client device’s battery.  
The only way to execute arbitrary code on the frontend in Perspective is by creating a custom module. That’s a deliberate decision.

---

<div class="post-metadata">

**Author:** ![simeonw](https://avatars.discourse-cdn.com/v4/letter/s/4da419/32.png) [@simeonw](https://forum.inductiveautomation.com/u/simeonw)\
**Post date:** [July 4, 2024, 11:55pm UTC](https://forum.inductiveautomation.com/t/battery-percentage-display/25993/11 "2024-07-04T23:55:23Z")

</div>

I know this is an old post, but for anyone who is interested, using the JS injection method by @victordcq from here:

> [@Javascript inject in Perspective... have fun](https://forum.inductiveautomation.com/t/javascript-inject-in-perspective-have-fun/65805):
>
> Soo i'll just link this here... You can do whatever with it, but its not really gonna be clean or easy xD Have fun man_mage WARNING Dont let userinput enter in here

You can use the Javascript battery API to retrieve the

> **[Battery Status API - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API)**
>
> The Battery Status API, more often referred to as the Battery API, provides information about the system's battery charge level and lets you be notified by events that are sent when the battery level or charging status change. This can be used to...

Here is a bit of sample code:

```python

	code = """<img style='display:none' 
	     src='/favicon.ico' 
	     onload="
	     
		     const jsInjectNode = document.getElementById('jsInjectBatteryMonitor');
		     
		     const dataComponentPath = jsInjectNode.getAttributeNode('data-component-path').value;
		     let view = [...window.__client.page.views._data.values()].find(
		       (view) => view.value.mountPath == dataComponentPath.split('.')[0]
		     ).value;
		     
		     function updateBatteryUI(battery) {
		       console.log('Battery Changed', battery);
		       view.custom.write('battery.level', (battery.level * 100));
		       view.custom.write('battery.charging', battery.charging);
		       view.custom.write('battery.chargingTime', battery.chargingTime);
		       view.custom.write('battery.dischargeTime', battery.dischargingTime);
		     }
		     
		     function monitorBattery(battery) {
		       updateBatteryUI(battery);
		       battery.addEventListener('levelchange',updateBatteryUI.bind(null, battery));
		       battery.addEventListener('chargingchange',updateBatteryUI.bind(null, battery));
		       battery.addEventListener('dischargingtimechange',updateBatteryUI.bind(null, battery));
		       battery.addEventListener('chargingtimechange',updateBatteryUI.bind(null, battery));
		     }
		     
		     if ('getBattery' in window.navigator) {
		       navigator.getBattery().then(monitorBattery);
		     } else {
		       console.log('The Battery Status API is not supported on ' +'this platform.');
		     }
	     
	       ">
	</img>""".replace("\n", "").replace("\t", "")

```

If you place this in a markdown component in a new view and add the following custom props, you can create yourself a little embeddable battery monitor.

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/5/8592c6da85b908873852c741a8d01b7f1550a75d.png)

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/4/f4e5c1f913468b08793ac1637728022623fc1285.png)

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/9/794d4f3dca5a4ed053c3473c0782a1259d29966c.png)

Keep in mind you need to be using HTTPS, not HTTP in order to use the Battery Status API.

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [July 5, 2024, 6:38am UTC](https://forum.inductiveautomation.com/t/battery-percentage-display/25993/12 "2024-07-05T06:38:18Z")

</div>

Ive recently started using this to get the view, a bit shorter than what i originally had. Than you dont really have to define this "jsInjectBatteryMonitor" ID.  
But yeah it works too

```python
const view = [...window.__client.page.views._data.values()].find(view => view.value.mountPath == this.closest('[data-component-path]').getAttributeNode('data-component-path').value.split('.')[0]).value;

```

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [April 1, 2025, 2:45pm UTC](https://forum.inductiveautomation.com/t/battery-percentage-display/25993/13 "2025-04-01T14:45:18Z")

</div>

(ignition 8.147)  
I try to use this kind of script for a similar use case,  
I want to update a custom property of the markdown with some JS listener.  
view.custom.write seems to be executed, but a binding based on the custom property seems not to be updated.

@simeonw, @victordcq did you encounter this kind of issue ?

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [April 1, 2025, 2:49pm UTC](https://forum.inductiveautomation.com/t/battery-percentage-display/25993/14 "2025-04-01T14:49:19Z")

</div>

> [@mazeyrat](#):
>
> view.custom.write seems to be executed, but a binding based on the custom property seems not to be updated.

wdym "seems to be executed" ?

note that these scripts never work the same in the designer as in the browser. so test in browser

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [April 1, 2025, 3:02pm UTC](https://forum.inductiveautomation.com/t/battery-percentage-display/25993/15 "2025-04-01T15:02:12Z")

</div>

I tested in navigator. When I read the value provided to view.custom.write in the navigator console it's ok, but the binding is not updated. I will post the code asap
