Friday, 2 June 2017

HOME AUTOMATION: openHAB exec2 binding

Since I started with openHAB I've been annoyed that it's not super simple to execute a script with a button press. There is an openHAB2 binding called the "Exec" binding that allows us to do exactly this, but the use of it is a mess in my opinion. The official documentation doesn't really give many good examples of how it can be used and trying to find a good example in the forums is hard because there is a openHAB1 and openHAB2 version of the binding that are used differently.

The first useful bit of information I came across was github repo XML definition of the Thing type.
This gives an explanation of what the various parameters and what they do.

Another bit of info that may be useful is how the different elements required for the Exec binding to work tie together:
    One Thing tied to a command we want to run
          Multiple Items for each Thing (input, output, exit, run, lastexecution)
              A sitemap that displays any Items we want to interact with

Armed with this info lets take a look at a few examples of using the Exec binding:


Example 1

This example executes a python command when the defined button changes states. It does not resend the command if the button is in the OFF state and it is turned OFF again.

Thing:

 Thing exec:command:TV_GF_Vol_Up "TV" [ command="python /home/openhabian/addons/BlackBeanControl/BlackBeanControl.py -c LG_TV_Vol_Up -d Lounge", timeout=15, interval=0, autorun=true ]  
 Thing exec:command:TV_GF_Vol_Down "TV" [ command="python /home/openhabian/addons/BlackBeanControl/BlackBeanControl.py -c LG_TV_Vol_Down -d Lounge", timeout=15, interval=0, autorun=true ]  

Item:

 String TV_GF_OnOff "TV" { channel="exec:command:TV_GF_OnOff:input" }  
 String TV_GF_OnOff_Output "TV" { channel="exec:command:TV_GF_OnOff:output" }  
 Switch TV_GF_OnOff_Running "TV" { channel="exec:command:TV_GF_OnOff:run" }  
 Number TV_GF_OnOff_ExitValue "TV" { channel="exec:command:TV_GF_OnOff:exit" }  
 DateTime TV_GF_OnOff_LastExec "TV" { channel="exec:command:TV_GF_OnOff:lastexecution" }  

 String TV_GF_Vol_Up "TV" { channel="exec:command:TV_GF_Vol_Up:input"}  
 String TV_GF_Vol_Up_Output "TV" { channel="exec:command:TV_GF_Vol_Up:output"}  
 Switch TV_GF_Vol_Up_Running "TV" { channel="exec:command:TV_GF_Vol_Up:run"}  
 Number TV_GF_Vol_Up_ExitValue "TV" { channel="exec:command:TV_GF_Vol_Up:exit" }  
 DateTime TV_GF_Vol_Up_LastExec "TV" { channel="exec:command:TV_GF_Vol_Up:lastexecution" }  

 String TV_GF_Vol_Down "TV" { channel="exec:command:TV_GF_Vol_Down:input"}  
 String TV_GF_Vol_Down_Output "TV" { channel="exec:command:TV_GF_Vol_Down:output"}  
 Switch TV_GF_Vol_Down_Running "TV" { channel="exec:command:TV_GF_Vol_Down:run"}  
 Number TV_GF_Vol_Down_ExitValue "TV" { channel="exec:command:TV_GF_Vol_Down:exit" }  
 DateTime TV_GF_Vol_Down_LastExec "TV" { channel="exec:command:TV_GF_Vol_Down:lastexecution" }  

Sitemap:

 Switch item=TV_GF_Vol_Up label="Volume Up" mappings=[OFF="+", ON="+"]  
 Switch item=TV_GF_Vol_Down label="Volume Down" mappings=[OFF="-", ON="-"]  



Example 2

In this example we will create a single button that runs a python script each time the button is pressed.

Things:

Not needed as we will be executing the commands from within the rules we create.

Items:

This example creates a virtual item that is not tied to a Thing.
 String TV_GF_Power "TV Power"

Rules:

 rule "LG TV Power"  
 when  
     Item TV_GF_Power received update  
 then  
     executeCommandLine("python /home/openhabian/addons/BlackBeanControl/BlackBeanControl.py -c LG_TV_Power -d Lounge", 1000)  
 end  

Sitemaps:

 Switch item=TV_GF_Power mappings=[OFF="Power"]  


I've also found the following forum examples useful in developing my Exec scripts and functions.
Online example 1Online example 2

No comments:

Post a Comment