OpenHAB executeCommandLine

OpenHAB executeCommandLine

How to execute operating system commands, shell scripts, programs and more trough OpenHAB rules

 

It’s a so called action launched inside a rule. The action parameter is the command itself.

Action “executeCommandLine

Some of the installed examples are designed using this feature of OpenHAB.

Example 1: an external mp3-player (more flexible than the internal java player) plays a sound:

 

rule "AirHorn"
when
    Item AirHorn changed
then
	val results = executeCommandLine("sudo@@/usr/bin/omxplayer@@-o@@local@@/etc/openhab2/sounds/AirHorn.mp3",5000)
    logInfo("Exec",results)
end

Example 2: There is no binding on OpenHAB to drive a DALI bus but it’s easy to write a Python program for bit-banging two GPIOs in order to interface this kind of protocol. Inside a rule we can use a standard OpenHAB slider to set an RGB value and drive the lamps accordingly:

 

var HSBType hsb
var red = 0
var green = 0
var blue = 0
var Cmd="/usr/bin/python@@/etc/openhab2/scripts/dali.py"

rule "HSBtoRGB"
    when
        Item DaliColor changed
    then
        hsb = DaliColor.state as HSBType
        red   = (hsb.red * 2.55).intValue
        green = (hsb.green * 2.55).intValue
        blue  = (hsb.blue * 2.55).intValue
        
        val results = executeCommandLine(Cmd+"@@"+red+"@@"+green+"@@"+blue,5000)
    	logInfo("Exec",results)
end

Example 3: Launch a Python script to drive a remote control IR repeater

 

rule "AMPLI_SRC"
	when 
		Item Ampli_Src changed
	then
		switch Ampli_Src.state
		{
			case  1:
			{
				val results = executeCommandLine("/usr/bin/python@@/home/openhabian/addons/BlackBeanControl/BlackBeanControl.py@@-c@@Ampli_VOL",1000)
    			logInfo("Exec VOL",results)
			}	
			case  2:
			{
				val results = executeCommandLine("/usr/bin/python@@/home/openhabian/addons/BlackBeanControl/BlackBeanControl.py@@-c@@Ampli_TUN",1000)
    			logInfo("Exec TUN",results)
			}		
			case  3:
			{
				val results = executeCommandLine("/usr/bin/python@@/home/openhabian/addons/BlackBeanControl/BlackBeanControl.py@@-c@@Ampli_SAT",1000)
    			logInfo("Exec SAT",results)
			}	
			case  4:
			{
				val results = executeCommandLine("/usr/bin/python@@/home/openhabian/addons/BlackBeanControl/BlackBeanControl.py@@-c@@Ampli_BOX",1000)
    			logInfo("Exec BOX",results)
			}	
		}
end	

Example 4: Launch a CLI system command

 

var String speedtestCliOutput = executeCommandLine("/usr/local/bin/speedtest-cli@@--simple@@--server@@395 ", 120*1000)

 

Some rules to keep in mind

  • Using the action as aforementioned (with a variable for the returning values) the system replies can be viewed as a standard OpenHAB log  for a simpler debug.
  • The timeout is mandatory in order to give enough time to the operating system to get the command.
  •  Take care to the blank spaces between commands and their parameters, they MUST be substituted with @ symbol.
  • As usual, because there is no console and therefore no environment variables, the full path for the command and its parameters must be specified.
  • The command is executed with openhabian user privileges. This user must be added to all the groups enabled to execute the commands.
  • To execute the command with sudo, the openhabian user must be added to the sudoers. Using ‘sudo visudo’, the text below must be added to the sudoers file:
openhab ALL=NOPASSWD: ALL

Share