OpenHAB python script run after button press, 15 September 2017

A short tutorial how to run a python script in response to a button press.

Last post I add a GPIO button to the OpenHAB pi

Today I wanted to add the ability to run a Python script in reaction to that press.

  • Important note : the script needs to be owned by the user openhab to be able to run

/etc/openhab2/items/btn.items –

Contact NormallyOpenPushButton "Normally Open Push Button [%s]" { gpio="pin:4 debounce:10000" }

/etc/openhab2/rules/btn.rules –

rule "btn pressed"
    Item NormallyOpenPushButton changed
    val results = executeCommandLine("/tmp/d/dd", 5000)
    logInfo("execTest", results)


/tmp/d/dd –

echo "$(date)" >> /tmp/d/tmp.txt


don’t forget to change the owner of both dd and tmp.txt

chown openhab:openhab dd
chown openhab:openhabtmp.txt


Refrenseto the article that helped me solve the permission issue: