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

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")
#!/bin/sh
###################################
tv_ipadresse="192.168.80.120"
vu_plus_ipadress="192.168.80.110"
###################################

case $1 in
  ######## VUplus Commands ########
  set_vuplus_mute)
    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"
      fi
    elif [[ $2 == "OFF" ]]; then
      if [ $MuteState = "True" ]; then
      curl "http://$vu_plus_ipadress/web/vol?set=mute"
      fi
    fi
    ;;
  set_SkySportNewsHD)
    curl -s http://192.168.80.110/web/zap?sRef=1:0:19:6C:C:85:C00000:0:0:0:
    ;;
esac

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:
exec.things:

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

exec.items:

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"}

exec.sitemap:

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

exec.rules:

rule "execRules"
	when
		Item tvCommand received command
	then
	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)
	tvCommand_Run.sendCommand(ON)
		// the Run indicator state will go ON shortly, and return OFF when script finished
end

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

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

Itā€™s just a String Item, if it changes that gets shown in events.log

ā€¦Side-question; If i were to remove the *.log files, they would merely get replaced with a empty log file, right?

IĀ“ll have a reboot to make sure everything gets loaded properly, then try again and check the logs.

the log snippet was bad, i am sorry for that!!
edit: log-snippet after reboot

2020-01-13 00:41:37.545 [ome.event.ItemCommandEvent] - Item 'tvCommand' received command OFF
2020-01-13 00:41:37.566 [vent.ItemStateChangedEvent] - tvCommand changed from NULL to OFF
2020-01-13 00:41:37.931 [ome.event.ItemCommandEvent] - Item 'tvCommand_Args' received command echo standby 0 | cec-client -s -d 1
2020-01-13 00:41:37.935 [ome.event.ItemCommandEvent] - Item 'tvCommand_Run' received command ON
2020-01-13 00:41:37.937 [nt.ItemStatePredictedEvent] - tvCommand_Args predicted to become NULL

Okay, one step forward. What you want arriving in ā€œargsā€

Next -

This is a problem.
Commanding this ON should trigger your exec Thing.
That in turn should update state to ON.
That is missing; exec is not triggered.

Perhaps exec v2.x binding is not installed.
Perhaps your xxx.things file is not loading correctly - check openhab.log

Hmm, now I notice that youā€™ve made up syntax for your exec Thing

Nope.
Follow the binding documentation. You must specify the Thing type, the exec binding supports only one Thing type, command.
Then add any name you like for this particulatr Thing.

Thing exec:command:MYTV [ ...

Obviously you will need edit your Item channel links to match new valid Thing name.