Skip to content

Instantly share code, notes, and snippets.

@SirCrocker
Last active April 5, 2023 11:07
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save SirCrocker/937d06f8f2c1227a83802b9b77f60ba0 to your computer and use it in GitHub Desktop.
Save SirCrocker/937d06f8f2c1227a83802b9b77f60ba0 to your computer and use it in GitHub Desktop.
Turn off and on the HDMI port and the TV. Options: on, off & status.
#!/bin/sh
#Enable and disable TV/HDMI
hdmi_status ()
{
vcgencmd display_power | grep "display_power=0" >/dev/null
}
cec_status ()
{
echo pow 0 | cec-client -s -d 1 | grep "power status: standby" >/dev/null
}
case $1 in
on)
echo on 0 | cec-client -s -d 1 | grep "opening a connection to the CEC adapter..." >/dev/null
vcgencmd display_power 1 | grep "display_power=1" >/dev/null
echo "Display and HDMI have been turned on by user."
;;
off)
echo standby 0 | cec-client -s -d 1 | grep "opening a connection to the CEC adapter..." >/dev/null
vcgencmd display_power 0 | grep "display_power=0" >/dev/null
echo "Display and HDMI have been turned off by user."
;;
status)
if hdmi_status
then
echo "The HDMI port is OFF"
else
echo "The HDMI port is ON"
fi
if cec_status
then
echo "The display is turned OFF"
else
echo "The display is turned ON"
fi
;;
*)
echo "Usage: $0 on|off|status" >&2
exit 2
;;
esac
exit 0
@SirCrocker
Copy link
Author

SirCrocker commented Mar 8, 2019

I took the original idea from this gist.

Options

On: turns on the display and the HDMI port.
Off: turns off the display and the HDMI port.
Status: prints the currents status of the display and HDMI port.

Important

For it to work your display must have CEC and you need to install cec-utils, you can do the latter by running sudo apt-get install cec-utils.

(If your display doesn't have CEC you can delete lines: 10, 11, 12, 13, 17, 22, 34, 35, 36, 37, 38 & 39 to make it work without using CEC).

Sources: Rpi CEC - HowTo Shell Script - vcgencmd

@JMS1717
Copy link

JMS1717 commented Apr 4, 2019

So I was having a problem with my previous rpi-hdmi-tv.sh so can I just copy and paste this in that file in that file and it will work?

@SirCrocker
Copy link
Author

SirCrocker commented Apr 5, 2019

Yes, if you installed cec-utils it should work. For me at least, its been working with no problems.

@Some1Elsewhere
Copy link

works perfectly. Thank you!

@TheDarkerPath
Copy link

Works perfectly, thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment