Lights up when two requirements are met ... AND in rules

I want to create a new rule like this:

  • IF (1) the TV is switched off
  • AND (2) lamp2 is already on
  • THEN turn on lamp1 as well

I haven’t really found anything about AND relationships except this info:

Specifically, however, I lack the understanding of getting started - a rule requires a “WHEN” … to install the above logic, however, I need an “IF”.

Would be great if someone could help me …


1 Like

In the rule body, you can use if/then statements. For example…

rule "Test DSL rule"
    // These are your triggers
    Item Test_Lamp_1 changed
    Item Test_Television changed
    logInfo("Test Rule", "Test: Start")
    if (Test_Lamp_1.state == ON && Test_Television.state == OFF) {
    logInfo("Test Rule", "Test: End")

Jython version…

from core.rules import rule
from core.triggers import when

@rule("Test Jython rule")
@when("Item Test_Lamp_1 changed")
@when("Item Test_Television changed")
def test_rule(event):"Test: Start")
    if items["Test_Lamp_1"] == ON and items["Test_Television"] == OFF):
        sendCommand("Test_Lamp_2", "ON")"Test: End")
1 Like

Yes, a must have.
Revisiting your requirement …

IF (1) the TV is switched off

Does that mean when the TV is switched off?
That would be a straightforward rule trigger.

But it’s ambiguous …

Does that mean anytime that lamp2 is turned on AND the TV is off?
That would be another straightforward trigger.

Don’t forget you can trigger a rule from multiple events, so you might trigger from both TV changing or light2 changing.

1 Like

I’m making a short (well, maybe not that short, but it’s not like we don’t all have more time) video about how to setup openHAB2, which includes a tiny section on DSL rules and NodeRed.

While I’m not going to cover linking different protocols, the whole “if this is ON AND I switch this ON, something else should come ON” basic rule will be in there.

What I’ll do is add an index of the video in the description, just so you don’t have to watch everything.

When it’s online, I’ll post the link here.

Update - 23rd April 2020