Auto Shutdown on External Power Loss

I’m looking to simply initiate a “safe” shutdown of the rPi (3B+) on the event that external power is lost. I have been looking through docs and see ways to schedule events, but i don’t see any way to “react” to events such as a switch from external to battery power from the hat. Is there something existing that can accommodate this? (i imagine it is a common use case) Otherwise, is there any pin or “signal” i can monitor when the hat switches from external to battery power?

Hi @pfemiani,
This event is not yet available but we will consider this for the upcoming features.

You can use the get_working_mode function in the Python API for this.

Hi @ensar and @pfemiani,

get_working_mode is ok, but under high power usage (while plugged in), the system will still report this condition as BATTERY_POWERED. It is appropriate to double check the output of api.get_input_power() to verify that power has actually been removed.

Something like:
if api.get_working_mode() == Definition.BATTERY_POWERED and api.get_input_power() <= 1.:
(… create and schedule hard shutdown event)
api.hard_power_off()

I run this as part of a 'watch python3 … ’ command so that it is done every 2 seconds (by default). Works great! Check out my other post for a hardware boot work around, if you need that too.

S

1 Like