Hello!
My actual use case is I want to end up with a button or switch in a sitemap that my wife can poke from openhab on her cellphone to turn my son’s internet access on and off. To do that, I think I want to use the exec binding as that will let me check the running state of the script so multiple hits won’t lose sync with what’s on the switch. Running the script takes a little bit - less that a minute, but not much less, so having her mashing at the button could be a thing.
Right now it looks like everything works up to actually calling the script. The implementation is based on the document at https://www.openhab.org/addons/bindings/exec/ except I don’t think I need a return value from my script at this point. I’ve added some internal tests to the script (write to a file in /tmp) that never gets called, but the log entry that fires immediately prior does log (the check state entry). The script runs fine as “sudo -u openhab /sbin/jonas)internet. py ON” and even ran fine when I stripped everything out and just did a blind executeCommand thing like this:
THIS_WORKED:
rule “Jonas internet”
when
Item JonasTrigger changed
then
if(JonasTrigger.state == ON){
executeCommandLine(’/sbin/jonas_internet.py@@ON’, 180000)
}else{
executeCommandLine(’/sbin/jonas_internet.py@@OFF’, 180000)
}
end
The only thing with doing it this way is the button dohickey stays green all the time and I’m not sure what toggling the switch a bunch of times in a row will leave me with. I’m totally game for trying other things, I’m already to the point of considering a flask instance with an api call and then using the http binding if I can’t get this to work.
Once running, I hope to add mysql persistence to this trigger so a restart will bring it back up in the last known state.
Thank you for reading my wall of text.
-ian
=================================================================
-
Platform information:
-
Hardware: VM with 2GB ram and four vcpus
-
OS: Ubuntu 18.04
-
Java Runtime Environment: openjdk version “1.8.0_252”
OpenJDK Runtime Environment (build 1.8.0_252-8u252-b09-1~18.04-b09)
OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode) -
openHAB version:
root@openhab:~# dpkg -l | grep openhab
ii openhab2 2.5.6-2 all openhab2
ii openhab2-addons 2.5.6-2 all openhab2-addons
-
-
Issue of the topic: Trying to run python3 script from exec but script doesn’t seem to be actually getting called.
-
Please post configurations (if applicable):
misc
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.
/sbin/jonas_internet.py %2$s
/sbin/jonas_internet.py@@%2$s
/sbin/jonas_internet.py ON
/sbin/jonas_internet.py OFF
**I’m trying everything here - not sure if this is blocking?
things
Thing exec:command:jonasinternet [ command="/sbin/jonas_internet.py@@%2$s", interval=0, timeout=180000, autorun=false ]
**Tried with @@ and without
items
Switch JonasTrigger “Turn Jonas Internet [%s]”
Switch jonasinternet_Run {channel=“exec:command:jonasinternet:run”, autoupdate=“false”}
String jonasinternet_Args {channel=“exec:command:jonasinternet:input”}
rules
rule “Set up your parameters”
when
Item JonasTrigger changed
then
if(JonasTrigger.state == ON){
jonasinternet_Args.sendCommand(“ON”)
}else{
jonasinternet_Args.sendCommand(“OFF”)
}
end
rule “begin your execution”
when
Item jonasinternet_Args received command
then
logInfo(“jonasinternet”, “args passed ok, trying to start script”)
if (jonasinternet_Run.state != ON) {
logInfo(“jonasinternet”, “Passed state not ON check”)
jonasinternet_Run.sendCommand(ON)
}else{
logInfo(“Jonas Internet”, “Script already in use, skipping execution.”)
}
end
rule “script complete”
when
Item jonasinternet_Run changed from ON to OFF
then
logInfo(“Jonas Internet”, “Script has completed.”)
end
sitemap
// Name of file and name of sitemap has to be the same
sitemap jonasinternet label=“Jonas Internet”
{
Frame {
Switch item=JonasTrigger
}
}
The actual script being called
#!/usr/bin/python3
import sys
from pprint import pprint
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
host = ‘my_switch’
user = ‘my_user’
password = ‘my_password’
commands = {
‘ON’: ‘turn on switch configuration stuff’,
‘OFF’: ‘turn off switch configuration stuff’
}
with open(’/tmp/jonasinternet.log’, ‘a’) as f:
f.write(“I ran!”)
if len(sys.argv) != 2:
raise ValueError(“Need to specify ON or OFF”)
if sys.argv[1] not in commands:
raise ValueError(“Invalid arguement”)
dev = Device(host=host, user=user, password=password ).open()
dev.timeout = 120
with Config(dev) as config:
config.load(commands[sys.argv[1]], format=‘set’)
config.commit()
dev.close()
print(sys.argv[1])
logs
2020-07-01 04:22:19.813 [INFO ] [smarthome.model.script.jonasinternet] - args passed ok, trying to start script
2020-07-01 04:22:19.815 [INFO ] [smarthome.model.script.jonasinternet] - Passed state not ON check