Rule not testing item state correctly

Platform Rpi4, OH3

I have tried to port a working rule from OH2 but it is not behaving in the same way.

I have a switch (which determines whether the rule is active) which is in state ON:


This is the first few rows from the rule. It gets past “triggered1” but never gets to “triggered2”.

rule "TPIRuleBR"
when
    Item BackRoomSetPoint changed
then
    logInfo ("TPIBackRoom  ", "triggered1")
    if (BackRoomHeatingLogic.state == ON)
    {
        logInfo ("TPIBackRoom  ", "triggered2")

The equivalent rule / switch works fine in OH2. What am I doing wrong?

Log out what the Item state is inside the rule.

Changed code to:

rule "TPIRuleBR"
when
    Item BackRoomSetPoint changed
then
    logInfo ("TPIBackRoom  ", "switch state is: "+ BackRoomHeatingLogic.state)
    if (BackRoomHeatingLogic.state == ON)
    {
        logInfo ("TPIBackRoom  ", "triggered2")

Output to log is now:

2021-01-08 09:01:29.390 [INFO ] [nhab.core.model.script.TPIBackRoom  ] - switch state is: ON

==> /var/log/openhab/events.log <==

2021-01-08 09:01:29.375 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'BackRoomSetPoint' received command 17.5

2021-01-08 09:01:29.383 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'BackRoomSetPoint' changed from 17 to 17.5

i.e. still doesn’t seem to be testing the state properly.

BackRoomHeatingLogic is for sure a Switch Item? It’s not a String or a Dimmer or something like that?

What happens if you us toString?

if (BackRoomHeatingLogic.state.toString == "ON")

Hi. Yes it does work correctly if, as you say, I convert to and test the state as a string. I will have a play and see if I can replicate a simple rule generated directly in the GUI.

I have now realised that i set up the “switch” as a String type item. Changed it to a Switch type item and the code now works correctly. Thanks for your help again.