Page 1 of 2

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

Posted: Monday 07 March 2016 22:24
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.

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

Posted: Monday 07 March 2016 22:55
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

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

Posted: Saturday 22 October 2016 14:24
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.

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

Posted: Saturday 22 October 2016 21:46
by Holland
Scoobydoo, Thanks for reporting. Will add it to the guide.

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

Posted: Sunday 23 October 2016 23:23
by juankar
Does it work with any Samsung model?

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

Posted: Monday 24 October 2016 19:24
by Derik
great job...

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

Posted: Monday 24 October 2016 21:44
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

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

Posted: Sunday 20 November 2016 16:43
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

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

Posted: Sunday 20 November 2016 17:17
by djansen1987

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

Posted: Monday 02 January 2017 12:03
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 23623 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' ;)

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

Posted: Monday 02 January 2017 17:54
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 ?

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

Posted: Monday 16 January 2017 16:51
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

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

Posted: Wednesday 18 January 2017 10:49
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.

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

Posted: Sunday 22 January 2017 21:38
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.

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

Posted: Sunday 22 January 2017 23:41
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

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

Posted: Sunday 22 January 2017 23:46
by giova
ok i see now, thanks for the tip !

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

Posted: Tuesday 29 August 2017 20:20
by pvangorp
Rrrrrr

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

Posted: Tuesday 29 August 2017 21:10
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.

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

Posted: Friday 15 September 2017 18:12
by ilpol984
I've a big problem. My TV pings even if it is off...

Inviato dal mio ONE A2003 utilizzando Tapatalk


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

Posted: Thursday 22 February 2018 9:03
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