# (SOLVED) Newbie, looking for help with Scripts & Rules

**URL:** <https://community.openhab.org/t/solved-newbie-looking-for-help-with-scripts-rules/74686>\
**Category:** Beginners\
**Created:** [May 18, 2019, 2:48pm UTC](https://community.openhab.org/t/solved-newbie-looking-for-help-with-scripts-rules/74686 "2019-05-18T14:48:15Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alarm\_guy1](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@Alarm\_guy1](https://community.openhab.org/u/Alarm_guy1)\
**Post date:** [May 18, 2019, 2:48pm UTC](https://community.openhab.org/t/solved-newbie-looking-for-help-with-scripts-rules/74686/1 "2019-05-18T14:48:15Z")

</div>

I am new to OpenHab, moving from Domoticz, I had a nice DZ Vents code to turn on certain lights when motion was detected by their retrospective Pirs for X amount of time but only at a certain Lux level or below.

I must add this wasn’t created by me my skills are no where near that level. I was hoping someone could point me in the right direction, I cannot even find in OpenHab where to start.

I can log on via SSH but the folder tree does not contain any of the files mentioned in the tutorials, I sort of dabbled with Node red but not sure if you can do what I need with that.

Swimming way out of my depth here but would like to migrate all my stuff across but need this element sorting before I can move my bits across.

I do find OpenHab a cleaner interface and deals with my RGBW much better than Domoticz.

Cheers for the help in advance

BTW im running on a Pi 2 software Linux openHABianPi 4.14.98-v7  
Keith

---

<div class="post-metadata">

**Author:** ![opus](https://community.openhab.org/user_avatar/community.openhab.org/opus/32/55801_2.png) [@opus](https://community.openhab.org/u/opus)\
**Post date:** [May 18, 2019, 3:00pm UTC](https://community.openhab.org/t/solved-newbie-looking-for-help-with-scripts-rules/74686/2 "2019-05-18T15:00:37Z")

</div>

Welcome to the openHAB Forum!  
How about reading here:  
[Documentation](https://www.openhab.org/docs/)

---

<div class="post-metadata">

**Author:** ![Alarm\_guy1](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@Alarm\_guy1](https://community.openhab.org/u/Alarm_guy1)\
**Post date:** [May 18, 2019, 4:34pm UTC](https://community.openhab.org/t/solved-newbie-looking-for-help-with-scripts-rules/74686/3 "2019-05-18T16:34:02Z")

</div>

Thankyou for the quick response, my issue is as follows:  
the screenshot is my OpenHab install

 ![Habian](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/e/5/e51e656d01173f25be609e5feaa1db7718dff6ec.jpeg)

And the tutorial states the following:  
Rules are placed in the folder `$OPENHAB_CONF/rules` . The [demo setup](https://www.openhab.org/docs/tutorials/demo.html) already comes with a demo file called `demo.rules` , which has a couple of examples that can be a good starting point."

I cant even get off the starting blocks

cheers

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [May 18, 2019, 5:01pm UTC](https://community.openhab.org/t/solved-newbie-looking-for-help-with-scripts-rules/74686/4 "2019-05-18T17:01:22Z")

</div>

> [@Alarm\_guy1](#):
>
> Rules are placed in the folder `$OPENHAB_CONF/rules`

The $OPENHAB\_CONF bit is a placeholder. The actual names depends on the kind of install and host system. On my Windows for example, its C:/openhab2/conf and there I find folders /rules. /items, /icons, etc.

> [@Alarm\_guy1](#):
>
> I can log on via SSH

Have another browse around and find the files

You’re gong to need some kind of text editor for e.g. rules text files.

---

<div class="post-metadata">

**Author:** ![Alarm\_guy1](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@Alarm\_guy1](https://community.openhab.org/u/Alarm_guy1)\
**Post date:** [May 18, 2019, 6:07pm UTC](https://community.openhab.org/t/solved-newbie-looking-for-help-with-scripts-rules/74686/5 "2019-05-18T18:07:22Z")

</div>

Cheers found it it was under /etc/openhab2/rules.

I use Nano so will have a play

cheers

---

<div class="post-metadata">

**Author:** ![opus](https://community.openhab.org/user_avatar/community.openhab.org/opus/32/55801_2.png) [@opus](https://community.openhab.org/u/opus)\
**Post date:** [May 18, 2019, 6:15pm UTC](https://community.openhab.org/t/solved-newbie-looking-for-help-with-scripts-rules/74686/6 "2019-05-18T18:15:00Z")

</div>

Since you have installed openhab with openhabian you also have some samba shares which you can connect to from the system you are using for the ssh connection.  
I suggest to use VisulalStudioCode with the openHAB extension to edit the files.

---

<div class="post-metadata">

**Author:** ![Alarm\_guy1](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@Alarm\_guy1](https://community.openhab.org/u/Alarm_guy1)\
**Post date:** [May 18, 2019, 6:40pm UTC](https://community.openhab.org/t/solved-newbie-looking-for-help-with-scripts-rules/74686/7 "2019-05-18T18:40:22Z")

</div>

Thankyou, i’m unsure of what you are telling me , but thankyou.

---

<div class="post-metadata">

**Author:** ![papaPhiL](https://community.openhab.org/letter_avatar_proxy/v4/letter/p/e99b99/32.png) [@papaPhiL](https://community.openhab.org/u/papaPhiL)\
**Post date:** [May 18, 2019, 8:59pm UTC](https://community.openhab.org/t/solved-newbie-looking-for-help-with-scripts-rules/74686/8 "2019-05-18T20:59:47Z")

</div>

what he is saying is that you can access the necessary item and rule files within windows, no nead for that mobaXterm tool you have shown.  
use windowskey+r and enter \openhabianpi

alternativly, you might want to try with ip address, or just the same detail that you used in mobaxterm. good luck

---

<div class="post-metadata">

**Author:** ![Alarm\_guy1](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@Alarm\_guy1](https://community.openhab.org/u/Alarm_guy1)\
**Post date:** [May 18, 2019, 9:22pm UTC](https://community.openhab.org/t/solved-newbie-looking-for-help-with-scripts-rules/74686/9 "2019-05-18T21:22:40Z")

</div>

Cheers, I am quite happy and comfortable with MobaXterm,  
I have created a file in /etc/openhab2/rules/light.rules  
this has the following script  
Switch KMotionPresence\_Timer { expire=“1m”, command=OFF }  
rule “Presence in Kitchen changed”  
when  
Item Front Hall MD changed  
then  
Front Hall MD\_Timer.sendCommand(ON) //Start timer whenever presence changes in the Kitchen  
end

rule “Kitchen Presence Timer expires”  
when  
Item KMotionPresence\_Timer changed to OFF  
then  
if(KMotionPresence == OFF && Front Hall Dimmer.state == ON) {  
logInfo(“RULE”, “Light switched OFF, because of no presence”)  
Front Hall Dimmer.sendCommand(OFF)  
}  
end

I now get this error on boot  
2019-05-18 22:16:58.337 [INFO] [el.core.internal.ModelRepositoryImpl] - Loading model ‘light.rules’

2019-05-18 22:16:58.380 [WARN] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘light.rules’ is either empty or cannot be parsed correctly!

any idea where it is going wrong cheers

---

<div class="post-metadata">

**Author:** ![anonymous.one](https://community.openhab.org/user_avatar/community.openhab.org/anonymous.one/32/56272_2.png) [@anonymous.one](https://community.openhab.org/u/anonymous.one)\
**Post date:** [May 18, 2019, 9:37pm UTC](https://community.openhab.org/t/solved-newbie-looking-for-help-with-scripts-rules/74686/10 "2019-05-18T21:37:41Z")

</div>

> [@Alarm\_guy1](#):
>
> Switch KMotionPresence\_Timer { expire=“1m”, command=OFF }

You don’t want to define an item within the rule file, you also need to create an item file for things or leverage Paper UI to create the item.

```php
rule “Presence in Kitchen changed”
when
Item Front Hall MD changed
then
Front Hall MD_Timer.sendCommand(ON) //Start timer whenever presence changes in the Kitchen
end

```

As for that rule while it will work, think through the logic, when Front Hall MD (also I think underscores are needed for items, not positive just something I’ve done based off examples of others and the doco) changes to ON or OFF it is going to activate the rule and case your timer item to turn ON again.

Also I also suggest using Visual code it will validate your work and help with narrowing down issues when your log reports it won’t load a rule file due to line item number 138 column 22. Using the editor will make things a lot easier.

---

<div class="post-metadata">

**Author:** ![opus](https://community.openhab.org/user_avatar/community.openhab.org/opus/32/55801_2.png) [@opus](https://community.openhab.org/u/opus)\
**Post date:** [May 18, 2019, 9:40pm UTC](https://community.openhab.org/t/solved-newbie-looking-for-help-with-scripts-rules/74686/11 "2019-05-18T21:40:27Z")

</div>

> [@Alarm\_guy1](#):
>
> Switch KMotionPresence\_Timer { expire=“1m”, command=OFF }

That line should not be in a rules file.  
You did not get any error. The first one is an Info, telling that a rules file got loaded (you probably created it at that moment. That second is a Warning telling that the newly created file is empty.  
You should see the loaded Info whenever you save that file.

---

<div class="post-metadata">

**Author:** ![Alarm\_guy1](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@Alarm\_guy1](https://community.openhab.org/u/Alarm_guy1)\
**Post date:** [May 18, 2019, 10:14pm UTC](https://community.openhab.org/t/solved-newbie-looking-for-help-with-scripts-rules/74686/12 "2019-05-18T22:14:10Z")

</div>

> [@anonymous.one](#):
>
> Switch KMotionPresence\_Timer { expire=“1m”, command=OFF }

Right slowly getting there, walk past the motion detector and the hall light comes on  
WoooooooHoooo a start.  
However the light does not go off  
I have an Item called KMotionPresence\_Timer.items in /etc/openhab2/items

with the line:  
Switch KMotionPresence\_Timer { expire=“1m”, command=OFF }  
in it  
Any ideas  
cheers

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [May 18, 2019, 11:45pm UTC](https://community.openhab.org/t/solved-newbie-looking-for-help-with-scripts-rules/74686/13 "2019-05-18T23:45:04Z")

</div>

> [@Alarm\_guy1](#):
>
> Switch KMotionPresence\_Timer { expire=“1m”, command=OFF }

There’s nothing in your rule to ever turn this Item on, so no reason for it to expire off.

---

<div class="post-metadata">

**Author:** ![anonymous.one](https://community.openhab.org/user_avatar/community.openhab.org/anonymous.one/32/56272_2.png) [@anonymous.one](https://community.openhab.org/u/anonymous.one)\
**Post date:** [May 19, 2019, 12:00am UTC](https://community.openhab.org/t/solved-newbie-looking-for-help-with-scripts-rules/74686/14 "2019-05-19T00:00:23Z")

</div>

If you have it in an item file already, then you don’t need it within your rules file.

You may want to add some extra information for your when clause:

```php
Item Front Hall MD changed from OFF to ON

```

Borth OFF and ON values are optional for the statement, it depends on your particular scenario. Another option is to place state values under the then section and have different actions take place for OFF and different actions for ON.

---

<div class="post-metadata">

**Author:** ![Alarm\_guy1](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@Alarm\_guy1](https://community.openhab.org/u/Alarm_guy1)\
**Post date:** [May 19, 2019, 9:50am UTC](https://community.openhab.org/t/solved-newbie-looking-for-help-with-scripts-rules/74686/15 "2019-05-19T09:50:28Z")

</div>

Cheers,  
i may not deal with this anymore possibly for a good few days as work commitments will get in the way, but I will let you know once I have it licked  
cheers

---

<div class="post-metadata">

**Author:** ![Alarm\_guy1](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@Alarm\_guy1](https://community.openhab.org/u/Alarm_guy1)\
**Post date:** [May 20, 2019, 8:06pm UTC](https://community.openhab.org/t/solved-newbie-looking-for-help-with-scripts-rules/74686/16 "2019-05-20T20:06:50Z")

</div>

Right, I have had a mess about, I have found this script that should meet my needs:

` var Number counter = 0  
var Number lastCheck = 0

```
rule "corLightOn"
when   
        Item FrontHallMD_MotionAlarm changed from OFF to ON or
        
then   
        counter = counter + 1
        if(FrontHallMD_SensorLuminance.state < 40) {
                sendCommand(zwave_device_dab61663_node7_switch_dimmer1, ON)
        }
end

rule "corLightOff"
when   
        Time cron "0 * * * * ?"
then   
        if(lastCheck == counter) {
                counter = 0
                lastCheck = -1;
                sendCommand(zwave_device_dab61663_node7_switch_dimmer1, OFF)
                sendCommand(FrontHallMD_MotionAlarm, OFF)
        } else {
                lastCheck = counter
        }
end`

```

When loading I get the following error:

2019-05-20 21:02:36.197 [INFO] [el.core.internal.ModelRepositoryImpl] - Loading model ‘Hallway\_Lights\_test.rules’

2019-05-20 21:02:36.202 [WARN] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘Hallway\_Lights\_test.rules’ is either empty or cannot be parsed correctly!

2019-05-20 21:02:37.370 [WARN] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘Hallway\_Lights\_test.rules’ has errors, therefore ignoring it: [8,5]: no viable alternative at input ‘then’

cheers

---

<div class="post-metadata">

**Author:** ![opus](https://community.openhab.org/user_avatar/community.openhab.org/opus/32/55801_2.png) [@opus](https://community.openhab.org/u/opus)\
**Post date:** [May 20, 2019, 8:16pm UTC](https://community.openhab.org/t/solved-newbie-looking-for-help-with-scripts-rules/74686/17 "2019-05-20T20:16:43Z")

</div>

Remove the “or” from the when part of the first rule and your variable counter is not declared.

If x you would use Visual studio code as an editor, those syntax errors would be noted before saving!

---

<div class="post-metadata">

**Author:** ![Alarm\_guy1](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@Alarm\_guy1](https://community.openhab.org/u/Alarm_guy1)\
**Post date:** [May 20, 2019, 8:24pm UTC](https://community.openhab.org/t/solved-newbie-looking-for-help-with-scripts-rules/74686/18 "2019-05-20T20:24:11Z")

</div>

Many thanks i believe that has it licked cheers. I am using "Visual Studio code " but this didnt seem to throw up an error, also for some reason I cannot get Samba to work, I have been through lots of tutorials, but i’m getting there. many many thanks for all he help
