Main power / secondary power and safe shutdown script

I need to know if the following is possible, I am trying to build a script but my knowledge of the API does not go far.

This script must include a way to configure:

  • Main power selection: 12v, USB or 5v battery (this will select the main power source to guard)
  • Secondary fallback selection: 12v, USB or 5v battery (if main power fails, switch to this source)
  • Shutdown timer: X seconds (if main power source fails, do a safe shutdown after X ammount of seconds)

This will give me the following scenario:

  • Use the UPS on a car or boat 12v system. Users will NOT safe shutdown the software so the UPS must take over.
  • Boat or car will power off, so no more 12v. Now, the 5v battery must take over.
  • At that same time a command “sudo shutdown -r now” (or in X seconds) will initiate for safe shutdown.

After 12v is restored, the UPS must power the Pi back on so it will boot. I don’t want to press any buttons for this.

Anybody know if this is possible?

Hi Jamos,

In the API scripting you don’t so much ‘configure’ the power off event to do something (which can be done in the SixFab Power interface at power.sixfab.com), instead you’d have to create a background script that would continually monitor the working_mode:
github.com/sixfab/sixfab-power-python-api/blob/master/power_api/power_api.py:

def get_working_mode(self, timeout=RESPONSE_DELAY):
    """
    Function for getting working mode
    
    Parameters
    -----------
    timeout : int (optional)
        timeout while receiving the response (default is RESPONSE_DELAY)
    Returns
    ------- 
    working_mode : int
        "1" for CHARGING, "2" for FULLY_CHARGED, "3" for BATTERY POWERED
    """

    command.create_command(command.PROTOCOL_COMMAND_GET_WORKING_MODE)
    command.send_command()
    delay_ms(timeout)
    raw = command.receive_command(COMMAND_SIZE_FOR_UINT8)

    mode = raw[PROTOCOL_HEADER_SIZE]
    return mode

What’s important in the snippet above is working_mode: 3 which would flag that mains has been disconnected.

I won’t teach you to suck eggs and write this for you because where’s the fun in that? :wink:
But what you want is a python script that imports PowerApi (You can check the examples here) and checks the input status every second or so, then you can set up your ‘disconnection timeout’ accordingly! :smiley:

Hope this helps, if you’ve got any questions just lemme know.
Regards,
Daniel

@Danny_Q,

Something worth knowing: It is not necessarily correct to assume get_working_mode indicates a full disconnect. While connected to external power, under heavy usage, the battery will be discharging. So, BATTERY_POWERED does not mean that external power is disconnected.

One must monitor the voltages to be sure it is disconnected.

S

Thanks for the heads up, hadn’t thought of that!

Many thanks,
Daniel

@jamos @Danny_Q,

Here is a solution that does exactly this. I have the same use case.

[SOLVED] How to make Easy Deployment Mode enable just before shutdown!