How to start

Hey,

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?

thx in advance

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

hey ingo,

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?

at the end, i want to be able to take the switch on and of with the android openhab app, which i have already installed

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

I believe all you need to know will become clear to you after browsing the documentation for a few minutes. http://docs.openhab.org

  • Install the SmartHome Designer on your PC
  • Mount the openHABian Samba shares. Username and password are either openhab:habopen or openhabian:openhabian, depending on your openHABian version
  • Connect the designer to the drive letter representing the share openhab-conf
  • Create new files under items and rules (keyboard shortcut Ctrl+N) named remote-socket.items or similar
  • Follow @Windrads solution above (possibly with full path to your scripts)

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?

thx for your help on sunday btw :slight_smile:

put this in your rules fie:
logInfo(“any keyword”, "w.t.f.i.g.on: " + maybeAnyItem)

the log will be:
/var/log/openhab2/openhab.log

1 Like

thx i can see the logs now:

tail -f openhab.log

i had the wrong script name.
now i changed the script name, but the rule is not updated.
the log says that the rule is still using the old rule.

do i have to manually trigger the use of the updated rules. i saved it and can see in the log, that openhab is doing something.

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?

how can i restart openhab2 via commandline?

is it running?
service openhab2 sstatus

stop it:
service openhab3 stop

start:
service openhab3 start

on some scrits even restart is working, not sure in OH

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.

i think i mixed something up here.

at first, i added the items/things via network devices via paper ui and later i created the rules/items/sitemap with the designer.

for testing purposes, how can i say, how often the status of device is updated, for example every 5 seconds?

Switch Presence_Mobile_S3_cfg "S3" <network> { channel="network:device:192_168_178_61:online" }
Switch Presence_Mobile_S4_cfg "S4" <network> { channel="network:device:192_168_178_37:online" }

@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… :wink:

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

Thx Thom,

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?

thx for your responses again.

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

(untested, might contain typos)

ahh, thx. that looks much more elegant.
i will try this at next.

do you have read my message before, where i asked about the permission problem?

Permissions: Please read directly below the table of content: http://docs.openhab.org/installation/linux.html

GPIO workaround: GPIO Binding not working · Issue #39 · openhab/openhabian · GitHub

Running scripts as root: There are numerous threads about that. Please read up on them.