Rule does not work propperly after upgrade to OH3

Hi,
I upgraded OH2.5 to OH3 on my RaspPi 3B+.
The following rule worked great under OH2.5 in any situation.

This rule is to control a light by using UI, Alexa and a xiaomi smart switch. Usually I ask Alexa to switch on the light while going to bed or I switch it on via Sitemap/UI. While in bed I switch it off with the Xiaomi switch.

Since the upgrade, I cannot switch the light off when switched on before via Alexa or UI/Sitemap. If the light is allready OFF, I can turn it ON and OFF as I like with the Xiaomi switch.

See my code attached:

rule "Nachttischlampe an/aus"
when
    Item SZ_Nachttisch_OnOff received command or
    Channel "deconz:switch:homeserver:SZ_SmartSwitch:buttonevent"  triggered 1002
then
    if (receivedCommand == ON || SZ_Nachttisch.state == 0){
        SZ_Nachttisch.sendCommand(Nachttisch_dimm)
        SZ_Nachttisch_OnOff.postUpdate(ON)
    }
    else{
        Nachttisch_dimm = (SZ_Nachttisch.state as DecimalType).intValue
        SZ_Nachttisch.sendCommand(0)
        SZ_Nachttisch_OnOff.postUpdate(OFF)
    }
end
        /* SmartSwitch Events:
            1002 	One 	single short press
            1004 	One 	double short press
            1001 	One 	hold
            1003 	One 	hold release
            1007 	One 	shake
        */

In my Log-file I can see that If the light is ON, the rule fires just the first IF statement. So the Xiaomi allways sets the light to the value “(Nachttisch_dim)”.

2021-03-30 10:59:13.454 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'SZ_Nachttisch_OnOff' received command ON

2021-03-30 10:59:13.461 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'SZ_Nachttisch_OnOff' changed from OFF to ON
--> Here I switched the light ON via Sitemap
2021-03-30 10:59:13.467 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'SZ_Nachttisch' received command 52

2021-03-30 10:59:13.474 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'SZ_Nachttisch' predicted to become 52

2021-03-30 10:59:13.490 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'SZ_Nachttisch' changed from 0 to 52
--> The light was set to the Value "Nachttisch_dimm"
2021-03-30 10:59:13.782 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Datum' changed from 2021-03-30T10:59:12.768+0200 to 2021-03-30T10:59:13.770+0200

2021-03-30 10:59:14.782 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Datum' changed from 2021-03-30T10:59:13.770+0200 to 2021-03-30T10:59:14.772+0200

2021-03-30 10:59:15.783 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Datum' changed from 2021-03-30T10:59:14.772+0200 to 2021-03-30T10:59:15.774+0200

2021-03-30 10:59:16.678 [INFO ] [openhab.event.ChannelTriggeredEvent ] - deconz:switch:homeserver:SZ_SmartSwitch:buttonevent triggered 1002
--> the Xiaomi switch was pressed
2021-03-30 10:59:16.691 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'SZ_Nachttisch' received command 52

2021-03-30 10:59:16.701 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'SZ_Nachttisch' predicted to become 52
--> here the rule wants to set the light ON again

So for me it seems, that the Event-Trigger still interpretes the state of “SZ_Nachttisch_OnOFF” (it’s ON in this error case) in the “receivedCommand” all the time.

Do I have overseen something?

Thanks for your help!
cheers Hannes

PS: I’m still using Text-files for all my configs. Did not move to UI yet and I’m not sure if i will.

Yes.
I’m surprised your rule worked in OH2.5, mixed trigger types and implicit variables do not always play nicely.

There is a bug in OH 3.0 where the events is not cleared from one rule run to the next, which shows up with mixed trigger types

Thanks for your fast reply,
didn’t know for what to look out in the issues.

Then I’ll wait till the fix is implemented.

Thx
Hannes

I think you might have missed the first part of rossko57’s comment. There is no guarantee that your rule will work in OH 3 even after that bug gets fixed. And given that it’s been a few months it might take some time before that bug does get fixed.

When your rule triggers from the Channel trigger, receivedCommand is not defined. So even in OH 2.5, this rule should throw an error when that occurs. If it didn’t throw an error in 2.5, that was actually a bug. So it will be far better for you to deal with this properly because there is no guarantee that it will continue to work forever as it depends on a bug in the code remaining.

The best approach though will largely depend on the behavior of SZ_Nachttisch_OnOff. When it receives the command does it change from ON to OFF every time? If so use a changed trigger. If not does the Item receive periodic updates from what ever it is linked to? If not use received update instead.

In either case you can remove the test on receivedCommand and the rule will work no matter how it’s triggered.

Ya, didn’t saw the big problem. Thanks

SZ_NachttischOnOff is a virtual switch in the Sitemap. So I want it in the right state all the time.
My aproach will be, that the Xiaomi switch will control SZ_Nachttisch_OnOff. So there will be two rules as seen below, unfortunately.
It’s a kind of stairway-switch-Problem, but with the difference, that I don’t have two normal switches, but one event-trigger and a switch.

rule "Nachttischschalter an/aus"
when
    Channel "deconz:switch:homeserver:SZ_SmartSwitch:buttonevent"  triggered 1002
then 
    if (SZ_Nachttisch_OnOff.state == OFF){
        SZ_Nachttisch_OnOff.sendCommand(ON)
    }
    else{
        SZ_Nachttisch_OnOff.sendCommand(OFF)
    }
end

rule "Nachttischlampe an/aus"
when
    Item SZ_Nachttisch_OnOff changed
then
    if (SZ_Nachttisch_OnOff.state == ON ){
        SZ_Nachttisch.sendCommand(Nachttisch_dimm)
    }
    else{
        Nachttisch_dimm = (SZ_Nachttisch.state as DecimalType).intValue
        SZ_Nachttisch.sendCommand(0)
    }
end

This code is working, but do you see an option to get both rules into a single one?

Thanks for your help!

Hannes

That’s okay, they don’t cost much. Don’t worry about that.

1 Like

I see no reason to merge it into one rule. The two rules are nice and short and much easier to understand than a single merged rule will ever be.

You could be a little clever and reduce your first rule to a single line.

SZ_Nachttisch_OnOff.sendCommand(if(SZ_Nachttisch_OnOff.state == OFF) ON else OFF)

But that’s not required and may not be easier to understand in the long run, but it’s shorter.

1 Like

Thx @rlkoshak and @rossko57, the two rules and it’s working great!

Hannes