Restart binding from Rule

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

Hi Chris,

have you been able to solve this?

Hi NCO,

Yes, It is working fine now. What I did was the following:

  • Install sshpass
  • Make a rule
  • Make a script, which is chmod 0755 and chown openhab:openhabian.

rule file:

rule "Restart Spotify Binding"
	when 
	Item Restart_Spotify received command ON
	
	then
       //logInfo(logfile, "AudioGruppe spotify device changed to " + spotify:device:xxxxxxxx.state);
		var status = ThingAction.getThingStatusInfo("spotify:device:xxxxxxx").getStatus()
		logError("Debug", "AudioGruppe status | " + status)
		
		//if (status.toString() == 'OFFLINE'&& Spotify_AudioGruppe_SpotifyConnectDeviceActive.state=="ON")
		{
			logError("Debug", "Attempting to restart spotify binding")
			executeCommandLine("/etc/openhab2/scripts/restartspotify.py")
			//logInfo(logfile, "Command result: " + output);			
		}
				
	end

The rule is not clean, as I have used different logics inside it over the last few weeks. But I it executes a script file when i toggle my item: Restart_Spotify. It should be possible to restart binding when detecting that channel is offline.

The script:
restartspotify.py

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

I’m using openhabian and standard habopen password for the karaf console. So that’s what is used in the python script. So when using sshpass, the script will fill in authentication automatically when executing a script.

Hope this helps.

BR
Chris

1 Like

Hi,
I am bit stuck and hope someone could help.

I have a script in the folder with owner and permissions as suggested.

If I run the script from console it executes my binding restarts. If I run it from a rule I can see that the rule ran and the command was sent but the binding does not restart.

sudo -u openhabian sshpass -p habopen ssh openhab@localhost -p8101 bundle:restart org.openhab.binding.openwebnet

log

2020-03-17 08:49:27.813 [ERROR] [.smarthome.model.script.Thing_Status] - BUS gateway is OFFLINE

2020-03-17 08:49:27.831 [INFO ] [lipse.smarthome.io.net.exec.ExecUtil] - executed commandLine '/etc/openhab2/scripts/restartOpenWebNet.py'

2020-03-17 08:49:27.835 [ERROR] [.smarthome.model.script.Thing_Status] - openwebnet binding restarted

Any ideas or does this not work anymore with OH2.5. I have added the command to the whitelist.

edit … got it working but changed from sshpass to rsa keys