Project

General

Profile

Power-saving techniques - sleep

Added by Anthony Thomas over 10 years ago

I'm looking to attempt to try and save some power across my electronics in the house.

One thing I'm looking at it is having my TVH server turned off (or suspended) when it isn't needed (which is most of the day!).

It looks like the wakeup scripts should be easy enough to setup for scheduled recordings.

Is it possible for TVH to turn off/suspend the system if there's been no active connections for "x" minutes? Is that even possible to track?

In terms of turning it back on, my idea was to send a WOL packet from my HTPCs when they turn on


Replies (21)

RE: Power-saving techniques - sleep - Added by Anthony Thomas over 10 years ago

Has the JSON API had any documentation made for it yet?

Looking to make a script to make all of the above possible. Should be able to just use status.xml but wouldn't mind a bit more information.

eg: I don't mind suspending when "epggrab" working but that comes up as a subscription in status.xml.

https://tvheadend.org/projects/tvheadend/wiki/Wakeup will definitely be useful.

I'll post a working script once I've made it if anyone is interested.

Also, is there any documentation regarding hidden things like status.xml?
Because I had no idea it existed until today!

RE: Power-saving techniques - sleep - Added by Anthony Thomas over 10 years ago

http://xxx.xxx.xxx.xxx:9981/api/status/subscriptions was what I was looking for. But status.xml seems to give enough info anyway.

RE: Power-saving techniques - sleep - Added by Anthony Thomas about 10 years ago

So I've managed to make a script that does a reasonable job of putting the system to sleep when it isn't needed.

It means rather than being on 24/7 it's only on about 6 hours a day.

I've used the exact 95_timer.sh from here - https://tvheadend.org/projects/tvheadend/wiki/Wakeup

For the 99_htpc.sh detailed in the Wakeup page on the wiki I have tweaked it slightly due to different DVB cards and the system in question being a standalone TVH server with no XBMC etc.

I have a TBS 6981 and two TBS 8920s. Not totally sure if I need to unload and load all these modules but it does the job.

#!/bin/sh

case "$1" in
suspend|hibernate)
/etc/init.d/tvheadend stop
rmmod cx88_dvb
rmmod cx8802
rmmod cx8800
rmmod cx25840
rmmod cx23885
rmmod cx2341x
;;
resume|thaw)
modprobe cx88_dvb
modprobe cx8802
modprobe cx8800
modprobe cx25840
modprobe cx23885
modprobe cx2341x
sleep 1
/etc/init.d/tvheadend start
;;
esac

To put the system to sleep I have the following (edited) script running as a cron job every 10 mins.

It can obviously be improved but so far it works fairly well.

#!/bin/sh
#

rm -rf /tmp/tvhsleep
mkdir -p /tmp/tvhsleep
cd /tmp/tvhsleep
wget -q --user=tv --password=tv http://127.0.0.1:9981/status.xml

subs="`cat status.xml | grep -i subs | grep -o "[0-9]"`" 
recn="`cat status.xml | grep -i next | grep -Eo '[0-9]{1,9}'`" 
rm status.xml

# Checks if HTPC is online.
if ping -c 1 -w 1 xxx.xxx.xxx.xxx
then
echo "htpc online" 
echo "Stay Awake" 
exit
else
echo "htpc offline" 
:
fi

# Check for any subscriptions
if [ "$subs" -gt 0 ]
then
echo $subs "subscriptions" 
echo "Stay Awake" 
exit
elif [ "$subs" -eq 0 ]
then
echo "No subs - check DVR" 
:
fi

# Check pending recordings
if [ -z "$recn" ]
then
echo "No recs scheduled" 
echo "sleep" 
/usr/sbin/pm-suspend
elif [ "$recn" -lt 10 ]
then
echo "Next rec less than 10 mins" 
echo "Stay Awake" 
exit
else
echo "Next rec more than 10 mins" 
echo "sleep" 
/usr/sbin/pm-suspend
fi

The structure/idea might be useful to someone!

To wake the system I have my OpenELEC HTPCs sending a WOL packet on boot.

RE: Power-saving techniques - sleep - Added by xraynorm - about 10 years ago

The script works great. It will wake your server when a recording is about to start.

#!/bin/bash

# set directory
cd ~hts/.hts/tvheadend/dvr/log

# start CHECK THIS section

# set time in seconds to prevent this script from running during boot
sleep 150s

# set time NOT to shut down before next recording
# minimum time in seconds needed for consecutive shutdown AND startup
safe_margin_shutdown=600

# set time to start up before next recording
# minimum time in seconds needed to start up the computer properly
safe_margin_startup=180

# set maximum hours EPG data available needed for updating automatic recorder schedule
# equivalent to maximum period system will not wake up if no future recording is detected
epg_hours=48

# set check for active users
# initial number of active users do-not-change
user_count=0
# uncomment next line to enable 
user_count=$(who | wc -l)

# set check for active clients
# this will only work properly with static ip-addresses and remember to exclude your tvheadend-server
# initial status clients do-not-change
clientA=0
clientB=0
clientC=0
clientD=0
clientE=0
clientF=0
clientG=0
clientH=0
clientI=0
clientJ=0
# uncomment up to next 10 lines and fill out the ip-addresses to be checked
clientA=$(ping -c1 192.168.1.9 | grep 'received' | awk -F ',' '{print $2}' | awk '{ print $1}')
clientB=$(ping -c1 192.168.1.6 | grep 'received' | awk -F ',' '{print $2}' | awk '{ print $1}')
clientC=$(ping -c1 192.168.1.8 | grep 'received' | awk -F ',' '{print $2}' | awk '{ print $1}')
clientD=$(ping -c1 192.168.1.10 | grep 'received' | awk -F ',' '{print $2}' | awk '{ print $1}')
# clientE=$(ping -c1 192.168.X.X | grep 'received' | awk -F ',' '{print $2}' | awk '{ print $1}')
# clientF=$(ping -c1 192.168.X.X | grep 'received' | awk -F ',' '{print $2}' | awk '{ print $1}')
# clientG=$(ping -c1 192.168.X.X | grep 'received' | awk -F ',' '{print $2}' | awk '{ print $1}')
# clientH=$(ping -c1 192.168.X.X | grep 'received' | awk -F ',' '{print $2}' | awk '{ print $1}')
# clientI=$(ping -c1 192.168.X.X | grep 'received' | awk -F ',' '{print $2}' | awk '{ print $1}')
# clientJ=$(ping -c1 192.168.X.X | grep 'received' | awk -F ',' '{print $2}' | awk '{ print $1}')

# set check for active post recording processes
# initial status processes do-not-change
processA=0
processB=0
processC=0
processD=0
processE=0
processF=0
processG=0
processH=0
processI=0
processJ=0
# uncomment up to next 10 lines and fill out the names of the processes to be checked
# processA=$(ps -A | grep 'process id' | wc -l)
# processB=$(ps -A | grep 'process id' | wc -l)
# processC=$(ps -A | grep 'process id' | wc -l)
# processD=$(ps -A | grep 'process id' | wc -l)
# processE=$(ps -A | grep 'process id' | wc -l)
# processF=$(ps -A | grep 'process id' | wc -l)
# processG=$(ps -A | grep 'process id' | wc -l)
# processH=$(ps -A | grep 'process id' | wc -l)
# processI=$(ps -A | grep 'process id' | wc -l)
# processJ=$(ps -A | grep 'process id' | wc -l)

# end CHECK THIS section

# some other initial values do-not-change
recording=0
current_date=`date +%s`
epg_secs=$((epg_hours*3600))
wake_date=$((current_date+epg_secs))

# routine to find out start and stop active recording and next recording
for i in $( ls ); do

# retrieve and calculate wake up data
program_start=`cat $i | grep '"start":' | cut -f 2 -d " " | cut -f 1 -d ","`
program_stop=`cat $i | grep '"stop":' | cut -f 2 -d " " | cut -f 1 -d ","`
recording_start_extra=$((`cat $i | grep '"start_extra":' | cut -f 2 -d " " | cut -f 1 -d ","`*60))
recording_stop_extra=$((`cat $i | grep '"stop_extra":' | cut -f 2 -d " " | cut -f 1 -d ","`*60))
shutdown_prevent=$((program_start-recording_start_extra-safe_margin_shutdown))
recording_end=$((program_stop+recording_stop_extra))
wake_date_tmp=$((program_start-recording_start_extra-safe_margin_startup))

# check for any running recording
if [ $((recording_end)) -gt $((current_date)) -a $((shutdown_prevent)) -lt $((current_date)) ]; then
recording=1
fi

# check for next recording and set wake up time
if [ $((wake_date_tmp)) -gt $((current_date)) -a $((wake_date_tmp)) -lt $((wake_date)) ]; then
wake_date=$((wake_date_tmp))
fi

done

# set wake up time
echo 0 > /sys/class/rtc/rtc0/wakealarm
echo $wake_date > /sys/class/rtc/rtc0/wakealarm

# check for active clients
client_count=$((clientA+clientB+clientC+clientD+clientE+clientF+clientG+clientH+clientI+clientJ))

# check for active post recording processes
process_count=$((processA+processB+processC+processD+processE+processF+processG+processH+processI+processJ))

# REPORTS after running the script manually

# about clients
if [ $((client_count)) -ne 0 ]; then
echo "Active clients detected" 
else
echo "No clients detected" 
fi 

# about users
if [ $((user_count)) -ne 0 ]; then
echo "Active users detected" 
else
echo "No users detected" 
fi 

# about recording
if [ $((recording)) -ne 0 ]; then
echo "Recording in progress" 
else
echo "Not recording" 
fi

# about processes
if [ $((process_count)) -ne 0 ]; then
echo "Active post recording processes detected" 
else
echo "No post recording processes detected" 
fi 

# about time
echo "Next wake-up @ `date -d @$wake_date +"%T %d.%m.%Y"`" 

# end of REPORTS

# final check will result in shutdown if
# no post recording processes present
# no clients present
# no users present
# no active recordings taking place
# no upcoming recording within safe_margin_shutdown detected
if [ $((process_count)) -eq 0 -a $((client_count)) -eq 0 -a $((user_count)) -eq 0 -a $((recording)) -eq 0 ]; then

sleep 10s
#/sbin/shutdown -h now
/usr/sbin/pm-suspend
fi

RE: Power-saving techniques - sleep - Added by Christian . about 8 years ago

I wrote a script to shutdown and wakeup the system based on Tvheadend activity. Is is based on systemd instead of pm-utils. You can find it on GitHub.

RE: Power-saving techniques - sleep - Added by Shane Angelo about 8 years ago

Hi Christian,

I tried your script and have the following errors when I run it manually:

AutoSuspend: Missing Tvheadend credentials (user and/or password)
/usr/local/sbin/autosuspend.sh: line 49: IsTvheadendBusy: command not found
/usr/local/sbin/autosuspend.sh: line 56: /usr/bin/smbstatus: No such file or directory

I followed the instructions here using Google translate to English: https://wiki.ubuntuusers.de/Skripte/AutoSuspend/

Files stored here:
tvheadend@tvheadend-OptiPlex-780:~$ ls /usr/local/sbin/
autosuspend.sh autosuspend.tvheadend-functions

The Tvheadend credentials are stored in/etc/autosuspend:

  1. User for access to the Tvheadend REST API
    #TVHEADEND_USER=<USER HERE>
#
  1. Password for access to the Tvheadend REST API
    #TVHEADEND_PASSWORD=<PASS HERE>

Looking forward to getting it sorted!

Just moved to tvheadend from Argus, this is my first post. The transition is a learning curve but has gone well thus far. Sleep is the next thing to nail down. Wondering if this should this be moved to another thread?

Cheer
Shane

RE: Power-saving techniques - sleep - Added by Christian . about 8 years ago

Hi Shane,

1.) Remove the leading hash character ('#') to activate your credentials, otherwise the line is interpreted as comment:

TVHEADEND_USER=<USER HERE>
TVHEADEND_PASSWORD=<PASS HERE>

instead of

#TVHEADEND_USER=<USER HERE>
#TVHEADEND_PASSWORD=<PASS HERE>

2.) The message

/usr/local/sbin/autosuspend.sh: line 56: /usr/bin/smbstatus: No such file or directory

indicates that the package samba is not installed on your machine. To fix this, enter

sudo apt-get install samba

Hope that helps.

RE: Power-saving techniques - sleep - Added by Shane Angelo about 8 years ago

Hi Christian,

The comment is embarrassing. My bad.

All sorted now and sleep/resume is working very well. All in all it was quite seamless to setup and it is a complete solution. The only complexity was my learning curve with the linux OS.

I did need to add systemd based scripts to stop/start tvheadend and reload the tuner modules because resume recordings were failing. Odd, manual ones were fine immediately after resume.

Thus far so good.

Thank you!

Cheers
Shane

RE: Power-saving techniques - sleep - Added by Shane Angelo about 8 years ago

Hi Christian,

Thank you for your script! It is working very well for suspend and resume and the system has not missed a recording.

I have one thought for a feature request. Are you able to put a method to wake at a scheduled time for the purpose of getting guide data? My resume script restarts TVH and TVH is configured to Force initial EPG scan at startup. I could add a dummy recording twice per day to force a wake up but a more elegant way would be to allow a wake for the period the TVH is grabbing EPG. I use OTA DVB EIT but would equally apply to any other format.

Shane

RE: Power-saving techniques - sleep - Added by Christian . about 8 years ago

I'm currently working on an extension of the script. Its purpose is to wake up the machine for other activities than Tvheadend recordings. I'll publish it once it's finished.

RE: Power-saving techniques - sleep - Added by Christian . about 8 years ago

I just uploaded an update of the autosuspend script supporting activity scripts. To try it, download the latest code from the repo and put an executable script into /etc/autosuspend.d/activities that outputs one or more lines containing a timestamp and an optional, colon-separated comment.

Example

$ sudo nano /etc/autosuspend.d/activities/epg-twice-a-day.sh

#!/bin/bash
echo $(date --date="2:00" +%s):EPG early
echo $(date --date="14:00" +%s):EPG late
echo $(date --date="tomorrow 2:00" +%s)
echo $(date --date="tomorrow 14:00" +%s)
$ sudo chmod +x /etc/autosuspend.d/activities/twice-a-day.sh

Additional resources

date: Getting next certain hour

RE: Power-saving techniques - sleep - Added by Pila RPi about 8 years ago

Anthony Thomas wrote:

So I've managed to make a script that does a reasonable job of putting the system to sleep when it isn't needed.
[...]
The structure/idea might be useful to someone!

I needed just a way to make my RPi LE TVH wakeup my server prior to recording. Your script worked great. I trimmed it a bit so it is down to just 2 lines of code, does not make any files and runs in 0,02 sec on an RP2. You did excellent job!

Made RPis send WOL to the server when they boot so there are no problems. Server goes to sleep in 30 minutes of inactivity on its own.

My only gripe is that I have a window of 5 minutes so it is possible to start a recording and my server may be sleeping. I do not want to make cron run more often.

RE: Power-saving techniques - sleep - Added by Shane Angelo about 8 years ago

Hi Christian,

Wow! that's fantastic.

To clarify, if I want EPG downloaded each day at 2am and 2pm my script would look like

#!/bin/bash
echo $(date --date="2:00" +%s):EPG early
echo $(date --date="14:00" +%s):EPG late

RE: Power-saving techniques - sleep - Added by Christian . about 8 years ago

You want an expression for something like the next 14:00, no matter if today or tomorrow. Unfortunately the date command does not support this. That's why you need all four lines from my example. Otherwise, if your machine shuts down at 15:00, both times 2:00 and 14:00 are in the past so that the autosuspend script would ignore them both.

RE: Power-saving techniques - sleep - Added by A L over 7 years ago

The tvheadend-autosuspend is exactly what I am looking for but I am really struggling to get it working (https://github.com/git-developer/autosuspend).

Like the author, I am running Debian Jessie (via Openmediavault), with TVHeadend installed on top.

As instructed, I copied the etc and usr/local/sbin to my system and then ran this:

[code]$ sudo apt-get install util-linux net-tools curl xmlstarlet jq[/code]

I then edited the autosuspend file in etc, changing the suspend method to suspend and also adding the TVHeadend username and password.

I rebooted the system but it never goes to sleep.

What am I doing wrong?

RE: Power-saving techniques - sleep - Added by Christian . over 7 years ago

Please check the system log under /var/log/syslog, you should see there why autosuspend refuses to suspend the system.

RE: Power-saving techniques - sleep - Added by Christian . over 7 years ago

You're probably missing the cron job that periodically runs the autosuspend script.
Run sudo crontab -e to edit the cron table and add the following line:

*/5  *   *   *   *    /usr/local/sbin/autosuspend.sh

This runs the autosuspend script every 5 minutes.

RE: Power-saving techniques - sleep - Added by A L over 7 years ago

Hello Christian.

Thank you so much for your help with this.

I added the cron job, as suggested, and my server went into sleep mode after a short period of inactivity - great start.

However, the server failed to wake up again for a scheduled recording.

I have been messing around with my server constantly, for the last few months, without really having any understanding of Linux. I am worried that I may have accidentally added/deleted/changed something that is preventing your add-on from working.

What I will do is completely wipe my system and start from scratch and see if that helps.

I shall report back here shortly with an update.

RE: Power-saving techniques - sleep - Added by A L over 7 years ago

Hi Christian,

After a full wipe and reinstall, my system does not seem to want to suspend now, no matter how long I leave it on.

[code]
root@openmediavault:/var/log# nano syslog
GNU nano 2.2.6 File: syslog

May 22 07:35:02 openmediavault rsyslogd: [origin software="rsyslogd" swVersion="8.4.2" x-pid="1001" x-info="http://www.rsyslog.com"] rsyslogd was HUPed
May 22 07:39:01 openmediavault rsyslogd0: action 'action 20' resumed (module 'builtin:ompipe') [try http://www.rsyslog.com/e/0 ]
May 22 07:39:01 openmediavault rsyslogd-2359: action 'action 20' resumed (module 'builtin:ompipe') [try http://www.rsyslog.com/e/2359 ]
May 22 07:39:01 openmediavault CRON15632: (root) CMD ( [ -x /usr/lib/php5/sessionclean ] && /usr/lib/php5/sessionclean)
May 22 07:40:01 openmediavault CRON15681: (root) CMD (/usr/local/sbin/autosuspend.sh)
May 22 07:45:01 openmediavault CRON15722: (root) CMD (/usr/local/sbin/autosuspend.sh)
May 22 07:45:01 openmediavault CRON15724: (root) CMD (/usr/sbin/omv-mkgraph >/dev/null 2>&1)
May 22 07:45:01 openmediavault rrdcached1000: Received FLUSHALL
May 22 07:45:09 openmediavault tvheadend1025: mpegts: 490MHz in DVB-T Network - tuning on Zarlink MT352 DVB-T #0 : DVB-T #0
May 22 07:45:09 openmediavault tvheadend1025: subscription: 027A: "192.168.1.5 [ alex | Kodi Media Center ]" subscribing on channel "CBeebies", weight: 15$
May 22 07:50:01 openmediavault CRON15836: (root) CMD (/usr/local/sbin/autosuspend.sh)
May 22 07:55:01 openmediavault CRON15876: (root) CMD (/usr/local/sbin/autosuspend.sh)
May 22 08:00:01 openmediavault CRON15917: (root) CMD (/usr/local/sbin/autosuspend.sh)
May 22 08:00:01 openmediavault CRON15919: (root) CMD (/usr/sbin/omv-mkgraph >/dev/null 2>&1)
May 22 08:00:01 openmediavault rrdcached1000: Received FLUSHALL
May 22 08:05:01 openmediavault CRON16029: (root) CMD (/usr/local/sbin/autosuspend.sh)
May 22 08:09:01 openmediavault CRON16062: (root) CMD ( [ -x /usr/lib/php5/sessionclean ] && /usr/lib/php5/sessionclean)
May 22 08:10:01 openmediavault CRON16112: (root) CMD (/usr/local/sbin/autosuspend.sh)
May 22 08:12:22 openmediavault tvheadend1025: tsfix: The timediff for MPEG2AUDIO is big (146929680), using current dts
May 22 08:15:01 openmediavault CRON16153: (root) CMD (/usr/sbin/omv-mkgraph >/dev/null 2>&1)
May 22 08:15:01 openmediavault CRON16154: (root) CMD (/usr/local/sbin/autosuspend.sh)
May 22 08:15:01 openmediavault rrdcached1000: Received FLUSHALL
May 22 08:17:01 openmediavault CRON16242: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
May 22 08:17:01 openmediavault postfix/postsuper16245: Deleted: 13 messages
May 22 08:19:13 openmediavault rrdcached1000: flushing old values
May 22 08:19:13 openmediavault rrdcached1000: rotating journals
[/code]

Does the above help to clarify what is going on?

RE: Power-saving techniques - sleep - Added by Christian . over 7 years ago

Unfortunately the message is missing in the log but at least we can see that autosuspend is running every 5 minutes. I believe that the EPG grabber is still active. On my system it took a few hours for the first time. You can check that on the tvheadend status page.
The next thing to check would be your rsyslog configuration, autosuspend logs detailed messages on INFO level.

    (1-21/21)