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

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

Welcome to the openHAB Forum!
How about reading here:
Documentation

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

And the tutorial states the following:
Rules are placed in the folder $OPENHAB_CONF/rules . The demo setup 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

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.

Have another browse around and find the files

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

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

I use Nano so will have a play

cheers

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.

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

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

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

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.

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.

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.

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

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

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:

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.

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

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

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!

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