Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

Running on Battery?

If you have a question or need help, this is the place to be.
Post Reply
jwka
Posts: 24
Joined: Sun Nov 28, 2010 8:45 pm

Running on Battery?

Post by jwka »

I have already posted this question in the German forum but with no response, so, please forgive the "cross post".

I am in search to find out if a PC is running on battery or on power supply.

All I have found is the event "System.PowerStatustChange", but that does not inform in any way how (which direction) this change has taken place.

Any hint on how I can find out this (maybe this could be a feature also: have the event more informational and instead of just having one, why not have one "to batery" and one "back to power"?

Thanks so much for your help!
jwka
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Running on Battery?

Post by Pako »

Of course it is possible.
You can create a new macro:
PowerSource.jpg
PowerSource.jpg (6.18 KiB) Viewed 5540 times
Python script contains this code:

Code: Select all

from ctypes import windll, byref, Structure, c_ubyte, c_ulong

class SYSTEM_POWER_STATUS(Structure):
    _fields_ = [("ACLineStatus", c_ubyte),
                ("BatteryFlag", c_ubyte),
                ("BatteryLifePercent", c_ubyte),
                ("Reserved1", c_ubyte),
                ("BatteryLifeTime", c_ulong),
                ("BatteryFullLifeTime", c_ulong)]

sps = SYSTEM_POWER_STATUS()
windll.kernel32.GetSystemPowerStatus(byref(sps))
#print 'sps.ACLineStatus =',sps.ACLineStatus
#print 'sps.BatteryFlag =',sps.BatteryFlag
#print 'sps.BatteryLifePercent =',sps.BatteryLifePercent
#print 'sps.BatteryLifeTime =',sps.BatteryLifeTime
#print 'sps.BatteryFullLifeTime =',sps.BatteryFullLifeTime
eg.TriggerEvent('%s.%s' % ("PowerSource",("Battery","ACadapter")[sps.ACLineStatus]), prefix = "System")
And that's all.

Pako
jwka
Posts: 24
Joined: Sun Nov 28, 2010 8:45 pm

Re: Running on Battery?

Post by jwka »

Pako, thanks for that quick help. wow!

I did incorporate your code into this little code here (having the macro, the event and this script):

Code: Select all

import socket
import sys
from datetime import datetime
now = str(datetime.now())

from ctypes import windll, byref, Structure, c_ubyte, c_ulong

class SYSTEM_POWER_STATUS(Structure):
    _fields_ = [("ACLineStatus", c_ubyte),
                ("BatteryFlag", c_ubyte),
                ("BatteryLifePercent", c_ubyte),
                ("Reserved1", c_ubyte),
                ("BatteryLifeTime", c_ulong),
                ("BatteryFullLifeTime", c_ulong)]

sps = SYSTEM_POWER_STATUS()
windll.kernel32.GetSystemPowerStatus(byref(sps))
#print 'sps.ACLineStatus =',sps.ACLineStatus
#print 'sps.BatteryFlag =',sps.BatteryFlag
#print 'sps.BatteryLifePercent =',sps.BatteryLifePercent
#print 'sps.BatteryLifeTime =',sps.BatteryLifeTime
#print 'sps.BatteryFullLifeTime =',sps.BatteryFullLifeTime
#eg.TriggerEvent('%s.%s' % ("Power Switch",("Battery","ACadapter")[sps.ACLineStatus]), prefix = "System")


TCP_IP="xxx.xx.x.xx"
TCP_PORT=10xx
TCP_MESSAGE = ":Sender=TPR42R:ACLineStatus=" + str(sps.ACLineStatus) + ":Time=" + now

print 'MSG:SendIOS_01:Sending Power Satus to IPS: ACLineStatus =',sps.ACLineStatus
sock = socket.socket( socket.AF_INET, # Internet
                       socket.SOCK_STREAM ) # TCP
                    
                
                    
sock.connect( (TCP_IP, TCP_PORT) )
sock.send( TCP_MESSAGE )
Works fine apart from one thing I do not understand:
I get allways MULTIPLE lines in the event log (left pane) if I pull/push the power cord.

I am not sure if that happened befor as well (I did never monitor the log intensively up to date), and why the hell this occurs.

Any ideas on this?
Thanks som much!
jwka
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Running on Battery?

Post by Pako »

I do not know.
Too bad that you're not here to give a sample of your log.

Pako
jwka
Posts: 24
Joined: Sun Nov 28, 2010 8:45 pm

Re: Running on Battery?

Post by jwka »

Here is the Log ... but I doubt it will nt tell so much ...

Be aware that I have disabled the event that started the macro for testing purpose.

16:06:43 Task.Activated.EventGhost
16:07:18 Send ACLineStatus to IPS
16:07:18 MSG:SendIOS_01:Sending Power Satus to IPS: ACLineStatus = 1
16:07:37 System.PowerStatusChange
16:07:37 SendPowerStatusChangeToIPS [SendIPS_01]
16:07:37 Send ACLineStatus to IPS
16:07:37 MSG: Sending Power Satus to IPS: ACLineStatus = 1
16:07:49 Send ACLineStatus to IPS
16:07:49 MSG:SendIOS_01:Sending Power Satus to IPS: ACLineStatus = 1
16:07:59 Task.Deactivated.EventGhost
16:07:59 Task.Activated.Desktop
16:08:06 Task.Deactivated.Desktop
16:08:06 Task.Activated.EventGhost
16:08:20 System.PowerStatusChange
16:08:20 SendPowerStatusChangeToIPS [SendIPS_01]
16:08:20 Send ACLineStatus to IPS
16:08:20 MSG:SendIOS_01:Sending Power Satus to IPS: ACLineStatus = 0
16:08:20 System.PowerStatusChange
16:08:20 SendPowerStatusChangeToIPS [SendIPS_01]
16:08:20 Send ACLineStatus to IPS
16:08:20 MSG:SendIOS_01:Sending Power Satus to IPS: ACLineStatus = 0
16:08:25 System.PowerStatusChange
16:08:25 SendPowerStatusChangeToIPS [SendIPS_01]
16:08:25 Send ACLineStatus to IPS
16:08:25 MSG:SendIOS_01:Sending Power Satus to IPS: ACLineStatus = 1
16:08:28 Task.Deactivated.EventGhost
16:08:28 Task.Activated.firefox
16:08:35 System.PowerStatusChange
16:08:35 SendPowerStatusChangeToIPS [SendIPS_01]
16:08:35 Send ACLineStatus to IPS
16:08:35 MSG:SendIOS_01:Sending Power Satus to IPS: ACLineStatus = 1
16:10:38 Task.Deactivated.firefox
16:10:38 Task.Activated.EventGhost
16:10:39 Task.Deactivated.EventGhost
16:10:39 Task.Activated.firefox
16:10:49 Task.Deactivated.firefox
16:10:49 Task.Activated.EventGhost
16:10:58 System.PowerStatusChange
16:10:59 System.PowerStatusChange
16:11:05 System.PowerStatusChange
16:11:05 System.PowerStatusChange
16:11:20 System.PowerStatusChange
16:13:12 System.PowerStatusChange
16:13:12 System.PowerStatusChange
16:13:16 System.PowerStatusChange
16:13:35 System.PowerStatusChange
16:16:38 Task.Deactivated.EventGhost
16:16:38 Task.Activated.firefox
16:17:13 Task.Deactivated.firefox
16:17:13 Task.Activated.EventGhost
16:17:14 Task.Deactivated.EventGhost
16:17:14 Task.Activated.firefox
16:17:42 Task.Deactivated.firefox
16:17:42 Task.Activated.EventGhost
16:17:50 Task.Deactivated.EventGhost
16:17:50 Task.Activated.firefox
16:17:51 Task.Deactivated.firefox
16:17:51 Task.Activated.EventGhost
16:18:02 System.PowerStatusChange
16:18:02 System.PowerStatusChange
16:18:21 System.PowerStatusChange
16:18:30 Task.Deactivated.EventGhost
16:18:30 Task.Activated.firefox
16:18:35 System.PowerStatusChange
16:18:51 System.PowerStatusChange
16:19:03 System.PowerStatusChange
16:19:13 Task.Deactivated.firefox
16:19:13 Task.Activated.EventGhost
16:19:21 System.PowerStatusChange
16:19:27 Task.Deactivated.EventGhost
16:19:27 Task.Activated.firefox
16:19:34 System.PowerStatusChange
16:19:51 System.PowerStatusChange
16:20:03 System.PowerStatusChange
16:20:21 System.PowerStatusChange
16:20:35 System.PowerStatusChange
16:20:51 System.PowerStatusChange
16:21:03 System.PowerStatusChange
16:21:21 Task.Deactivated.firefox
16:21:21 Task.Activated.EventGhost
16:21:21 System.PowerStatusChange
16:21:35 System.PowerStatusChange
16:21:51 System.PowerStatusChange
16:22:03 System.PowerStatusChange
16:22:16 System.PowerStatusChange
16:22:21 System.PowerStatusChange
16:22:30 System.PowerStatusChange
16:22:35 System.PowerStatusChange
16:22:43 System.PowerStatusChange
16:22:51 System.PowerStatusChange
16:23:01 Task.Deactivated.EventGhost
16:23:01 Task.Activated.firefox
16:23:03 System.PowerStatusChange
16:23:17 Task.Deactivated.firefox
16:23:17 Task.Activated.EventGhost
16:23:18 Task.Deactivated.EventGhost
16:23:18 Task.Activated.firefox
16:23:20 Task.Deactivated.firefox
16:23:20 Task.Activated.EventGhost
16:23:21 System.PowerStatusChange
16:23:24 Task.Deactivated.EventGhost
16:23:24 Task.Activated.firefox
16:23:30 Task.Deactivated.firefox
16:23:30 Task.Activated.EventGhost
16:23:35 System.PowerStatusChange
16:23:41 System.PowerStatusChange
16:23:45 System.PowerStatusChange
16:23:52 System.PowerStatusChange
16:24:04 System.PowerStatusChange
16:24:22 System.PowerStatusChange
16:24:28 Task.Deactivated.EventGhost
16:24:28 Task.Activated.Desktop
16:24:35 System.PowerStatusChange
16:24:40 Task.Deactivated.Desktop
16:24:40 Task.Activated.EventGhost
16:24:49 Task.Deactivated.EventGhost
16:24:49 Task.Activated.firefox
16:24:52 System.PowerStatusChange
16:25:04 System.PowerStatusChange
16:25:22 System.PowerStatusChange
16:25:35 System.PowerStatusChange
16:25:52 System.PowerStatusChange
16:26:04 System.PowerStatusChange
16:26:22 System.PowerStatusChange
16:26:35 System.PowerStatusChange
16:26:52 System.PowerStatusChange
16:27:04 System.PowerStatusChange
16:27:22 System.PowerStatusChange
16:27:35 System.PowerStatusChange
16:27:47 Task.Deactivated.firefox
16:27:47 Task.Activated.EventGhost
16:27:52 System.PowerStatusChange
16:28:05 System.PowerStatusChange
16:28:11 Task.Deactivated.EventGhost
16:28:11 Task.Activated.Desktop
16:28:17 Task.Deactivated.Desktop
16:28:17 Task.Activated.EventGhost
16:28:22 System.PowerStatusChange
16:28:35 System.PowerStatusChange
16:28:52 System.PowerStatusChange
16:29:05 System.PowerStatusChange
16:29:22 System.PowerStatusChange
16:29:35 System.PowerStatusChange
16:29:51 Task.NewWindow.EventGhost
16:29:51 Task.Deactivated.EventGhost
16:29:51 Task.Activated.EventGhost
16:29:52 System.PowerStatusChange
16:30:05 System.PowerStatusChange
16:30:22 System.PowerStatusChange
16:30:35 System.PowerStatusChange
16:30:53 System.PowerStatusChange
16:31:05 System.PowerStatusChange
16:31:23 System.PowerStatusChange
16:31:34 System.PowerStatusChange
16:31:53 System.PowerStatusChange
16:32:05 System.PowerStatusChange
16:32:07 System.Idle
16:32:23 System.PowerStatusChange
16:32:35 System.PowerStatusChange
16:32:53 System.PowerStatusChange
16:33:00 System.PowerStatusChange
16:33:05 System.PowerStatusChange
16:33:23 System.PowerStatusChange
16:33:35 System.PowerStatusChange
16:33:53 System.PowerStatusChange
16:34:05 System.PowerStatusChange
16:34:23 System.PowerStatusChange
16:34:35 System.PowerStatusChange
16:34:53 System.PowerStatusChange
16:35:05 System.PowerStatusChange
16:35:23 System.PowerStatusChange
16:35:35 System.PowerStatusChange
16:35:53 System.PowerStatusChange
16:36:05 System.PowerStatusChange
16:36:23 System.PowerStatusChange
16:36:35 System.PowerStatusChange
16:36:53 System.PowerStatusChange
16:37:05 System.PowerStatusChange
16:37:23 System.PowerStatusChange
16:37:35 System.PowerStatusChange
16:37:54 System.PowerStatusChange
16:38:05 System.PowerStatusChange
16:38:24 System.PowerStatusChange
16:38:35 System.PowerStatusChange
16:38:54 System.PowerStatusChange
16:39:05 System.PowerStatusChange
16:39:24 System.PowerStatusChange
16:39:35 System.PowerStatusChange
16:39:54 System.PowerStatusChange
16:40:05 System.PowerStatusChange
16:40:24 System.PowerStatusChange
16:40:35 System.PowerStatusChange
16:40:54 System.PowerStatusChange
16:41:05 System.PowerStatusChange
16:41:07 Task.Created.logon
16:41:07 Task.NewWindow.logon
16:41:07 Task.Deactivated.EventGhost
16:41:07 Task.Activated.logon
16:41:07 Task.NewWindow.logon
16:41:07 Task.Deactivated.logon
16:41:07 Task.Activated.logon
16:41:07 Task.Deactivated.logon
16:41:07 Task.Activated.logon
16:41:24 System.PowerStatusChange
16:41:54 System.PowerStatusChange
16:42:05 System.PowerStatusChange
16:42:25 System.PowerStatusChange
16:42:35 System.PowerStatusChange
16:42:55 System.PowerStatusChange
16:43:05 System.PowerStatusChange
16:43:25 System.PowerStatusChange
16:43:55 System.PowerStatusChange
16:44:06 System.PowerStatusChange
16:44:25 System.PowerStatusChange
16:44:30 System.PowerStatusChange
16:44:55 System.PowerStatusChange
16:45:00 System.PowerStatusChange
16:45:25 System.PowerStatusChange
16:45:30 System.PowerStatusChange
16:45:55 System.PowerStatusChange
16:46:00 System.PowerStatusChange
16:46:25 System.PowerStatusChange
16:46:35 System.PowerStatusChange
16:46:55 System.PowerStatusChange
16:47:05 System.PowerStatusChange
16:47:25 System.PowerStatusChange
16:47:35 System.PowerStatusChange
16:47:55 System.PowerStatusChange
16:48:05 System.PowerStatusChange
16:48:25 System.PowerStatusChange
16:48:52 System.PowerStatusChange
16:49:05 System.PowerStatusChange
16:49:22 System.PowerStatusChange
16:49:35 System.PowerStatusChange
16:49:52 System.PowerStatusChange
16:50:22 System.PowerStatusChange
16:50:35 System.PowerStatusChange
16:50:52 System.PowerStatusChange
16:51:22 System.PowerStatusChange
16:51:35 System.PowerStatusChange
16:51:56 System.PowerStatusChange
16:52:26 System.PowerStatusChange
16:52:35 System.PowerStatusChange
16:52:56 System.PowerStatusChange
16:53:26 System.PowerStatusChange
16:53:56 System.PowerStatusChange
16:56:55 System.PowerStatusChange
17:00:46 System.UnIdle
17:00:46 Task.Deactivated.logon
17:00:46 Task.ClosedWindow.logon
17:00:46 Task.Activated.logon
17:00:46 Task.Deactivated.logon
17:00:46 Task.ClosedWindow.logon
17:00:46 Task.Destroyed.logon
17:00:46 Task.Activated.EventGhost
17:01:34 Task.Deactivated.EventGhost
17:01:34 Task.Activated.EventGhost
17:02:11 Task.Deactivated.EventGhost
17:02:11 Task.Activated.EventGhost
17:02:12 Task.Deactivated.EventGhost
17:02:12 Task.Activated.firefox
17:02:29 Task.Deactivated.firefox
17:02:29 Task.Activated.EventGhost
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Running on Battery?

Post by Pako »

It is really interesting, but I do not know the cause.
However if I understand correctly, then this phenomenon does not occur every time, but only sometimes.
It occurred to me the only option - flicker when switching on or off.
I tested it, but to me it never happened.
If you're going to want to avoid sending multiple messages, you'll have to introduce some state variables (such as eg.globals.powerSource) ...

Pako
jwka
Posts: 24
Joined: Sun Nov 28, 2010 8:45 pm

Re: Running on Battery?

Post by jwka »

With that other tiny script printing the batt infos, i found out the following:

System.PowerStatusChange is allways called if ANYTHING in the power system changes. This is also getting drained or getting loaded.

So, unless the battery is not fully loaded (and this, of course is the case specifically when operating with ono AC) every 15 seconds or so, the status seem to change (I have not tried out on other systems with newe/older batteries, but I would assume that this duration could be longer or shorter).

So, everybody using this event should be advised that it will be invoked quite often (and for non-ac also a little bit unprdictable in terms of timing).

[edit] as soon as I am a bit more experienced with Python & EG, I will post my "result" having a more stable and more usable code basis for this use.

(I am to use this for a domotic system telling it about power outages)
[\edit]

Thanks for the help so far!

jwka
swordh
Posts: 4
Joined: Sun Jul 03, 2011 9:40 am

Re: Running on Battery?

Post by swordh »

Pako wrote:Of course it is possible.
You can create a new macro:
PowerSource.jpg
Python script contains this code:

Code: Select all

from ctypes import windll, byref, Structure, c_ubyte, c_ulong

class SYSTEM_POWER_STATUS(Structure):
    _fields_ = [("ACLineStatus", c_ubyte),
                ("BatteryFlag", c_ubyte),
                ("BatteryLifePercent", c_ubyte),
                ("Reserved1", c_ubyte),
                ("BatteryLifeTime", c_ulong),
                ("BatteryFullLifeTime", c_ulong)]

sps = SYSTEM_POWER_STATUS()
windll.kernel32.GetSystemPowerStatus(byref(sps))
#print 'sps.ACLineStatus =',sps.ACLineStatus
#print 'sps.BatteryFlag =',sps.BatteryFlag
#print 'sps.BatteryLifePercent =',sps.BatteryLifePercent
#print 'sps.BatteryLifeTime =',sps.BatteryLifeTime
#print 'sps.BatteryFullLifeTime =',sps.BatteryFullLifeTime
eg.TriggerEvent('%s.%s' % ("PowerSource",("Battery","ACadapter")[sps.ACLineStatus]), prefix = "System")
And that's all.

Pako
Thanx! This help alot!
cableghost
Posts: 37
Joined: Thu Oct 10, 2013 9:43 pm

Re: Running on Battery?

Post by cableghost »

How do I use this script? I see 'TriggerEvent', though what would I add an action to be triggered on if all I want to know is the PC on battery power?

Thanks.

Scott
Post Reply