The following warnings occurred:
Warning [2] count(): Parameter must be an array or an object that implements Countable - Line: 906 - File: showthread.php PHP 7.2.34 (Linux)
File Line Function
/showthread.php 906 errorHandler->error



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
rpi 5, topping E70 velvet + B100, monitor audio silver 100
Reply


Messages In This Thread
Automatic power on/off active speakers with connected power plug / IFTTT - by lelobster - 09-03-2020, 10:35 AM

Forum Jump: