Automatic turn on/off receiver
I want to turn on my receiver with my USB-controlled power outlet when my computer is playing sound. When there has not been any sound output for 5 minutes, I want the power outlet to switch off again.
Testing if there is any sound playing
My computer is using pulseaudio as sound server. With
pacmd list-sinks I can get a load of informations about the audio sinks pulseaudio is using. The information we are looking for is state:
pacmd list-sinks | grep state. If there is sound playing one or several sinks will be running. Because we only want to know if any sink is running we will use
grep --quiet. This will not generate any output other then the exit status.
pacmd list-sinks | grep --quiet state:\ RUNNING
Switching the outlet
I am using a Silvershild PM USB-controlled power-outlet to switch the power on and off. I wrote an Blog post a wile ago Sispmcl on how to use it.
On my outlet the receiver is on plug number four.
sispmctl -D 01:01:3e:1f:00 -o 4
Putting everything together
Now we only need to write some bash code to implement the hole thing.
My script endet looking like this:
#!/bin/bash sispmctl -D 01:01:3e:1f:00 -f 4 COUNT=0 ISON=0 while true; do #start endless loop sleep 5 if pacmd list-sinks | grep --quiet state:\ RUNNING; then if [ $ISON -eq 0 ]; then sispmctl -D 01:01:3e:1f:00 -o 4 ISON=1 fi COUNT=0 else if [ $COUNT -eq 60 ]; then echo $COUNT sispmctl -D 01:01:3e:1f:00 -f 4 ISON=0 COUNT=0 else let COUNT=COUNT+1 fi fi done