Switch ON Philips TV with HDMI CEC via Kodi

exec
openhab2
kodi
Tags: #<Tag:0x00007f1e5b7062c0> #<Tag:0x00007f1e5b706180> #<Tag:0x00007f1e5b706040>
(Maurits) #1

So my Philips TV (2010) has limited interaction possibilities, I use a few via the Jointspace binding. I cannot switch on the TV as Jointspace is only capable of sending commands when the TV is on. However the TV supports the CEC protocol via HDMI (more info: wiki).

My Media/TV setup: RPi 3 with LibreElec installed running kodi v16.x. The RPi and Kodi support sending CEC commands to the TV. I use the XBMC binding to interact with Kodi, which all works well. The 1.x XBMC binding supports Input.ExecuteAction, but cannot execute built-in Kodi functions as far as I understand.

I made it work by using the exec binding:

rule "Switch the TV on via CEC/Kodi"
when
	Item TVOn received update ON
then {
	logInfo("RULE", "TV is switched on")
	val String FeedbackScript = executeCommandLine('ssh@@root@192.168.xx.xx@@`echo@@"on@@0"@@|@@cec-client@@-s`', 10000)
	logInfo("RULE", FeedbackScript)
	}
end

A prerequisite is that you don’t need a password when using the ssh command, by shared keys. Also you need to add ssh to visudo.

But to me this looks like a suboptimal solution and I would like to understand the following:

  1. Is there a smarter way to execute a Kodi built-in function like CECActivateSource? Kodi wiki.

Thanks.

Btw, this is a good post to sort out any troubles with the exec binding, it helped me a lot.

https://community.openhab.org/t/how-to-solve-exec-binding-problems/18131

EDIT: Updated by removing the script I used to circumvent the non-functioning of the exec binding directly from openHAB.

1 Like

Lookin for ideas to power on TV
Hdmi cec binding
(HG) #2

Thanks for this tutorial, but unfortunately I am not able to set it up correctly.

I am running openHAB 2.1.0-1 (Release Build) on openHABianPi, my media center is libreELEC 8.0.2. Both are running on RPi 2.

In my .items I defined my Philips TV as follows:

Switch Fernseher "Fernseher" ["Lighting"]

I also added a rule following your example:

rule "Switch the TV on via CEC/Kodi"
when
	Item Fernseher received update ON
then {
	logInfo("RULE", "TV is switched on")
	val String FeedbackScript = executeCommandLine('ssh@@root@192.168.X.Y@@`echo@@"on@@0"@@|@@cec-client@@-s`', 10000)
	logInfo("RULE", FeedbackScript)
	}
end

I then followed this to generate a key pair.

On openHABian I added via

sudo visudo

the following lines

openhab    ALL=(ALL:ALL) ALL
openhabian    ALL=(ALL:ALL) ALL
sudo ALL=NOPASSWD: ALL

However, whenever I activate the toggle switch to switch on the TV, nothing happens and I get the following message in the log:

2017-07-24 19:09:48.475 [INFO ] [.eclipse.smarthome.model.script.RULE] - Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,password,keyboard-interactive).

However, when I ssh on the libreELEC RPi from the openHABian RPi, I am able to start the TV via the command line

echo "on 0" | cec-client -s

I believe I have a permissions problem, but I did not figure out where. My assumption is that I did not correctly “add ssh to visudo”.

As I am new to the forum, I hope that my problem description is precise enough. Any help on this issue is highly appreciated.

Thanks!

0 Likes

(HG) #3

I figured out my mistake. In fact I generated the key pair with the openhabian user instead of the openhab user. Just in case anybody is running in the same problem.

0 Likes

(Maurits) #4

Hi HG,

Good that you could solve it, I’ve been not able lately to spend anytime on the forum or on openHAB for that matter.

Cheers.

0 Likes

(Markus Storm) #5

Just a simple (hopefully not blasphemic) question: why don’t you have the TV turn on automatically on CEC ?
You should be able to configure Kodi to send CEC on (Kodi) UI interaction or similar (I think it does anyway).
I’ve got a similar setup, just I have another media player (Dreambox TV receiver) to send CEC when you turn it on, so that’s how I start my combo.
No other chance anyway since I’ve got one of those new Samsung TVs that aren’t supported by the binding yet.

0 Likes

(HG) #6

The simple reason is that the Raspberry running Kodi is permanently powered on because it also serves as a tvheadend server for another client. That’s why I need to be able to switch the TV separately. Secondly, I wanted to be able to tell Alexa to turn on the TV.

0 Likes

(Kevin) #7

Try this :slight_smile:

rule "Switch the TV on via CEC/Kodi"
when
  Item TVOn received update ON
then
  executeCommandLine("sudo /etc/openhab2/scripts/switchTVon.sh")
end

//BASHSCRIPT -> switchTVon.sh

#!/bin/bash
ssh root@192.168.xx.xx 'echo "on 0" | cec-client -s -d 1'
0 Likes

(Kevin) #8

This is not the best Configuration :wink:

openhab ALL = (root) NOPASSWD: /etc/openhab2/scripts/switchTVon.sh

This is enough rights for the Openhab User , if you followed my rule

0 Likes

(gert_konemann) #9

Can you publish the shell script too ?

Gert

0 Likes

(Kevin) #10

See here :wink:

0 Likes

(gert_konemann) #11

Thanks. I did not see that, because I expected a long script.

Gert

0 Likes

(Jerome Luckenbach) #12

Can someone point me to the right direction.
How can I add those keys for the openhab user?!
I am not able to switch to that user and run the keygen and i have no idea where i should copy my keys according to How to solve Exec binding problems

It is maybe ultra easy but i am a bit lost now.

0 Likes

(Jerome Luckenbach) #13

Forget it.
It seems there is a mistake in that exec binding problems thread.
Running now for me.

I will add a comment there, to discuss the possible mistake.

0 Likes