Exec Binding in HABPanel

DeepL:
Hello,

I would like to be able to start and stop my PC via the HABPanel. I wanted to realize this with the WOL-Binding and the Exec-Binding.

Starting up is possible. But I can’t shut it down. If I type the command in the command line, the command is executed without problems. But if I try to bind it to the HABPanel via Things and Items, nothing works.

.things

Thing exec:command:pcoff "Switch off PC" [command="net rpc shutdown -I 192.168.178.36 -U Admin%admin -f"]

.items

Switch PCoff "PC off" { channel="exec:command:pcoff:run" }

exec.whitelist

net rpc shutdown -I 192.168.178.36 -U Admin%admin -f

I just don’t know what to do anymore.

Is there any message in the openhab.log, events.log file when you execute the command ?
Is there any entry in the windows event logs ?

Hi Wolfgang,

thanks for your answer. To be honest, i dont know where to find and read the logs. I am relatively new to openHAB and dont know a lot.

Can you tell me where to find these logs? I am running openhab on a raspberry.

You should find the logs in directory /var/log/openhab2/ ( installed via apt ) or userdata/log ( in case it is a manual setup ). See https://www.openhab.org/docs/administration/logging.html

Hi

There is security measure in place which prevents any command being used by the exec binding until it is added to the whitelist.
(Search the forum for more information on this and how to resolve it)

Alternatively, you can run the command from a DSL rule, using the executeCommandLine("/YourExactPath/FileName") line

You can also construct complex commands this way.

1 Like

I have been using a rpc shutdown command successfully for quite some time with the executeCommandLine function running OH natively on linux:

executeCommandLine("sudo /usr/bin/net rpc shutdown --ipaddress XXX.XXX.XXX.XXX --user *****%*****")

but in this case there are a couple of additional things (and these probably apply to using the exec binding as well)

  1. In most cases the net command requires elevated privileges (i.e., sudo)
    1a) You will also have to add the command to your sudoers file so it can run without password input (plenty of how to’s online about the various ways to do this, I prefer visudo)
  2. The user that OH is running under does not necessarily have all the environment set that you would expect and it probably can’t find the command just as net which is why you have to specify the complete path to the command /usr/bin/net
3 Likes

Hey guys,

i retried it. I deleted the .things and the .items files and did it from scratch.

.things

Thing exec:command:stoppc "Stoppe PC" [command="net rpc shutdown -I 192.168.178.36 -U Admin%%admin"]

.items

Switch StartPC "Starte PC" { wol="192.168.178.255#B4-2E-99-30-F9-CD" }
String StopPC "Stoppe PC" { channel="exec:command:stoppc:run" }

I made in HABPanel 2 Buttons. One sends the “ON” command and the other sends the “OFF” command to the “StopPC”-item.

After clicking both buttons a few times, this shows up:
events.log

2020-09-20 00:11:53.147 [ome.event.ItemCommandEvent] - Item 'StopPC' received command ON
2020-09-20 00:11:53.150 [nt.ItemStatePredictedEvent] - StopPC predicted to become ON
2020-09-20 00:11:53.156 [vent.ItemStateChangedEvent] - StopPC changed from NULL to ON
2020-09-20 00:11:57.301 [ome.event.ItemCommandEvent] - Item 'StopPC' received command OFF
2020-09-20 00:11:57.304 [nt.ItemStatePredictedEvent] - StopPC predicted to become OFF
2020-09-20 00:11:57.312 [vent.ItemStateChangedEvent] - StopPC changed from ON to OFF
2020-09-20 00:11:59.448 [ome.event.ItemCommandEvent] - Item 'StopPC' received command ON
2020-09-20 00:11:59.451 [nt.ItemStatePredictedEvent] - StopPC predicted to become ON
2020-09-20 00:11:59.459 [vent.ItemStateChangedEvent] - StopPC changed from OFF to ON

exec.whitelist

net rpc shutdown -I 192.168.178.36 -U Admin%%admin

the habpanel.log does not say anything. And i dont know how to refresh it, if its even possible.

Just a thought: You might need to mask the %-sign

Sorry for my bad english-skills. What do you mean by “mask” something?

I got a solution.

https://openhabforum.de/viewtopic.php?t=982

I added a .rules file.

.items

Switch PC_Switch "PC Schalter" { wol="192.168.178.255#B4-2E-99-30-F9-CD" }

.rule

rule	"Computer aus"
when	Item PC_Switch received command OFF
then 	executeCommandLine('net rpc shutdown -f -I 192.168.178.36 -U Admin%admin -t 30') 
end

rule	"Computer an"
when	Item PC_Switch received command ON
then 	executeCommandLine("net rpc abortshutdown -f -I 192.168.178.36 -U Admin%admin")
end

the first part sends a shutdown command, if the state of PC_Switch switches to “OFF”
the secon part sends a abortshutdown command, if the state of PC_Switch switches to “ON”

If the Computer is powered off, the WOL-Binding takes part and starts the PC.

1 Like

Please try this:

Thing exec:command:stoppc “Stoppe PC” [command=“net rpc shutdown -I 192.168.178.36 -U Admin%%%%admin”]

(also add the 2 %-signs in the whitelist)

Btw: You defined StopPC as String? I guess you want a Switch too right?

1 Like

I defined it as a string, because i read somwhere, that the Exec-Binding got problems with Switch-items.

Great! Glad you found a solution! I tried it on my test-system and it worked for me with the double %%. The OS-process then was:

openhab 12799 1192 1 06:02 ? 00:00:00 sh -c /usr/bin/net rpc shutdown -I 192.168.178.36 -U Admin%%admin

Before with only 1 %:
openhab 12800 12799 34 06:00 ? 00:00:00 /usr/bin/net rpc shutdown -I 192.168.178.36 -U Admin

Hey Flip,

i got one last question. Everything works fine for now, except the comment while shutting down.

executeCommandLine('net rpc shutdown -f -C "A B C D E F G" -I 192.168.178.36 -U Admin%admin -t 30')

The output is always

"A

Yet i have seen no solution to output my whole Text

You have to mask the quotes with a backslash

executeCommandLine('net rpc shutdown -f -C \"A B C D E F G\" -I 192.168.178.36 -U Admin%admin -t 30')

Its not working either.

Output:

"A

For anyone who is reading this thread and got the same problem. I found a solution with a rule workaround.

I also optimized my whole HABPanel experience. I want to share this with you.
Inside the HABPanel the Switch shows me if the computer is on, off, shutting down or starting.

Required:

  1. Wake-on-LAN Binding
  2. Network Binding

First you need to configure a pingable-device.thing using Network binding.

.things

network:pingdevice:192_168_XXX_XXX "Computer" [ hostname="192.168.XXX.XXX", refreshInterval=6000 ]

Now you need three items. One items needs to store the status of you computer. e.g. on, off, starting, shutting down. And the other item needs to be a Wake on LAN Switch. And the last item stores the real on/off state, using Network Binding.

.items

String Computer_Status "Computer Status"
Switch Computer_WOL "Computer Wake on LAN"  { wol="<broadcastIP>#<MAC-ADDRESS>" }
Switch Computer_Online "Computer Online" { channel="network:pingdevice:192_168_XXX_XXX:online" }

Now you need a rule, which:

  1. remotely shutdown your computer
  2. abort shutdowns, if you want to
  3. starts the computer, if its turned off
  4. saves the status of the computer into “Computer_Status”

.rules

rule	"Shutdown Computer"
when	Item Computer_Status received command OFF
then 	    if (Computer_Online.state == ON) {
		        executeCommandLine('net rpc shutdown -f -I 192.168.XXX.XXX -U USER%PASSWORD -t 30')
		        Computer_Status.postUpdate("SHUTDOWN")
		    }
end

rule	"Abort/Start Computer"
when	Item Computer_Status received command ON
then	executeCommandLine("net rpc abortshutdown -f -I 192.168.XXX.XXX -U USER%PASSWORD")
 	        if (Computer_Online.state == OFF) {
		         Computer_Status.postUpdate("STARTING")
		         Computer_WOL.sendCommand(ON)
	        }
end

rule	"Computer on"
when	Item Computer_Online changed from OFF to ON
then	Computer_Status.postUpdate("ON")
end

rule	"Computer off"
when	Item Computer_Online changed from ON to OFF
then	Computer_Status.postUpdate("OFF")
end

You can add this easily to your .sitemaps, if you want to

.sitemaps

Switch item=Computer_Status label="Computer [%s]" icon=computer

In HABPanel you need to add a button.

Name:               Computer
Action:             Switch between two states
openHAB item:       Computer_Status
command value:      ON
alternative:        OFF
userdefined symbol: computer

Then you need to use a own symbol, to display all states. I added some for you.

You need to rename these icons to:

  • computer-off.svg
  • computer-on.svg
  • computer-shutdown.svg
  • computer-starting.svg

and then store them easily in: /etc/openhab2/icons/classic if youre running openhab on a raspberry pi

computer computer-on computer-shutdown computer-starting

Now have fun with your working Start/Shutdown Button in HABPanel.

1 Like

Sorry! That‘s really weird… In my environment it works… I will check again!