Cascading rules - timing issues

Hi,

I have three cascading rules:

rule "Nachtmodus auto-deaktivierung"
        when Time cron "0 0 9 * * ?"
then
        logInfo("Nachtmodus", "Regel Nachtmodus auto-deaktivierung")
        Nachtmodus.sendCommand(OFF)
end

rule "Nachtmodus aus"
        when Item Nachtmodus received command OFF
then
        logInfo("Nachtmodus", "Regel Nachtmodus aus")
        Galerie_LA18_permanent_aus.sendCommand(OFF)
        Bad_LA22_permanent_aus.sendCommand(OFF)
end

rule "LA22_auto"
        when Item Bad_LA22_permanent_an received command OFF 
        or Item Bad_LA22_permanent_aus received command OFF
then
    if (Bad_LA22_permanent_an.state == OFF && Bad_LA22_permanent_aus.state == OFF)
        {
                Bad_LA22_auto.sendCommand(ON)
                Bad_BWM_Sperre.sendCommand(OFF)
                Bad_LA22_phys.sendCommand(OFF)
        }
end

A bit of explanation (skip this, if you don’t want to read it ;-)):

I have several lamps which are controlled by motion detectors (auto state).
For these lamps, I also have a permanent_on and permanent_off state.
Because I want to handle all states on KNX level as well, I have one item for each state (permanent_an, permanent_aus and auto), each of which are also mapped to a KNX group address.
This is why I have logic for managing the three states in OH:

  • when user switches to permanent_on -> auto and permanent_off will be set to OFF
  • when user switches to permanent_off -> auto and permanent_on will be set to OFF
  • when user switches either permanent_on or permanent_off to OFF so that both are OFF, set auto to ON.

(the first two rules are not quoted above, because they do not contribute to the problem here).

On top of this simple logic (which works fine), I also have a more global night mode in my house which is usually activated by pressing a switch when we go to bed. This puts the lamps on the second floor into permanent_off state (works fine as well).

In the morning, the night mode is automatically switched OFF (rule “Nachtmodus auto-deaktivierung” above), but we also have the option to deactivate it manually (hence the rule split, so the rule “Nachtmodus aus” is executed both for automatic and manual cases).

My Problem

Now to my problem. In the morning I would expect that

  • rule “Nachtmodus auto-deaktivierung” is executed by cron schedule.
  • this triggers rule “Nachtmodus aus” (to switch off the permanent_off items)
  • this triggers rule “LA22_auto” (to actually switch the physical items and to switch on auto mode).

The rules are triggered as expected, BUT this condition often fails:

if (Bad_LA22_permanent_an.state == OFF && Bad_LA22_permanent_aus.state == OFF)

This leads to the night mode being deavtivated and also the permanent_off state being deactivated eventually, but the auto state is not activated, so the state is more or less undefined (because all three state items are in OFF state …)

I put in some debug logging statements and even though the rule is triggered by when Item Bad_LA22_permanent_aus received command OFF, the Bad_LA22_permanent_aus.state is ON in the check more often than not.
(Adding a log statement before makes the rule more stable, so I suspect there is some kind of race condition happening).

For some reason, this cascade used to work a few months back (I was on OH 1.8 back then, but also was using a Raspberry Pi 2. Whereas now I am using OH2 on a PINE A64…)

Can anyone tell me if this is a bug or expected behavior?
Should I write my rule differently to make it more stable?
(I have now put a Thread::sleep(250) before the if-statement. Let’s see how this goes tomorrow … :wink:

Best,
Stefan

Is this a more complicated version of this post?

Wow, that was quick :wink:

Yes, indeed it seems I fell for the same misunderstanding as is mentioned in that other post:

I don’t believe this is is a bug, just a misunderstanding that sending a command to an Item may not instantly (or indeed ever) change its state.

I’ll try to change the rule trigger and see if this solves the issue.
Thanks!