Skip to content

Instantly share code, notes, and snippets.

@matiaskorhonen
Created January 28, 2016 16:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matiaskorhonen/738fd698ac7a7a73ca14 to your computer and use it in GitHub Desktop.
Save matiaskorhonen/738fd698ac7a7a73ca14 to your computer and use it in GitHub Desktop.
A quick script to control the power state of a TV attached to a Raspberry Pi over HDMI. Requires `cec-utils` to be installed.
#!/usr/bin/env bash
iso8601_date () {
date +%Y-%m-%dT%H:%M:%S%z
}
turn_on () {
echo 'on 0' | cec-client -s -d 1 RPI
}
turn_off () {
echo 'standby 0' | cec-client -s -d 1 RPI
}
args="$1"
case $args in
on)
echo "[$(iso8601_date)]: Turning the TV on"
turn_on
;;
off)
echo "[$(iso8601_date)]: Turning the TV off"
turn_off
;;
auto)
echo "[$(iso8601_date)]: Checking the TV power state"
if [[ $(echo 'pow 0' | cec-client -s -d 1 RPI) =~ "power status: standby" ]]; then
echo "[$(iso8601_date)]: The TV appears to be off, turning it on now"
turn_on
else
echo "[$(iso8601_date)]: The TV appears to be on, turning it off now"
turn_off
fi
;;
*)
echo "Usage:"
echo " toggle-tv-power on|off|auto"
exit 1
esac
echo "---"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment