Skip to content

Instantly share code, notes, and snippets.

@SirCrocker SirCrocker/rpi-hdmi-tv.sh
Last active Oct 2, 2019

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner 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

This comment has been minimized.

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

This comment has been minimized.

Copy link
Owner 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

This comment has been minimized.

Copy link

Some1Elsewhere commented Jul 31, 2019

works perfectly. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.