Moode Forum
Auto power off / shut down when not playing music - Printable Version

+- Moode Forum (https://moodeaudio.org/forum)
+-- Forum: moOde audio player (https://moodeaudio.org/forum/forumdisplay.php?fid=3)
+--- Forum: FAQ and Guides (https://moodeaudio.org/forum/forumdisplay.php?fid=9)
+--- Thread: Auto power off / shut down when not playing music (/showthread.php?tid=7740)



Auto power off / shut down when not playing music - bottlecaps - 05-19-2025

Instructions for seting up a small script that will power off the raspberry pi after not playing music for a given time.
If the pi is just starting the program it will wait for 3 hours until it powers off, if the pi is paused it will wait 2 hours and if it's stopped it will wait 1 hour. This time can be changed in the script.
First backup your MicroSD
Create a folder 
Code:
sudo mkdir -p /var/tmp/autopoweroff
Create a tmpfs mount point in fstab, do not mess this up as it might make your pi unbootable (I'm using vim, change it if you want)

Code:
sudo vim /etc/fstab
Add the following line
Code:
tmpfs /var/tmp/autopoweroff tmpfs nodev,nosuid,size=1M 0 0
You can now reboot you pi and test if it boots
Create systemd timer in
Code:
sudo vim /etc/systemd/system/autopoweroff.timer
Containing
Code:
[Unit]
Description="Timer for autopoweroff.service"
After=var-tmp-autopoweroff.mount

[Timer]
Unit=autopoweroff.service
OnBootSec=5min
OnUnitActiveSec=10min

[Install]
WantedBy=timers.target
If you want to change the loop time for cheking if music is playing change the value in OnUnitActiveSec
Create a service in
Code:
sudo vim /etc/systemd/system/autopoweroff.service
Containing
Code:
[Unit]
Description="Auto powers off moode when not in use"
Requires=autopoweroff.timer

[Service]
Type=simple
ExecStart=/bin/bash /home/moodeuser/autopoweroffv1
Create the script in
Code:
vim /home/moodeuser/autopoweroffv1
Containing
Code:
#!/bin/bash

#Change this values if you want to change how long it takes to power off
#In seconds
starting=10800
pausing=7200
stopping=3600

if [[ $(pgrep -c -f system-updater.sh) == 0 ]]; then
   if [[ $(mpc | grep -c "Updating DB") == 0 ]]; then
       if [[ $(pgrep -c thumb-gen.php) == 0 ]]; then
           time=/var/tmp/autopoweroff/time
           #if para ver si existe el archivo donde almacenamos la variable
           if [ -f "$time" ]; then
               read -r line</var/tmp/autopoweroff/time
               while read line; do
                   if [[ $line =~ STARTING ]]; then
                       sleeptime=${line#*=}
                       timenow=$(date +%s)
                       if [ $sleeptime -lt $timenow ]; then
                           sudo poweroff
                       fi
                       if [[ $(mpc status %state%) == 'playing' ]]; then
                           echo "PLAYING">$time
                       fi
                   elif [[ $line =~ PLAYING ]]; then
                       if [[ $(mpc status %state%) == 'paused' ]]; then
                           sleeptime=$(date +%s)
                           echo "PAUSED=$((sleeptime+$pausing))">$time
                       elif [[ $(mpc status %state%) == 'stopped' ]]; then
                           sleeptime=$(date +%s)
                           echo "STOPPED=$((sleeptime+$stopping))">$time
                       else
                           echo "PLAYING">$time
                       fi
                   elif [[ $line =~ PAUSED ]]; then
                       if [[ $(mpc status %state%) == 'playing' ]]; then
                           echo "PLAYING">$time
                       elif [[ $(mpc status %state%) == 'stopped' ]]; then
                           sleeptime=$(date +%s)
                           echo "STOPPED=$((sleeptime+$stopping))">$time
                       else
                           sleeptime=${line#*=}
                           timenow=$(date +%s)
                           if [ $sleeptime -lt $timenow ]; then
                               sudo poweroff
                           fi
                       fi
                   elif [[ $line =~ STOPPED ]]; then
                       if [[ $(mpc status %state%) == 'playing' ]]; then
                           echo "PLAYING">$time
                       elif [[ $(mpc status %state%) == 'paused' ]]; then
                           sleeptime=$(date +%s)
                           echo "PAUSED=$((sleeptime+$pausing))">$time
                       else
                           sleeptime=${line#*=}
                           timenow=$(date +%s)
                           if [ $sleeptime -lt $timenow ]; then
                               sudo poweroff
                           fi
                       fi
                   fi
               done <$time
           else
               sleeptime=$(date +%s)
               echo "STARTING=$((sleeptime+$starting))">$time
           fi
       fi
   fi
fi
Change the values of "starting", "pausing" and "stopping" at the begining of the script as you see fit.
Reload systemd daemon
Code:
sudo systemctl daemon-reload
Enable and start timer
Code:
sudo systemctl enable autopoweroff.timer
sudo systemctl start autopoweroff.timer
The script is ready.
If anybody knows how to minimize my script system load please do share. Any advice will also be welcomed.

Thanks
bottlecaps