Battery percentage display

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

2 Likes

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.

you can use this in the windows command prompt

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.

2 Likes

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

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 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, also - the more community interest, the sooner we're likely to implement something like this.

1 Like

Okay, thanks for letting me know

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

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

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)…

3 Likes

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?

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.

2 Likes

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

You can use the Javascript battery API to retrieve the

Here is a bit of sample code:


	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

image

image

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

4 Likes

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

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;
2 Likes