Warning when laptop is on battery power

This thread I liked, having had someone unplug a laptop PSU and not plug it back in!!!
I have a zero GP Script version that should work cross platform as it uses a python library that is supported on Windows, OSX and Linux, and uses OSC to deliver the battery status.

Add a text widget to the global rackspace and name it Battery. Enable OSC for the widget and it will set an OSC address of /GlobalRackspace/Battery/SetValue. You should also ensure that OSC is enabled from the menu Options/OSC Setup. Note the script is using SetCaption and not SetValue to change the text of the widget.

The following python oscbattery.py script can be run without any command line parameters if on the same machine as GP, if the defaults ports have been used and the widget value has been set to “Battery” in a global rackspace. It will update the battery status once per minute by default.

# Read battery percentage and send OSC message to Gig Performer
import psutil
from pythonosc import udp_client
import argparse
import time

if __name__ == '__main__':

    # Set the defaults and grab command line parameters if present
    parser = argparse.ArgumentParser()
    parser.add_argument("--ip", default="127.0.0.1", help="The ip of the OSC server")
    parser.add_argument("--port", type=int, default=54344, help="The port the OSC server is listening on")
    parser.add_argument("--interval", type=int, default=60, help="The number of seconds between updates")
    parser.add_argument("--widget", default="/GlobalRackspace/Battery/SetCaption", help="The OSC path to the widget")
    args = parser.parse_args()

    # Serve the OSC messages to Gig Performer
    client = udp_client.SimpleUDPClient(args.ip, args.port)
    while 1:
        battery = psutil.sensors_battery()
        plugged = "Plugged In" if battery.power_plugged else "Not Plugged In"
        client.send_message(args.widget, (str(battery.percent) + "% | " + plugged))
        time.sleep(args.interval)

A couple of points to note when running python on the MAC: the OS requires python to be present, so check the version of python preinstalled using python --version if this is python 3.x.x then all should be good. If the version is python 2.x.x then python 3 will need to be installed, DO NOT UNINSTALL python 2.x.x. Two additional modules will need to be installed, using pip3 install psutil python-osc. The pythonosc module requires Python 3 to function correctly.

The python script can then be launched from a command line terminal with python3 oscbattery.py, but the terminal will remain. With further effort it could be written to run as a service.

With very few changes this code can be used to read fanspeed, temperatures, memory etc…