Skip to content

OpenHab Exec Binding Example

This example uses a single shell script, and a single 'String' item for reading and writing an external state. The String item kan be linked directly to a button in a panel like a Switch.

File: misc/exec.whitelist

Holds same bash script for writing and for reading depending on whether an argument was added.

# For security reasons all commands that are used by the exec binding or transformation need to be whitelisted.
# Every command needs to be listed on a separate line below.
/usr/local/bin/example.sh %2$s
/usr/local/bin/example.sh

File: things/example.things

Two things one for writing and one for reading the state.

Thing exec:command:setexample "Example In" @ "Home" [command="/usr/local/bin/example.sh %2$s", interval=0,timeout=10, autorun=true]
Thing exec:command:getexample "Example Out" @ "Home" [command="/usr/local/bin/example.sh", interval=5,timeout=10, autorun=true]

File: items/example.items

Combines output (reading) and input (writing) for the string.

String ExampleIO "Example IO [%s]" {channel="exec:command:setexample:input", channel="exec:command:getexample:output"}

File: rules/example.rules

Just a rule to show a change can be reacted on. For some reason when the external state changes clicking the button is no working. So this is teh fix.

rule "Example IO Change"
when
  Item ExampleIO changed
then
  ExampleIO.sendCommand(ExampleIO.state)
  logInfo("Exec", "Change in 'Example IO': " + ExampleIO.state)
end

File: /usr/local/bin/example.sh

This script writes a state argument into a file and returns it depending on an argument is given or not. Use this script on the command line to trigger an external change of the string.

#!/bin/bash

if [[ -z "$1" ]]; then

    value=$(cat /tmp/state.txt)
    echo "Get: ${value}" >> /tmp/output.txt
    echo -n "${value}"

else

    echo "Set: $1"  >> /tmp/output.txt
    echo -n "$1" > /tmp/state.txt
    echo "$1"

fi