Retroflag Case Safe Shutdown script

  • Hello,

    I own a few Raspberry Pi (3&4) that are inside Retroflag cases (SuperPi Case, Nespi4 Case and SuperPi 4 Case). They all support safe shutdown which means that they have physical buttons that allow the OS to send a signal to shutdown itself rather than just being on an off physical switches.

    Retroflag provides their own official script with installers for several OS but LibreElec is not one of them: https://github.com/RetroFlag/retroflag-picase

    There are a few mentions of people trying to do the same thing on this forum in the past but the instructions don't work or seem obsolete. Is there a straightforward way to make this work on the latest version of LibreELEC?

    This is handled automatically when installing the latest version of Recalbox, but it provides an old version of Kodi that brings its own multiple issues.

    Any help or pointing in the right direction would be much appreciated!

  • Altough I m not sure how that script works...Assuming you already connected the cables to your rpi gpio pins...the "recalbox_SafeShutdown.py" should work fine for you..

    step 1

    Install virtual.rpi-tools addon from libreelec repo.

    step 2

    download the script and place somewhere in your rpi storage where you keep your scripts

    add these 2 lines to the top

    Code
    import sys
    sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib')

    then

    chmod +x <path to script>

    dos2unix <path to script>

    step 3

    test with command

    Code
    /usr/bin/python <path to script>

    If it works just create a service or add to autostart.sh last command in step 3 to always start the script on startup..

  • Thanks!

    It almost works with your instructions, however there must be some other command that's needed before issuing os.system("sudo shutdown -r now") in the poweroff() function for it to truly replicate the "power off system" item in kodi's menu: without it, the RPi does shut down but it doesn't send a HDMI-CEC signal to TV and amplifyer to shut down as well.

    I've tried adding a os.system("killall kodi") followed by os.system("sudo sleep 5s") or even a os.system("systemctl stop kodi") but that does not help. What am I missing here?

    EDIT: It seems I have figured it out, it works with:

    os.system("kodi-send --action=Powerdown")

    os.system("sudo sleep 5s")

    os.system("shutdown -r now")

    Edited once, last by danielristic: Answered my own question (November 3, 2022 at 6:10 PM).

  • Great...is the led blinking for you?

    You can adjust blinking speed from lines 38 and 40...just adjust that 0.2..

    Also remove this line:

    Code
    os.system("sudo sleep 5s")

    just use:

    Code
    time.sleep(5)

    Edited once, last by Mario77 (November 5, 2022 at 12:04 AM).

  • Thanks for the suggestion, both os.system("sudo sleep 5s") and time.sleep(5) seem to work intchangeably.

    The led only blinks buring the few seconds of poweoff, which is the expected behavior (the rest of the time it just stays on).

    Thanks again for your suggestions, I've been trying to make this script work for quite some time and with your help is was much easier than expected!

  • Thanks for the suggestion, both os.system("sudo sleep 5s") and time.sleep(5) seem to work intchangeably.

    The led only blinks buring the few seconds of poweoff, which is the expected behavior (the rest of the time it just stays on).

    Thanks again for your suggestions, I've been trying to make this script work for quite some time and with your help is was much easier than expected!

    Code
    LibreELEC:~ # sudo
    
     There is no working 'sudo'.
     
     On debian/ubuntu/all general purpose linux distributions 'sudo'
     allows a permitted user to execute a command as the superuser
     or another user, as specified by the security policy
     
     With LibreELEC you have root access by default, so you dont need 'sudo'