Help with rules in OH3

Hi there
the migrating to OH3 for me seemed like a good time to do things properly, where in OH1 and OH2 I was basically building things as they came along rather than having a concept.

Having said that, I would not consider myself a newbie, but I still cannot code in any way - most of the rules I have (I have about 60) where adapted from other rules I found in this forum (a big thanks to all in here, by the way!)

Now, in OH2 I had the following requirement: Whenever I switched my alarm system (a simple switch in OH) to “ON” it would notify me of any doors/windows which were still open.
Being someone who cannot code, this was achieved by one rule for every sensor, which basically said:

when
    Item Alarm changed from OFF to ON
   
   then
    if (Sensor_State.state == OPEN) {
 
    sendNotification ("e-mail","Sensor is open")
    }
    end

Now, I figured that could be done easier with one rule and I think I could probably do this with the old rule engine by now (using a combination of if/or actions).
I was expecting that could easily be done with blockly, but apparently not so - I can’t even create a simple “If item-state equals ON then do something” usin blockly or the “regular” rule engine (because there is no “block” or “trigger” which fires upon an item state rather than a change in item state (nested rules, for exmaple, are also not possible in this way).
Am I overlooking someone or does someone have an idea how to achieve this?
Thanks

First of all have you reviewed [wiki] Getting Started with OH3: rewriting the tutorial - 8. Rules

The rule trigger isn’t covered in Blockly. That’s done outside the script action.

At a high level you want to put all your door is and window Items into a Group. it Then you can use Design Pattern: Working with Groups in Rules to filter and create a single message with all the open windows and doors.

Hi -
thanks for that. When I referred to “trigger” I was well aware that the rule trigger itself is done outside the script - I was referring to “nested” triggers, i.e. “if” statements.

Nevertheless, thanks for the hint with the design patters and working with groups in rules - that sounds exactly like what I need. I’ll definitely try that.

Maybe just one more thing, as I’ve just noticed that.

I’ve tried to create a rule with Blockly, the original OH2 rule was as follows:

when
    Item BWM_WZ changed
then
if (BWM_WZ.state == ON) {

if (BWM_WZ_Helligkeit.state < 40) {

   T_EG_LED_DIM.sendCommand(40)
  }

}
else if (BWM_WZ.state == OFF) {

 T_EG_LED_DIM.sendCommand(OFF)

}

end

The trigger was easy. But then - in the built-in rule editor there is a “only if” but not a “else if”.
I tried blockly.
There is a block which says “if…do”
But there is no OpenHAB block corresponding to this, i.e. “if item_state = X,” which I could “dock” to the IF function of the logic block, is there?

Then your terminology is way off which has met concerned that your understanding might be as well.

If statements don’t trigger. There is nothing that kicks then off. A rule, even in Blockly I think, runs from top to bottom. So you would create an if block and set the conditions. If there conditions are met, the code attached to the if block ribs. Otherwise it’s skipped. I still need to do some work with Blockly but that much should be pretty standard across all languages.

So, an if statement doesn’t get triggered by a change. That isn’t a concept that exists in openHAB. Instead, the rule gets triggered by a change in the Item. Then in the code that runs you can have an if statement that checks where it not a condition is met.

You can add a Script Condition and have as many if/else if/else statements as you want. You can also add a Script Action with as many if/else if/else statements as you want. The to Condition though is a place to define that conditions that must be true to allow the rule to run. There is no place to say 'if the condition is false do something else." If you want that, move the if statements inside a Script Action.

1 Like

Yes, apparently my terminology is way off, because everything you have provided in your answer I am fully aware of. It must have been my explanation, sorry if it was bad. Let me try again with an example.

when
Item BWM_WZ changed
then
if (BWM_WZ.state == ON) {

if (BWM_WZ_Helligkeit.state < 40) {

   T_EG_LED_DIM.sendCommand(40)
  }

}
else if (BWM_WZ.state == OFF) {

 T_EG_LED_DIM.sendCommand(OFF)

}

end

This is my rule - very simple, I believe: when an item changes then do something, but only if a certain condition is met, otherwise do something else (I think the last part of the sentence (the part starting with otherwise) is the crucial part here.

All I am saying is that even though this is a very simple rule, I cannot do this in blockly, because there is no "If " block.

I know I can do this with script condition, in fact, I have migrated the above rule to OH3 easily.
Again, I am not asking a question here. I’m just noticing that this sort of condition seems rather common, but yet there is no blockly “block” for this.

It has been said that one of the objectives of OH3 was to make it easier for “Newbies” to configure. If for a rule which is as simple as the one I have posted here a script is needed, that - in my opinion - is not very “newbie”-friendly.

However, it could be that I am wrong and hence my question whether I have overlooked something.

Thank you.

It would be like the following in Blockly

Or if you use a Script Action using Rules DSL:

if (BWM_WZ_Helligkeit.state < 40) {

   T_EG_LED_DIM.sendCommand(40)
  }

}
else if (BWM_WZ.state == OFF) {

 T_EG_LED_DIM.sendCommand(OFF)

}

Or in JavaScript

    if(items["BWM_WZ_Helligkeit"] < 40) {
        events.sendCommand("T_EG_LED_DIM", "40");
    }
    else if(items["BWM_WZ"] == OFF){
        events.sendCommand("T_EG_LED_DIM", "OFF");
    }

It’s right there under “Logic”. When you click on the cog icon you can add else if and else clauses to the if statement.

I think this is the source of my confusion because clearly there is. See my screenshots above.

1 Like

Thanks - now I do understand, and I do have to apologize myself.
I did find those blocks you show in your screenshot, but looking at the code it looked very much different from what I used to - but of course, it’s a different language than the “old” OH2 world.

Sorry again for the confusion

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.