i am a openHAB Beginner.
I installed Openhabian on my Raspberry 2.
On the same Raspberry i got two scripts running (script1.sh and script2.sh).
One, that switches a remote socket on and the other switches the remote socket of.
Now i like to integrate that into my openHAB running instance. I want to run the scripts in the following style: “sh script1.sh”.
I installed the openHAB 2 addons (sudo apt-get install openhab2-addons) and have paper ui running in the browser.
But now i am lost on how to get further.
Can someone please send me a link to a tutorial or give me advice in descibing the next steps i have to do?
So you want to trigger the scripts by Openhab?
by rule:
rule "switch on"
when Time cron "0 0/3 * 1/1 * ? *" // or any other conditio like a switch, or anything else
then
var String Antwort = executeCommandLine("yourscript.sh",1000)
// Antwort can by used, but no need to
thank you.
Where do i have to save this code?
i prefer to configure openHAB with config files.
do i have to create a folder /usr/share/openhab2/rules/rule1.cfg?
install the Designer, you will find a folder for rule, item and so on. the above goes to the rules folder (a file in there)
you need a items file with something like:
Switch ScriptTrigger "switch for the scripts"
then rules:
rule "switch on"
when Item ScriptTrigger changed
// or any other conditio like a switch, or anything else
then
if (ScriptTrigger.state == ON) {
var String Antwort = executeCommandLine("yourscript.sh",1000) // Antwort can by used, but no need to
}
else {
var String Antwort = executeCommandLine("yourscriptNo2.sh",1000) // Antwort can by used, but no need to
}
only rughly like this. read the Openhabpage for getting started
thx for your replies. i installed the Designer and created some things. right now i am able to see the presence of my smartphone in the basic ui. i configured that with the designer.
now i created these rules:
rule "Presence_Mobile_S3 ON"
when
Item Presence_Mobile_S3 changed from OFF to ON
then
executeCommandLine("sh /home/pi/openHAB/scripts/remoteSocketOn.sh",1000);
end
rule "Presence_Mobile_S3 OFF"
when
Item Presence_Mobile_S3 changed from ON to OFF
then
executeCommandLine("sh /home/pi/openHAB/scripts/remoteSocketOff.sh",1000);
end
the rules are not triggered although i can see that the presence changed.
can you say me, if i can see a log somewhere what happens or not happens? in openHAB-userdata/log in the samba-share i cant see a file?
openhab takes the new rule as the rulefile is saved. but sometimes you have to restart openhab. check if your rule file realy changed, put a loginfo in it and see if the rule gets triggered. post your rule if you don’t get it working
2017-02-19 18:36:35.550 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'default.rules'
2017-02-19 18:36:35.612 [WARN ] [.rule.jvmmodel.RulesJvmModelInferrer] - Duplicate field: 'Presence_Mobile_S3'. Ignoring 'org.eclipse.smarthome.core.library.items.SwitchItem'.
2017-02-19 18:36:35.613 [WARN ] [.rule.jvmmodel.RulesJvmModelInferrer] - Duplicate field: 'Presence_Mobile_S4'. Ignoring 'org.eclipse.smarthome.core.library.items.SwitchItem'.
2017-02-19 18:36:44.089 [ERROR] [hab.binding.exec.handler.ExecHandler] - An exception occurred while executing '/home/pi/openHAB/scripts/remoteSocketOff.sh' : 'Cannot run program "/home/pi/openHAB/scripts/remoteSocketOff.sh": error=2, No such file or directory'
2017-02-19 18:36:55.416 [INFO ] [g.eclipse.smarthome.model.script.off] - off
rule "Presence_Mobile_S3 ON"
when
Item Presence_Mobile_S3 changed from OFF to ON
then
logInfo("on", "on");
executeCommandLine("sh /home/pi/openHAB/scripts/remoteSocket01On.sh",1000);
end
rule "Presence_Mobile_S3 OFF"
when
Item Presence_Mobile_S3 changed from ON to OFF
then
logInfo("off", "off");
executeCommandLine("sh /home/pi/openHAB/scripts/remoteSocket01Off.sh",1000);
end
really awkward, its triggering the log, but uses the old scriptname. but i changed the scriptname as you can see. hmm?
something is wrong here. the loginfo is comming after the execute command…
and a “Duplicate field”, I don’t know, but do you have more than one rule with the same name??
Does Designer show any red sigs on the reight side?
have a look direct in the config directory and check the rulefiles with an other editor.
@divramod please study the documentation. All you’ve asked before is said there. I’m not trying to mock you, I just fear you are making it harder for yourself by not reading up on the fundamentals…
How to restart openHAB is a bit hidden, at the end of this part
Regarding your question: For testing purposes you should log the output of the command as @Windrad suggested in his first answer (see “Antwort”). For triggering the rule yourself, just define a switch item just as @Windrad suggested in his second answer (see “ScriptTrigger”). You can even react on both events:
The result:
rule "Presence_Mobile_S3 ON"
when
Item Presence_Mobile_S3 changed from OFF to ON or
Item ScriptTrigger received command
then
logInfo("on", "on");
var String Antwort = executeCommandLine("sh /home/pi/openHAB/scripts/remoteSocket01On.sh",1000);
logInfo("on", Antwort);
end
i just did this, created a switch and now i react to both events. swicht state change in basic ui, which i created with a sitemap and the online state of my phone.
the missing link was the String Antwort.
I see now, that i am having a permission problem.
I already know, where all the config files are and can see my logfiles live.
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
sudo: no tty present and no askpass program specified
sh on done
/var/lib/openhab2
So do you have a hint, where i can read about permissions of the openhab-user and running a script which needs root-permissions because of the raspberry pi gpio usage?
Besides that, you could combine both rules into one. My rule file might look like this
val logfile="s3.rules"
rule "Presence_Mobile_S3"
when
Item Presence_Mobile_S3 received command or
Item ScriptTrigger received command
then
logInfo(logfile, "Presence of Phone changed to " + Presence_Mobile_S3.state);
var String output;
if (receivedCommand == ON) {
output = executeCommandLine("/home/pi/openHAB/scripts/remoteSocket01On.sh",1000);
} else {
output = executeCommandLine("/home/pi/openHAB/scripts/remoteSocket01Off.sh",1000);
}
logInfo(logfile, "Command result: " + output);
end