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.
Post Reply
Holland
Posts: 317
Joined: Friday 12 July 2013 13:53
Target OS: Raspberry Pi
Domoticz version: Beta Ch
Location: The Netherlands
Contact:

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

Post by Holland » Monday 07 March 2016 23:24

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 2 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 1) 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

1. Write a fresh image with Domoticz version 3530, http://sourceforge.net/projects/domoticz/files/

2. The following piece of the puzzle that is required is libcec 3.1.0, https://github.com/Pulse-Eight/libcec , but the libcec 3.1.0 is not supported on the distribution Raspian Wheezy. You need to install the newer version Jessie.

To upgrade Wheezy to Jessie, I followed this guide; https://linuxconfig.org/how-to-upgrade- ... le-release

Just issue the following;
# apt-get update
# apt-get upgrade
# apt-get dist-upgrade
# sed -i 's/wheezy/jessie/g' /etc/apt/sources.list
# apt-get update
# apt-get upgrade
# apt-get dist-upgrade
# reboot

3. After the reboot, start with the installation of libcec 3.1.0

a. Install required package to enables you to compile libcec

# sudo apt-get update
# sudo apt-get install cmake liblockdev1-dev libudev-dev libxrandr-dev python-dev swig
# sudo apt-get install --reinstall libraspberrypi0 libraspberrypi-dev libraspberrypi-doc libraspberrypi-bin
# cmake -DRPI_INCLUDE_DIR=/path/to/vc/include \
-DRPI_LIB_DIR=/path/to/vc/lib \
..
b. get libcec:

git clone git://github.com/Pulse-Eight/libcec.git

c. #force libcec to use c++11
edit the CMakeLists.txt
add following line:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

d. install
mkdir build
cd build
cmake -DRPI_INCLUDE_DIR=/opt/vc/include -DRPI_LIB_DIR=/opt/vc/lib -DCMAKE_INSTALL_PREFIX:PATH=/usr ..
make
sudo make install
sudo ldconfig

e. check if installation worked
cec-client -l

For the above I used 2 guides;
1. https://github.com/Pulse-Eight/libcec/issues/77
2. https://github.com/Pulse-Eight/libcec

4. 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.

5. 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.

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

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

8. 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

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

10. 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 12401 times
tv woonkamer.JPG
tv woonkamer.JPG (65.1 KiB) Viewed 12401 times
Last edited by Holland on Monday 23 January 2017 0:39, edited 4 times in total.

stlaha2007
Posts: 437
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 » Monday 07 March 2016 23:55

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: 56
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 » Saturday 22 October 2016 14:24

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: 317
Joined: Friday 12 July 2013 13:53
Target OS: Raspberry Pi
Domoticz version: Beta Ch
Location: The Netherlands
Contact:

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

Post by Holland » Saturday 22 October 2016 21:46

Scoobydoo, Thanks for reporting. Will add it to the guide.

juankar
Posts: 498
Joined: Saturday 30 August 2014 20:20
Target OS: Raspberry Pi
Domoticz version:
Location: Spain
Contact:

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

Post by juankar » Sunday 23 October 2016 23:23

Does it work with any Samsung model?

User avatar
Derik
Posts: 1890
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

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

Post by Derik » Monday 24 October 2016 19:24

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: 317
Joined: Friday 12 July 2013 13:53
Target OS: Raspberry Pi
Domoticz version: Beta Ch
Location: The Netherlands
Contact:

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

Post by Holland » Monday 24 October 2016 21:44

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 17:40
Target OS: Raspberry Pi
Domoticz version:
Contact:

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

Post by djansen1987 » Sunday 20 November 2016 17:43

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 17:40
Target OS: Raspberry Pi
Domoticz version:
Contact:

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

Post by djansen1987 » Sunday 20 November 2016 18:17


User avatar
capman
Posts: 167
Joined: Friday 12 July 2013 20:48
Target OS: NAS (Synology & others)
Domoticz version: v.3.8143
Location: Belgium
Contact:

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

Post by capman » Monday 02 January 2017 13:03

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 7958 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: 167
Joined: Friday 12 July 2013 20:48
Target OS: NAS (Synology & others)
Domoticz version: v.3.8143
Location: Belgium
Contact:

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

Post by capman » Monday 02 January 2017 18:54

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: 201
Joined: Saturday 19 December 2015 23:34
Target OS: Raspberry Pi
Domoticz version: 3.5837
Contact:

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

Post by squandor » Monday 16 January 2017 17:51

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: 110
Joined: Tuesday 19 January 2016 13:51
Target OS: -
Domoticz version:
Contact:

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

Post by toreandre » Wednesday 18 January 2017 11:49

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 22:27
Target OS: Raspberry Pi
Domoticz version:
Contact:

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

Post by giova » Sunday 22 January 2017 22:38

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: 317
Joined: Friday 12 July 2013 13:53
Target OS: Raspberry Pi
Domoticz version: Beta Ch
Location: The Netherlands
Contact:

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

Post by Holland » Monday 23 January 2017 0:41

@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 22:27
Target OS: Raspberry Pi
Domoticz version:
Contact:

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

Post by giova » Monday 23 January 2017 0:46

ok i see now, thanks for the tip !

pvangorp
Posts: 121
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi
Domoticz version: Latest B
Location: The Netherlands
Contact:

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

Post by pvangorp » Tuesday 29 August 2017 20:20

Rrrrrr

User avatar
sincze
Posts: 729
Joined: Monday 02 June 2014 22:46
Target OS: Linux
Domoticz version: 3.8153
Location: Netherlands
Contact:

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

Post by sincze » Tuesday 29 August 2017 21:10

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.
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

ilpol984
Posts: 6
Joined: Monday 25 July 2016 10:28
Target OS: Raspberry Pi
Domoticz version:
Contact:

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

Post by ilpol984 » Friday 15 September 2017 18:12

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

Inviato dal mio ONE A2003 utilizzando Tapatalk


Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests