Configuring a virtual Switch executing a python script

  • Platform information:
    • Hardware: RPI4 (2GB RAM)
    • OS: Debian Linux
    • Java Runtime Environment: openjdk version .1.8.0_272
    • openHAB version:2.5.10-1

I am trying to configure a virtual ON/OFF switch to execute a python script. With this aim I have installed the Exec binding. Therefore I have set up the following files.

exec.things:
Thing exec:command:luce [command=“python /srv/openhab2-conf/python_scripts/light_on.py”]

exec.items:
Switch Trigger “Trigger”
String in {channel=“exec:command:luce”}

exec.rules:
rule “Exec script”
when
Item Trigger received command ON
then
executeCommandLine(“python /srv/openhab2-conf/python_scripts/light_on.py”)
end

In the control panel of OpenHAB the switch is disactivated.
I also tried to set another thing connected to a physical switch as autorun=true, interval=10 so the script starts automatically once the physical switch is off, but this is not what I want.
How can I solve the issue?

That switch is active once the exec binding related script is being executed.
E.g. in case the script is triggered every 10 s by using the interval option the the switch is active every 10 seconds. So it is not made for switching …
You need to add a physical button and e.g. trigger a rule once the physical button is switched ON. The rule then can trigger the exec binding via REST API.

That runs the script entirely independently of any Item/channel/Thing
Any result is discarded.

executeCommandLine() is an either/or option to the Exec binding.

You can capture the result with
var results = executeCommandLine( ...

Or, you can use the binding Thing, but read the docs. You can trigger by sending an ON command to an Item linked to run channel.` You’ll need to add the command to the exec whitelist .