openhab-scripters / openhab-helper-libraries

Scripts and modules for use with openHAB
Eclipse Public License 1.0
88 stars 70 forks source link

[Feature Request] Add a condition decorator #47

Open andiwirs opened 5 years ago

andiwirs commented 5 years ago

Hello, for quite some time i thought about logical AND-links for triggering rules.

OR-links are already possible with successive @when decorators

@when("Item Item1 changed")
@when("Item Item2 changed")
...

but how about a corresponding @and decorator to a @when?

As one of many examples i have a rule that should notify me when a door or window was opened while i am on holiday. It triggers every time a door or window is opened:

@rule("rNotificationDoorWindowOpenedDuringHoliday")
@when("Descendent of gWindowsDoors changed to OPEN")
def rNotificationDooWindowrOpenedDuringHoliday(event):
    if items.Sw_sRules_HolidayMode == ON:
        # some warning

But i would prefer it would only trigger when i am away, because this would not flood the log that much:

@rule("rNotificationDoorWindowOpenedDuringHoliday")
@when("Descendent of gWindowsDoors changed to OPEN")
@and("Item Sw_sRules_HolidayMode  == ON")
def rNotificationDooWindowrOpenedDuringHoliday(event):
    # some warning

Whats your opinion? Thanks a lot :)

mjcumming commented 5 years ago

Essentially, you are adding what the new rules engine calls a condition, ie the rule gets execute when the event happens and when certain conditions are met. I wonder if that could be added to the decorator?

5iver commented 5 years ago

Conditions could be added, and I've thought about adding them several times, but I always decide against it. It would be a significant amount of coding, and they really aren't necessary for JSR223 scripting, since you can very easily just add them to the script... like in your example. But thank you for the suggestion. We can leave this open to see if anyone else would be interested in putting it together.

It sounds like the issue you are trying to solve is the amount of logging. If your logs are getting flooded, you probably have it still set to DEBUG... change the logging level to WARN (log:set WARN org.eclipse.smarthome.automation). Also, IIRC the rule will still be triggered when using conditions, but the action will not be executed, so there would still be trigger and rule log entries.

andiwirs commented 5 years ago

Thanks for your feedback. My logging is definitly set to WARN, so no chance with that approach :(

Regarding the conditions: If the rule would still trigger (and place a log entry) but not be executed then it would really not be better than a simple if condition in the action block.

5iver commented 5 years ago

Thanks for your feedback. My logging is definitly set to WARN, so no chance with that approach :(

Would you please paste in an example of the logs you're being flooded with? When logging level is set to WARN, there shouldn't be anything but errors, warnings, and log entries from your rules.

andiwirs commented 5 years ago

It is not the flodding what you're thinking :) I just have a lot of rules, which are triggered from various items and there i always get the entries that certain rules are running and then idle again. So no log entries where i don't know where they are coming from...

5iver commented 5 years ago

OK... so totally under your control :wink: