OpenHAB python script run after button press

Eran Goldman-Malka · September 15, 2017

A short tutorial on 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"
when
    Item NormallyOpenPushButton changed
then
    val results = executeCommandLine("/tmp/d/dd", 5000)
    logInfo("execTest", results)
end

/tmp/d/dd -

#!/bin/bash
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: https://community.openhab.org/t/how-to-solve-exec-binding-problems/18131

Twitter, Facebook