Thank you for your donation!


Cloudsmith graciously provides open-source package management and distribution for our project.


Instruction Guide Automatic power on/off active speakers with connected power plug / IFTTT
#1
Wink 
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
Code:
#!/usr/bin/python
import requests
r = requests.post('https://maker.ifttt.com/trigger/power-on/with/key/EnterYourWebhooksKeyHere')
     /home/pi/ifttt_off.py
Code:
#!/usr/bin/python
import requests
r = requests.post('https://maker.ifttt.com/trigger/power-off/with/key/EnterYourWebhooksKeyHere')
Enter your Webhooks key in place of EnterYourWebhooksKeyHere for the 2 above files

     /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 &
     /home/pi/watchdog-speakers-off.sh
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 &
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"
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 Cool
Reply
#2
Very nice stuff
Reply
#3
GREAT STUFF! This is gonna make me use Moode as alarm clock!
Thanks a loooot!
Reply
#4
Thumbs Up 
Fantastic!!!

Work like a charm.

Very clear, organize, easy to understand and elegant code.

Thanks, thanks, thanks!!!
Reply
#5
(05-05-2021, 09:25 PM)chutiloco Wrote: Fantastic!!!

Work like a charm.

Very clear, organize, easy to understand and elegant code.

Thanks, thanks, thanks!!!

Thanks for feedback!
Reply
#6
It would be nice and very useful to have a version of the "watchdog-speakers-on.sh" script to check for alarm-clock events instead of sound device state, but sadly I've no skill to do this...
Reply
#7
(09-25-2021, 05:41 AM)Daksha Wrote: It would be nice and very useful to have a version of the "watchdog-speakers-on.sh" script to check for alarm-clock events instead of sound device state, but sadly I've no skill to do this...

hey, the script is powering up plug on alarm clocks already, I m not sure i follow your suggestion.
Reply
#8
(09-26-2021, 10:04 PM)lelobster Wrote:
(09-25-2021, 05:41 AM)Daksha Wrote: It would be nice and very useful to have a version of the "watchdog-speakers-on.sh" script to check for alarm-clock events instead of sound device state, but sadly I've no skill to do this...

hey, the script is powering up plug on alarm clocks already, I m not sure i follow your suggestion.

The current script powers up on soundcard activity. It fires speakers on whenever music plays, than of course, even on alarm clock events.
My suggestion (but I don't know if this is possible) was to have a script reacting to just the alarm event instead of soundcard state: this would be useful, for instance, to switch on speakers and lights too, every morning (but not every time music plays)  Wink
Reply


Forum Jump: