Skip to content

Control TV over HDMI

Peter Uithoven edited this page Dec 21, 2024 · 1 revision

A Raspberry PI can automatically turn a TV that's connected over HDMI on and off using HDMI-CEC. More info: https://pimylifeup.com/raspberrypi-hdmi-cec/

Configure HDMI output

You might need to configure the Pi to always output HDMI so do this add these two lines to /boot/config.txt and reboot:

hdmi_force_hotplug=1
hdmi_drive=2

More info: https://raspberrypi.stackexchange.com/a/2171

Install CEC client

sudo apt install cec-utils

Test

Put TV into stand-by Run the following command to turn it on: echo 'on 0.0.0.0' | cec-client -s -d 1

Automatically execute on boot

Create Systemd service to turn the tv on when booting: sudo nano /etc/systemd/system/tv-on.service

[Unit]
Description=Turn TV on using cec-client
After=network.target

[Service]
Type=simple
ExecStart=/bin/bash -c "echo 'on 0.0.0.0' | cec-client -s -d 1"

[Install]
WantedBy=multi-user.target

Enable service: sudo systemctl enable tv-on

Automatically turn tv off on a set time

Edit cronjobs: crontab -e:

0 18 * * * echo 'standby 0.0.0.0' | /usr/bin/cec-client -s -d 1
5 18 * * * sudo /sbin/shutdown -h now >/dev/null 2>&1

This does: 18:00: Put the tv into standby mode 18:15: Gracefully shutdown

Alternatively you could also use systemd timers https://opensource.com/article/20/7/systemd-timers