Exec, HDMI-CEC and powering on/off the TV

  • Platform information:
    • Hardware: Raspberry Pi 4 (4gb)
    • OS: raspbian 4.19.75-v7l+
    • Java Runtime Environment: Zulu8.40.0.178-CA-linux_aarch32hf
    • openHAB version: 2.5.1~20200109043448-1, snapshot

Im trying to send on/off-commands to my toshiba TV via HDMI-cec and exec.
Im able to turn on/off the tv from a bash terminal using the following two commands:
echo “on 0” | cec-client RPI -s -d 1
echo “standby 0” | cec-client RPI -s -d 1

My goal is to be able to turn OFF the tv with my group switch AllOff, as well as turning ON the tv from a dedicated ON-(only)button

But im unable to get it to work thru openHAB due to gross incompetence…


Thing exec:HDMI-CEC:TV [ command="echo “%2$s” | cec-client -s", interval=0, autorun=false ]


String tv-command (gHDMI-CEC) {channel="exec:HDMI-CEC:TV:input"}


sitemap exec label="HDMI CEC"
	Frame { Switch item=tv-command label="TV_pwr" mappings=["on"="on 0","off"="standby 0"] }


2020-01-10 23:20:58.436 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'exec.sitemap' has errors, therefore ignoring it: [3,24]: mismatched input '-' expecting '}'
[4,1]: extraneous input '}' expecting EOF


The error message is pretty clear.

The sitemap file is ignored through a mismatched input of the character -,
which you have used in your item.

According to

The only characters permitted in an Item name are letters, numbers and the underscore character.

So the solution would be to rename your item to tv_command for example.

I fixed the naming issue, i now have a exec-sitemap in the openhab app on my phone, that does have one entry with two buttons, on and off.

The logs do report buttons being pressed, but the TV stays on…


Thing exec:HDMICEC:TV [ command="echo “%2$s” | cec-client -s", interval=0, autorun=true ]


String tvCommand (gHDMICEC) {channel="exec:HDMICEC:TV:command"}


rule "execRules"
		Item tvCommand received command
	if (receivedCommand==ON ) {
		executeCommandLine("echo on 0 | cec-client -s -d 1")
	else if (receivedCommand==OFF ) {
		executeCommandLine("echo standby 0 | cec-client -s -d 1")


sitemap exec label="HDMI CEC"
	Frame { Switch item=tvCommand label="TV_pwr" mappings=["ON"="on","OFF"="off"] }


2020-01-11 20:05:40.571 [ome.event.ItemCommandEvent] - Item 'tvCommand' received command OFF
2020-01-11 20:05:40.576 [nt.ItemStatePredictedEvent] - tvCommand predicted to become NULL

I am really no expert for the exec binding ( i don’t use it), but from a short look at https://www.openhab.org/addons/bindings/exec/#full-example it seems that you have some unnecessary quotes in your thing definition.

Could you try the following change in your thing file:

Thing exec:HDMICEC:TV [ command="echo %2$s | cec-client -s", interval=0, autorun=true ]

Notice the %2$s without the quotes.

That would be my first try based on the quick docs look.

Thank you kindly for your input!
However, that did not make any difference… :frowning:

Im not sure if the autorun boolean should be true or false ether, tried both with the same result…

One moment.
I think did oversee something too.
You are doing same things twice if i recognize this right.
(If yes this would not be necessary.)

When you define the command in your thing definition,
there should be no need for the complete rule file.
No idea how i did oversee this so far…

Could you try removing the rule completely for now and then edit the Switch in the sitemap as following:

Switch item=tvCommand label="TV_pwr" mappings=["ON"="on 0","OFF"="standby 0"]

I kind of did what you asked, renamed the exec.rules to exec.rules.off
…hope that´s good enough… :slight_smile:

I did update the sitemap too, now my phone shows 2 buttons on the sitemap, “on 0” and “standby 0”.
Nothing happens when i click the buttons tho…
I figure it should be the other way around, so i´ll try that too.

Should be. :slight_smile:

I think your openHAB reated confiogurations should fit somehow.
There are problems and hurdles with the exec binding (at least in the older v1 version) that could cause problems.
There is a tutorial about that somewhere.

@rlkoshak can you say something about those problems in the OH 2 binding?
Are the permission issues valid for this too?

I tried re-arranging “on 0”=“ON” but still no luck…

However, i did come up with an idea…
According to the exec “manual”, i should try my command in a terminal with the openhab user:

sudo -u openhab <YOUR COMMAND>

So i tried that a few times, but it appears as if the openhab user isnt allowed to use the cec-client or something, terminal returns nothing at all after entering:

sudo -u openhab echo "standby 0 | cec-client RPI -s -d 1

for troubleshooting reasons, i tried:

sudo -u openhab ls

and that returned a list of items in the current directory.

This leads me to belive that openHAB and exec is working just fine, but the problem is my manual installation and/or execute permissions for cec-client…
I´ll go find a linux forum and beg for help there, but in case we have a linux guru reading this; if you know how to fix this, please share! pretty please with sugar on top! :slight_smile:

Or you stay in this wonderful forum first and try to solve it with the mentioned thread. :slight_smile:

I have found it:

1 Like

Thank you for finding that link for me!
Ive read thru it but unfortunately my linux skills are rudimentary at best so im not too sure how to use the info to solve my problem.

However, ive started to investigate the possible permissions issue and i gathered the group memberships for both the pi user and the openhab user.

[23:07:19] pi@openHABserver:~$ groups pi
pi : pi adm tty dialout cdrom sudo audio video plugdev games users input netdev bluetooth spi i2c gpio openhab
[23:07:23] pi@openHABserver:~$ groups openhab
openhab : openhab tty dialout audio bluetooth gpio

I also added a sudoers entry for the user openhab, just in case that would make any difference, but still no go…

Exec binding is hard and it’s doubly hard if you don’t know the OS you are testing to execute the commands on.

It’s not clear if you ever got the command to run as the openhab user. If not that’s your first job, and frankly it’s outside of OH so the answer to that might be on some other forum it Google search.

Once it works there than you should just need to call it. You might need to separate the arguments to the command using @@ instead of spaces.

I don’t use the exec binding either so all I can offer is more general advice and what is in the post linked to above.

1 Like

I do it like this and this works fine :slight_smile:

executeCommandLine("bash /openhab/conf/scripts/tv_api_wz.sh set_SkySportNewsHD")

case $1 in
  ######## VUplus Commands ########
    MuteState=$(curl "http://$vu_plus_ipadress/web/vol?set=state" | grep -oP '(?<=<e2ismuted>).*?(?=</e2ismuted>)')
    if [[ $2 == "ON" ]]; then
      if [ $MuteState = "False" ]; then
      curl "http://$vu_plus_ipadress/web/vol?set=mute"
    elif [[ $2 == "OFF" ]]; then
      if [ $MuteState = "True" ]; then
      curl "http://$vu_plus_ipadress/web/vol?set=mute"
    curl -s

Adopt it to your HDMI-CEC and it will work :slight_smile:

Hmm… I did consider a shellscript, but i figured it wouldnt work, since the script will still have the openhab user running it…
So i would probably have to use sudo su -u pi in the shellscript, forcing me make the openhab user member of the sudoers.

My backup-plan is to use MQTT, post the command from openHAB and have a “daemon” running under my normal user, subscribing to the MQTT/tv/command and execute the cec command when the MQTT topic changes state.
But that doesnt seem like a simple or intuitive solution…

Try it … I think the cec-client needs no sudo …

A few years ago I switched from cec-client to CEC from VU + Reciever and it works out of the box :slight_smile: So I’m not 100% sure whether it worked without sudo :wink:

The simplest, most direct way to do what you want is the exec binding, with Thing and channels.
It often needs tweaking permissions etc. to get it right.

You’re not making use of the features that could help you to diagnose what’s going on.

Giving an exec Thing a timeout allows the binding to listen for and capture any response - like an error message. (With no timeout, your call is fired into the void, and any results go into the void too.)

When you’re getting results, it is worth looking to see what they are.
Create a new String Item and link to the output channel.
Watch for this getting updated in events.log

I finally figured out the permissions issue, i simply added the user openhab to the pi group. :slight_smile:

I have added a timeout to my bindings and i tried adding a String Item linked to the output channel, but after a while it seemed easier to go back to the exec examples, so ive re-written my config files.

I even threw in the .rules-file since the examples used them, but im still not getting anywhere…

Im starting to think that i should skip exec all-together and use some sort of shellscript to check MQTT-topics and react to certain states with certain commands.
But thats kind of a bad idea as well, since im just as incompetent with MQTT and linux, but at least i got that part of my project working…

I can verify that the rules are executed in the events.log:

2020-01-12 20:49:05.281 [ome.event.ItemCommandEvent] - Item 'tvCommand' received command OFF
2020-01-12 20:49:05.289 [vent.ItemStateChangedEvent] - tvCommand changed from ON to OFF
2020-01-12 20:49:05.293 [ome.event.ItemCommandEvent] - Item 'tvCommand_Args' received command standby 0
2020-01-12 20:49:05.296 [nt.ItemStatePredictedEvent] - tvCommand_Args predicted to become NULL
2020-01-12 20:49:05.299 [ome.event.ItemCommandEvent] - Item 'tvCommand_Run' received command ON

But still no action, tv stays on…

my current files:

Thing exec:HDMICEC:TV [ command="%2$s", interval=0, timeout=20, autorun=false ]


Switch tvCommand
String theReturn "result [%s]"

	// state of the execution, is running or finished
Switch tvCommand_Run {channel="exec:HDMICEC:TV:run", autoupdate="false"}

	// Arguments to be placed for '%2$s' in command line
String tvCommand_Args {channel="exec:HDMICEC:TV:input"}

	// Output of command line execution 
String tvCommand_Out {channel="exec:HDMICEC:TV:output"}


sitemap exec label="HDMI CEC"
	Frame { 
		Switch item=tvCommand label="TV_pwr" mappings=["ON"="on","OFF"="off"]
		Text item=theReturn


rule "execRules"
		Item tvCommand received command
	if (receivedCommand==ON ) {
		tvCommand_Args.sendCommand("echo on 0 | cec-client -s -d 1")
	else if (receivedCommand==OFF ) {
		tvCommand_Args.sendCommand("echo standby 0 | cec-client -s -d 1")
		// Trigger execution (if autorun false)
		// the Run indicator state will go ON shortly, and return OFF when script finished

rule "process your results"
   Item tvCommand_Run changed from ON to OFF
      // Logging of raw command line result
   logInfo("HDMI-CEC command exec", "Result:" + tvCommand_Out.state )

What’s in openhab.log?

What’s in the Out Item? That’s the result of the command.

the openhab.log shows nothing relating to this.

Im not sure how to check whats in the out item… any suggestions?

I tried to get the data to feedback to the sitemap Text item=theReturn, but nothing is showing up on the sitemap… Probaly yet another “error between seat and screen”…

Okay, so your rule says

but your events.log shows

I’d suspect your rules file may not have loaded and this running an old version of your rule. Check openhab.log for “yourrulesfile.rules Loaded” messages

Alright, you are configured for that to trigger an exec run.

The exec binding would update this Switch Item’s state to ON when it runs the call. (as you have correctly suppressed autoupdate in the config)
Not present in the events.log snippet you show us - is it snipped or not present? Suggests not called, and I’d expect to find some reason in openhab.log