09-03-2020, 10:35 AM
Hi, As my speakers and sub have a big electric consumption and because I wanted something more fluid, I manage to power them automatically if mpd/renderers are playing, also it's powered off after x minutes of audio inactivity. I used a connected (wifi) power plug, ifttt, and the code is inspired by original moodeaudio watchdog.sh script.
1. ifttt config
- Get a connected power plug compatible with ifttt (any model should work mine is the tp-link hs100)
- Give power plug access to ifttt (search for the right service, for me it was kasa)
- Install webhooks service on ifttt
- Create 2 applets on ifttt:
IF "webhooks event power-on" THEN "turn on power plug"
IF "webhooks event power-off" THEN "turn off power plug"
- Go to webhooks documentation or (settings) on ifttt and copy/paste your personnal key somewhere
2. Get your rpi ready to power on/off your speakers
ssh your pi and with the code below, create these 4 files:
/home/pi/ifttt_on.py
/home/pi/ifttt_off.py
Enter your Webhooks key in place of EnterYourWebhooksKeyHere for the 2 above files
/home/pi/watchdog-speakers-on.sh
/home/pi/watchdog-speakers-off.sh
For the 2 files above:
- Edit the card# according to your setup if it's not card0
- Edit the time you want the rpi to wait before power off speakers in watchdog-speakers-off.sh where there is "sleep 500" (meaning x seconds before power off, here 500)
- Make the 4 files executable "chmod +x /home/pi/all4files"
add to rc.local for execute script on boot
Add these 3 lines at the end of /etc/rc.local but before last line "exit 0"
3. Reboot and enjoy
1. ifttt config
- Get a connected power plug compatible with ifttt (any model should work mine is the tp-link hs100)
- Give power plug access to ifttt (search for the right service, for me it was kasa)
- Install webhooks service on ifttt
- Create 2 applets on ifttt:
IF "webhooks event power-on" THEN "turn on power plug"
IF "webhooks event power-off" THEN "turn off power plug"
- Go to webhooks documentation or (settings) on ifttt and copy/paste your personnal key somewhere
2. Get your rpi ready to power on/off your speakers
ssh your pi and with the code below, create these 4 files:
/home/pi/ifttt_on.py
Code:
#!/usr/bin/python
import requests
r = requests.post('https://maker.ifttt.com/trigger/power-on/with/key/EnterYourWebhooksKeyHere')
Code:
#!/usr/bin/python
import requests
r = requests.post('https://maker.ifttt.com/trigger/power-off/with/key/EnterYourWebhooksKeyHere')
/home/pi/watchdog-speakers-on.sh
Code:
#!/bin/bash
#
# Power ON script
#
HW_PARAMS_LAST=""
while true; do
HW_PARAMS=$(cat /proc/asound/card0/pcm0p/sub0/hw_params)
if [[ $HW_PARAMS != $HW_PARAMS_LAST ]]; then
if [[ $HW_PARAMS != "closed" ]]; then
sudo /usr/bin/python /home/pi/ifttt_on.py
fi
fi
sleep 5
HW_PARAMS_LAST=$HW_PARAMS
done > /dev/null 2>&1 &
Code:
#!/bin/bash
#
# Power OFF script
#
sleep 30
HW_PARAMS_LAST=""
while true; do
HW_PARAMS=$(cat /proc/asound/card0/pcm0p/sub0/hw_params)
if [[ $HW_PARAMS != $HW_PARAMS_LAST ]]; then
if [[ $HW_PARAMS = "closed" ]]; then
sleep 500
HW_PARAMS=$(cat /proc/asound/card0/pcm0p/sub0/hw_params)
if [[ $HW_PARAMS = "closed" ]]; then
sudo /usr/bin/python /home/pi/ifttt_off.py
fi
fi
fi
sleep 5
HW_PARAMS_LAST=$HW_PARAMS
done > /dev/null 2>&1 &
- Edit the card# according to your setup if it's not card0
- Edit the time you want the rpi to wait before power off speakers in watchdog-speakers-off.sh where there is "sleep 500" (meaning x seconds before power off, here 500)
- Make the 4 files executable "chmod +x /home/pi/all4files"
add to rc.local for execute script on boot
Add these 3 lines at the end of /etc/rc.local but before last line "exit 0"
Code:
# Power plug management
/home/pi/watchdog-speakers-off.sh > /dev/null 2>&1 &
/home/pi/watchdog-speakers-on.sh > /dev/null 2>&1 &
3. Reboot and enjoy
rpi 5, topping E70 velvet + B100, monitor audio silver 100