How to restart binding with in rules?

Hi. Some times when my network is down and up, NetworkUpsTools Binding (its connected to remote host) and Harmony bindings stops working (no errors in log).
If i execute from karaf console bundle:restart 208, 211 this bindings start works again.
How to execute this command from rules?

Iā€™m pretty sure you can control karaf from a shell script so using the exec binding might be an option.

So, this can be done but it will require some config on your part.

Firstly, you need follow these instructions (scroll down to Managing authentication by key), the folder is userdata/etc/keys.properties, to configure karaf to use ssh certs for authentication and make sure the openhab user is configured to use those certs. This will make it so you donā€™t have to enter a password for the openhab user to ssh into the karaf console.

Then you can issue the following command through executeCommandLine or the Exec binding:

ssh -p 8101 openhab@localhost 'bundle:restart 208, 211'

There are other approaches you can take with sshpass but you really should use the ssh keys instead.

2 Likes

Restart command does accept list of bundle IDs without comma. Another option is to use bin/client script which accepts -f option (commands in file, executed line by line).

Thanks for all. I thought it would be much easier :slight_smile:
openHAB makes programmers from all of us.

Hi martiniman. Iā€™ve the same issue with digitalStrom binding. From time to time all digitalStrom things are offline and restarting the binding is the solution.

But how can the above execution be triggered? How can a rule check if a thing (dssBridge) is offline?

Hi @tailor. May be you should use thing state, but i cant tell how to do that (i use only items).

I think you can trigger a rule based on the online/offline state of a Thing. There is no way I know of to check the state of a Thing in a Rule though, only Items.

It hasnā€™t made it into the docs yet as far as I can tell but you can find the syntax here

Ok, thank you. Is this feature already available in stable release or do I need a current snapshot?

There are the following states for a thing:

  • UNINITIALIZED
  • INITIALIZING
  • UNKNOWN
  • ONLINE
  • OFFLINE
  • REMOVING
  • REMOVED

Iā€™ll try to implement the following two rules triggering a switch.

rule "Check dssBridge OFFLINE"
when
    Thing 'digitalstrom:dssBridge:302ed89f43f0xxx' received update OFFLINE or
    Thing 'digitalstrom:dssBridge:302ed89f43f0xxx' received update UNINITIALIZED or
    Thing 'digitalstrom:dssBridge:302ed89f43f0xxx' received update UNKNOWN
then
    swi_dss_online.sendCommand(OFF)
end
rule "Check dssBridge ONLINE"
when
    Thing 'digitalstrom:dssBridge:302ed89f43f0xxx' received update ONLINE
then
    swi_dss_online.sendCommand(ON)
end

Does this make sense?

The final goal is to restart the binding via execute binding.

You probably need the SNAPSHOT but I donā€™t really know.

I donā€™t know if you will ever see an UNINITIALIZED event to trigger a Rule as I think that is a Thingā€™s default state. Beyond that what you have looks like it should work.

ok, thank you. It works with current stable release. Iā€™ll try now to implement the exec binding.

1 Like

Hi,

I need some advise regarding restarting a binding from rules.
I have installed sshpass, and can run the command from from ssh terminal and restart binding without typing karaf password. I have created a python script and put it in script folder. I can run the script from ssh terminal
like: ./restartspotify.py, and the binding is restarting.

restartspotify.py:

sshpass -p 'habopen' ssh openhab@localhost -p8101 'bundle:restart org.openhab.binding.spotify'

BUT, i can not get my rule to execute the python script without error.

rule:

rule "Restart Spotify Binding"
	when 
	Thing "spotify:device:dd997e5e:1a729da3-76b9-4234-a426-1c445aa638a2" changed or
	Item Restart_Spotify received command ON
	
	then
		var status = ThingAction.getThingStatusInfo("spotify:device:dd997e5e:1a729da3-76b9-4234-a426-1c445aa638a2").getStatus()
		//logError("Debug", "AudioGruppe status | " + status)
		
		if (status.toString() == 'OFFLINE')
		{
			//logError("Debug", "Attempting to restart spotify binding")
			executeCommandLine("/etc/openhab2/scripts/restartspotify.py")
					
		}
		
	end

openhab.log

2017-09-16 19:54:17.434 [INFO ] [lipse.smarthome.io.net.exec.ExecUtil] - executed commandLine '/etc/openhab2/scripts/restartspotify.py'
2017-09-16 19:54:19.090 [WARN ] [shd.server.session.ServerSessionImpl] - exceptionCaught(ServerSessionImpl[null@/127.0.0.1:40886])[state=Opened] IOException: Connection reset by peer

Any advise would be appreciated :slight_smile:

Best regards
Chris

Remember that OH is running as the openhab user.

Hi Rich.

Would you mind elaborating how to solve this?
What do I need to do, to make user openhab run this script?
I have chown restartspotify.py to openhab:openhabian, and chmod 0755.

I try to copy /.ssh to ~openhab/.ssh but fail in the process. A step by step guide would be great :slight_smile:

Follow the steps in the link.

Try running it with excuteCommandLine and log what is being written by the script. This will tell you what the problem is and give you a clue how to solve it.

User openHAB IS running it when it is executed by openHAB. Thatā€™s the point. Itā€™s a different user than when you run the script from the command line. This is almost always the source of problems with exec.

Everyoneā€™s setup is different making a step by step all but impossible.

Iā€™ve already posted my openhab.log after executing python script containing executeCommandLine.
I get :

[WARN ] [shd.server.session.ServerSessionImpl] - exceptionCaught(ServerSessionImpl[null@/127.0.0.1:40886])[state=Opened] IOException: Connection reset by peer

Iā€™ve followed the steps in the link, copied .ssh files from openhabian to openhab, but no changes in my results :frowning:
As I can run the command from terminal using openhabian user, I now need to make openhab user permissions to run it to solve my problem.
The exec guide is very good and well written, but I canā€™t get my head to fully understand how to do this :blush:

Like I already said. You need to use executeCommandLine from a rule and log out what the python script is writing out.

For the purposes of solving your problem, that log statement is irrelevant.

Iā€™m sorry Rich, I feel very stupid :smiley: I understand what you mean now. I have generated another rule now with log out from my script. this is whatā€™s returned:

2017-09-18 18:41:36.517 [INFO ] [lipse.smarthome.io.net.exec.ExecUtil] - executed commandLine '/etc/openhab2/scripts/restartspotify.py'
2017-09-18 18:41:36.568 [INFO ] [ipse.smarthome.model.script.execTest] - 
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.
sudo: no tty present and no askpass program specified

AND now I have managed to corrupt my sudoers file :frowning: the openhabian user is no longer part of sudo group. and I have no privileges to do anything to fix thisā€¦ Would really like to avoid reinstalling my entire setup, due to privileges issues though :blush:
Have tried almost everything iā€™ve found on google, without luck

used these tips:

I get this:

openhabian@openHABianPi:~$ sudo openhabian-config
sudo: /etc/sudoers is world writable
sudo: no valid sudoers sources found, quitting
sudo: unable to initialize policy plugin