Skip to content

Instantly share code, notes, and snippets.

@jak
Created May 21, 2018 08:01
Show Gist options
  • Save jak/924212709825d0273b65d04592a8d7d4 to your computer and use it in GitHub Desktop.
Save jak/924212709825d0273b65d04592a8d7d4 to your computer and use it in GitHub Desktop.
Raspberry Pi HDMI CEC
#!/bin/bash
echo "as" | cec-client -s
#!/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