Step by step guide: switch on and off tv via cec

In this subforum you can show projects you have made, or you are busy with. Please create your own topic.

Moderator: leecollings

Holland
Posts: 179
Joined: Friday 12 July 2013 13:53
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta Ch
Location: The Netherlands
Contact:

Step by step guide: switch on and off tv via cec

Post by Holland »

Updated on April, 2019

If you want to switch off and on a ( in my case Samsung) tv via home automation, chances are that you run into several issues. It's not an straightforward task, also since WOL is not supported by Samsung (2015 tv).
Please find below a step by step guide to, which enables you to switch off and on the tv via a Domoticz virtual switch. In addition, the switch follows closely the state of the tv, so the button is off when the tv is off, even when you have used the rc to switch off the tv.

There are several solution that could or will work;
a. Use a Logitech Harmony Hub. I didn't want to go this way, given that you need to place a infrared sensor somewhere in the neighborhood of the tv. In addition my samsung remote control uses RF, and I read somewhere that the HH can't send and receive RF at the same time. So the HH solution is a no go for me.

b. I use a raspberry 3 with OSMC (based on Kodi) as a mediaplayer. And since the pi supports CEC you could via CEC, by issuing "echo "standby 0" | cec-client -s -d 1 and echo "on 0" | cec-client -s -d 1" switch off and on a tv that also supports CEC. The problem with this solution is that by issuing the before mentioned command the CEC connection between the OSMC and the tv is disconnected, which results that you can no longer use the tv remote control to use the OSMC media player. Therefore using the Pi OSMC CEC solution is a no go.

c. then the logical solution is to connect a separate pi (in my case an old Pi 2) to the tv, just for CEC support. This is the solution that I prefer, and the step by step guide is available below;

# Setup the pi (Based on Strech version April 2019)

1. Update the Pi to the latest and reboot (new firmware and dbus daemon)

Code: Select all

sudo apt-get update
sudo apt-get upgrade
sudo reboot
a. Install Domoticz

Code: Select all

sudo curl -L install.domoticz.com | bash
Run updatebeta, otherwise the following will pop up;
error while loading shared libraries: libssl.so.1.0.0: cannot open shared object file: No such file or directory problem
https://www.domoticz.com/forum/viewtopi ... 2&start=40

b. Install some required components

Code: Select all

sudo apt-get install cmake libudev-dev libxrandr-dev python-dev swig
c. Prepare cec-client environment

Following is copied from https://github.com/home-assistant/hassb ... /issues/38 (@john-ruddy)

Code: Select all

cd
git clone https://github.com/Pulse-Eight/platform.git
mkdir platform/build
cd platform/build
cmake ..
make
sudo make install
d. Compile cec-client, takes about 3 min on a Pi 2

Code: Select all

cd
git clone https://github.com/Pulse-Eight/libcec.git
mkdir libcec/build
cd libcec/build
cmake -DRPI_INCLUDE_DIR=/opt/vc/include -DRPI_LIB_DIR=/opt/vc/lib ..
make -j4
sudo make install
sudo ldconfig
e. Test

Code: Select all

cec-client -l
Output, in case it works :)

Code: Select all

libCEC version: 4.0.4, git revision: libcec-4.0.4+1-ba9b538, compiled on Thu 11 Apr 22:35:24 UTC 2019 by pi@cec-pi on Linux 4.14.98-v7+ (armv7l), features: P8_USB, DRM, P8_detect, randr, RPi
Found devices: 1

device:              1
com port:            RPI
vendor id:           2708
product id:          1001
firmware version:    1
type:                Raspberry Pi


2. Setup the Domoticz config on the CEC pi

a. Create a dummy switch on the CEC pi.

b. create 2 python scripts and install those in the scripts/python directory. Also make both scripts executable, with sudo chmod +x filename.py

File 1 tv on
-------
#!/usr/bin/python3
import os
os.system("date")
os.system('echo "on 0" | cec-client -s -d 1')
--------------------

File 2 tv off
-------
#!/usr/bin/python3
import os
os.system("date")
os.system('echo "standby 0" | cec-client -s -d 1')
--------------------

c. Link the scripts with the switch via:

On action: script:///home/pi/domoticz/scripts/python/tv_woonkamer_on.py
Off action: script:///home/pi/domoticz/scripts/python/tv_woonkamer_off.py

d. the cec pi is now done. Also write down the idx number of the dummy switch, mine is 1.

3. Create a system alive check checker (add via hardware in Domoticz: System Alive Checker) on the main Domoticz installation (production environment in my case)
The ping should be directed to the ip address of the tv, with a timeout of 30 sec. To check if the tv is on or off.

4. Create a dummy switch e.g TV Woonkamer.

5. Create a dummy switch e.g TV Woonkamer.tv.script attached

6. Link the on and off actions ( via JSON), of the TV woonkamer switch to the TV Woonkamer.tv.script attached
switch

http://ip 1:8080/json.htm?type=command&param=switchlight&idx=95&switchcmd=On
http://ip 1:8080/json.htm?type=command&param=switchlight&idx=95&switchcmd=Off

7. Make the system alive checker device slave (wk.tv.local system alive) of the TV Woonkamer switch.

8. Link the on and off actions ( via JSON) of the TV Woonkamer.tv.script attached switch to the switch on the CEC pi

http://ip 2:8080/json.htm?type=command&param=switchlight&idx=1&switchcmd=On
http://ip 2:8080/json.htm?type=command&param=switchlight&idx=1&switchcmd=Off

That's it. I have it working now for a few weeks and it's rock stable.
Attachments
tv woonkamer scripted attached.JPG
tv woonkamer scripted attached.JPG (61.23 KiB) Viewed 27587 times
tv woonkamer.JPG
tv woonkamer.JPG (65.1 KiB) Viewed 27587 times
Last edited by Holland on Friday 12 April 2019 0:39, edited 10 times in total.
stlaha2007
Posts: 370
Joined: Monday 05 October 2015 10:16
Target OS: -
Domoticz version:
Contact:

Re: Step by step guide: switch on and off tv via cec

Post by stlaha2007 »

Nice solution...

However my tv is just a display connected to my av-receiver. How about this config? Both av-receiver and tv are CEC compliant...

On the other hand, when you are using a second pi with Jessie, drop in kodi itself on it with apt-get install kodi...
Have used OSMC and OpenElec, but missing extras to install like ser2net.

Gonna look into your solution for my config, as i want some scenes to handle a WOL-less Sony TV, so this hack can probably do this for me to.

Grtz,
Stephan
Scoobydoo
Posts: 54
Joined: Tuesday 16 July 2013 12:20
Target OS: -
Domoticz version:
Contact:

Re: Step by step guide: switch on and off tv via cec

Post by Scoobydoo »

Thanks for the instructions.

Took a while but finally got it to build.

I found I had to add:-

sudo apt-get install --reinstall libraspberrypi0 libraspberrypi-dev libraspberrypi-doc libraspberrypi-bin

at the beginging

If i didnt do this then the build failed on the make command in the libcec/build folder.
Holland
Posts: 179
Joined: Friday 12 July 2013 13:53
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta Ch
Location: The Netherlands
Contact:

Re: Step by step guide: switch on and off tv via cec

Post by Holland »

Scoobydoo, Thanks for reporting. Will add it to the guide.
juankar
Posts: 221
Joined: Saturday 30 August 2014 20:20
Target OS: Linux
Domoticz version: 4.
Location: Spain
Contact:

Re: Step by step guide: switch on and off tv via cec

Post by juankar »

Does it work with any Samsung model?
Derik
Posts: 1601
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Step by step guide: switch on and off tv via cec

Post by Derik »

great job...
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
Holland
Posts: 179
Joined: Friday 12 July 2013 13:53
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta Ch
Location: The Netherlands
Contact:

Re: Step by step guide: switch on and off tv via cec

Post by Holland »

Post by juankar » Sun Oct 23, 2016 10:23 pm
Does it work with any Samsung model?
If your samsung supports Anynet+(HDMI-CEC) then its likely it will work. Make sure that you enable it in the setup menu of your tv.

Anynet+ is Samsung's implementation of CEC
djansen1987
Posts: 2
Joined: Sunday 20 November 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Step by step guide: switch on and off tv via cec

Post by djansen1987 »

Hi The manual is outdated. Install of libCEC didn't work for me anymore.

find the new install:
To compile libCEC on a new Raspbian installation, follow these instructions:

Code: Select all

sudo apt-get update
sudo apt-get install cmake libudev-dev libxrandr-dev python-dev swig
cd
git clone https://github.com/Pulse-Eight/platform.git
mkdir platform/build
cd platform/build
cmake ..
make
sudo make install
cd
git clone https://github.com/Pulse-Eight/libcec.git
mkdir libcec/build
cd libcec/build
cmake -DRPI_INCLUDE_DIR=/opt/vc/include -DRPI_LIB_DIR=/opt/vc/lib ..
make -j4
sudo make install
sudo ldconfig


ref:
https://github.com/Pulse-Eight/libcec/b ... berrypi.md
djansen1987
Posts: 2
Joined: Sunday 20 November 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Step by step guide: switch on and off tv via cec

Post by djansen1987 »

User avatar
capman
Posts: 153
Joined: Friday 12 July 2013 20:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Belgium
Contact:

Re: Step by step guide: switch on and off tv via cec

Post by capman »

djansen1987 wrote:quick tip for the LG tv users

https://www.domoticz.com/wiki/LG_2012_smart_tv
When a first run this script a get following errors in putty.
lg.jpg
lg.jpg (52 KiB) Viewed 23146 times
A must say that a run domoticz on my synology nas. Python is installed because a can run other python scripts with domoticz.
A have my ip adress from my lg television inserted.

Note: when a searching in the forum with the keywords 'lg' , then there 's no search because of two letters. Also 'tv' ;)
User avatar
capman
Posts: 153
Joined: Friday 12 July 2013 20:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Belgium
Contact:

Re: Step by step guide: switch on and off tv via cec

Post by capman »

Probarly it's not working with the webOS 3.
Dutch: https://gathering.tweakers.net/forum/li ... 9#49161059 :(
Someone have it already working ?
squandor
Posts: 213
Joined: Saturday 19 December 2015 22:34
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5837
Contact:

Re: Step by step guide: switch on and off tv via cec

Post by squandor »

capman wrote:
djansen1987 wrote:quick tip for the LG tv users

https://www.domoticz.com/wiki/LG_2012_smart_tv
When a first run this script a get following errors in putty.
lg.jpg

A must say that a run domoticz on my synology nas. Python is installed because a can run other python scripts with domoticz.
A have my ip adress from my lg television inserted.

Note: when a searching in the forum with the keywords 'lg' , then there 's no search because of two letters. Also 'tv' ;)
Thats because you are trying to run an python script with shell (sh)
try to run it using the following command:

Code: Select all

python lgtv2012.py
toreandre
Posts: 91
Joined: Tuesday 19 January 2016 12:51
Target OS: -
Domoticz version:
Contact:

Re: Step by step guide: switch on and off tv via cec

Post by toreandre »

Holland wrote:If you want to switch off and on a ( in my case Samsung) tv via home automation, chances are that you run into several issues. It's not an straightforward task, also since WOL is not supported by Samsung (2015 tv).
Please find below a step by step guide to, which enables you to switch off and on the tv via a Domoticz virtual switch. In addition, the switch follows closely the state of the tv, so the button is off when the tv is off, even when you have used the rc to switch off the tv.
Will this work when connected to AV receiver?
The setup will look like this "Pi Zero -> AV receiver -> TV"
Both the tv and receiver is HDMI CEC devices.
giova
Posts: 7
Joined: Sunday 22 January 2017 21:27
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Step by step guide: switch on and off tv via cec

Post by giova »

Thanks to your tutorial, i can now switch TV ON/OFF
i also made a bash script to find tv's ip and test if it is turned on or off.

Now, i didn't understood the last part of your tuto regarding Woonkamer.tv.script device

May you please give us more details regarding this?

can the alive check script be a .sh?
or perhaps it must be a script_timeXXXX file... then why not let it set the parent device (idx 1 for you) ON or OFF depending of ping result?
well i don't see what is the best solution to attach it to the dummy Woonkamer.tv.script device...

Thanks again.
Holland
Posts: 179
Joined: Friday 12 July 2013 13:53
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta Ch
Location: The Netherlands
Contact:

Re: Step by step guide: switch on and off tv via cec

Post by Holland »

@giova

You don't need a bash script to check if a system is switched on.

Add via hardware in Domoticz: System Alive Checker

Hopefully this helps to take you to the next step
giova
Posts: 7
Joined: Sunday 22 January 2017 21:27
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Step by step guide: switch on and off tv via cec

Post by giova »

ok i see now, thanks for the tip !
pvangorp
Posts: 70
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Step by step guide: switch on and off tv via cec

Post by pvangorp »

Rrrrrr
User avatar
sincze
Posts: 1299
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: Step by step guide: switch on and off tv via cec

Post by sincze »

Whoehoe Very nice solution. :D

I solved it in a different way using https://github.com/joshjowen/script.json-cec, as an addon for kodi.

In my case... If I switch on the Fan above the stove....
- it switches on my tv (as kodi is always running on my non illegal native mediaplayer (libreelec).
- And it starts playing radio using the radio plugin of kodi.

WAF factor +100 kudos for me now.
As we don't have SONOS money.
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
ilpol984
Posts: 7
Joined: Monday 25 July 2016 10:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Step by step guide: switch on and off tv via cec

Post by ilpol984 »

I've a big problem. My TV pings even if it is off...

Inviato dal mio ONE A2003 utilizzando Tapatalk

Holland
Posts: 179
Joined: Friday 12 July 2013 13:53
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta Ch
Location: The Netherlands
Contact:

Re: Step by step guide: switch on and off tv via cec

Post by Holland »

I updated the guide as a power outage killed the SD card. The pi cec has been running for almost 2 years without any issues

Now based on Stretch Nov 17 and libcec 4.02.

Enjoy
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest