OH2: Need help on rules that require multiple conditions to be true

Hi there.

I have a few generic questions to start with:

  1. Are there differences in textual rule design between OH1 and OH2? I found a few websites which all referred to OH1 and I got the impression, the syntax differed slightly.
  2. I neither could find any useful (text or video) tutorials regarding ruling (Habmin-GUI or textual) nor an overview regarding the syntax and commands.
    Any tips are appreciated. I’d prefer German websites, videos or tutorials, English works just as fine.

My main problem:
I need some rules that say e.g.:

IF time is after 5 am AND time is before 10 am AND rollershuter sleeping room receives command UP THEN send command UP to all other rollershutters

IF frontdoor opens_ AND any.window is open THEN turn hallway light to red

IF fire deteceted AND nobody is home THEN _open all rollershutters AND switch on all lights ELSE _open all rollershutters AND switch on all lights AND send notification “fire alert”

I’ve tried both, text and habmin gui and failed so far.
What I found out is that multiple WHEN-conditions linked with AND won’t work.

Thanks in advance.
Sven

The approach you need to take is that e.g. WHEN an item changes state THEN if this and this etc. So you have one trigger and when that triggers you check for other conditions to be met.

So in my first example the WHEN-part is the rollershutter and i need to check the time in the IF-part.
How do I check for a given time of day in the IF-part?
I have to admit i’m fairly new to rule-scripting.

Thanks to your help I found the solution:

I used the rules from rule examples and altered it to my needs.

Only a few minor changes. You no longer need to import anything from org.joda and org.openhab. See the The Migration Tutorial for a list of everything that is different.

http://docs.openhab.org/configuration/rules-dsl.html
This is a good place to start.

Rule triggers are event based, not state based. No two events will occur at exactly the same time so and is not supported in triggers.

What you want is to have a triggering event in the when and if/else in the body of the rule to test for state.

One thing you might find useful in a lot of these sorts of situations is to separated it the calibration of state into their own rules and store the state in an item. See the [Time of Day design pattern] ([Deprecated] Design Pattern: Time Of Day) for a good illustration of this using time.

Happy to have helped! And sorry I couldn’t reply sooner with extra aid, but good that you found a working solution.

Mikael

Hello @Svolli

Rules follow object-oriented prgramming.

For your examples

rule "Rollershutter sleeping room"
  when
    Item rollershutter_sleeping received command UP
  then
    if ((now.getHourofDay > 5) && (now.getHourofDay < 10)) {
         rollershutter_sleeping.sendCommand(UP)
  }
end
rule "Alert if Windows are open"
when
   Item Frondoor changed to OPEN
then
  if(gWindows.state == OPEN) {
    hallwaylight.sendCommand(RED)
  }
end
rule "Fire-Detection"
when
  Item Fire-Alert received command
then
  rollershutters.sendCommand(UP)
  lights.sendCommand(ON)
  if (Presence.state == ON) {
   sendNotification
  }
end

This code snippets are written out of brain and are not functional. They should give you only an idea how rules are built.

Thanks. I’ve learned a lot!

The above helped me to create a simple toggle in a protocol translation between a Velbus button press and a DMX Chaser switch.

The requirement was for a single button press to either switch on the DMX chaser, or Switch it off, based on the current state of the Chaser Switch.

While it hiccups a couple of times, the following rules seems to do what I had in mind.

Any corrections would be most welcome.

rule "Button  4 of  Velbus OLED Glass panel"
when

	Channel 'velbus:vmbgpod:c5053467:20:input#CH4' triggered PRESSED
then

	
	if(DMXChaser_Switch.state == OFF){
	 DMXChaser_Switch.sendCommand(ON)
	 //say("Pressed to switch on", "voicerss:enGB", "webaudio")
    }


	
	if (DMXChaser_Switch.state == ON){
	 DMXChaser_Switch.sendCommand(OFF)
	 DMXCColorRGBDimmerTVRoomLamp1_Color.sendCommand(OFF)
	 //say("Pressed to Switch Off", "voicerss:enGB", "webaudio")

 }
 
end