Rules triggering not working in OH3

No in Rules DSL you can pass either a String or the Command/State Object and all of those are imported. So MyItem.sendCommand(ON) and MyItem.sendCommand("ON") will both work. It’s only the other languages which have to use the events Class’s actions to issue events directly on the event bus instead of through the Item where the String is required. sendCommand('MyItem', 'ON').

They are required under the following two circumstances:

  1. when using the actions instead of the methods on the Items
  2. when Rules DSL can’t figure out how to coerce the command/update to a Command or State; Rules DSL sometimes can’t navigate the inheritance tree very well

To test triggers within OH3 (without the need for z2m) I created the next rules:

//---------------------------------------------------------------------------------------------------------------------------
rule "HOB_burolampState_changed"
when
        Item HOB_burolampState  changed
then
        var loginfo_rulename = String::format( "%-54s, ", "RULE=HOB_burolampState_changed" )

        logInfo( loginfo_rulefile, loginfo_rulename + "switched to  xxxx" )

end
//---------------------------------------------------------------------------------------------------------------------------
rule "HOB_burolampState_changed_to_ON"
when
        Item HOB_burolampState  changed to ON
then
        var loginfo_rulename = String::format( "%-54s, ", "RULE=HOB_burolampState_changed_to_ON" )

        logInfo( loginfo_rulefile, loginfo_rulename + "switched to  ON" )

end
//---------------------------------------------------------------------------------------------------------------------------
rule "HOB_burolampState_changed_to_OFF"
when
        Item HOB_burolampState  changed to OFF
then
        var loginfo_rulename = String::format( "%-54s, ", "RULE=HOB_burolampState_changed_to_OFF" )

        logInfo( loginfo_rulefile, loginfo_rulename + "switched to  OFF" )

end
//---------------------------------------------------------------------------------------------------------------------------

The openhab.log shows shows that the rules have triggered:

your code goes here==> /opt/openhab3/userdata/logs/events.log <==
2023-04-03 10:23:35.227 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'HOB_burolampState' received command OFF
2023-04-03 10:23:35.228 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'HOB_burolampState' predicted to become OFF
2023-04-03 10:23:35.229 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'HOB_burolampState' changed from ON to OFF

==> /opt/openhab3/userdata/logs/openhab.log <==
2023-04-03 10:23:35.230 [INFO ] [el.script.        report_state.ruleS] - RULE=HOB_burolampState_changed_to_OFF                 , switched to  OFF
2023-04-03 10:23:35.231 [INFO ] [el.script.        report_state.ruleS] - RULE=HOB_burolampState_changed                        , switched to  xxxx

==> /opt/openhab3/userdata/logs/events.log <==
2023-04-03 10:23:41.885 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'HOB_burolampState' received command ON
2023-04-03 10:23:41.885 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'HOB_burolampState' predicted to become ON
2023-04-03 10:23:41.886 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'HOB_burolampState' changed from OFF to ON

==> /opt/openhab3/userdata/logs/openhab.log <==
2023-04-03 10:23:41.887 [INFO ] [el.script.        report_state.ruleS] - RULE=HOB_burolampState_changed                        , switched to  xxxx
2023-04-03 10:23:41.889 [INFO ] [el.script.        report_state.ruleS] - RULE=HOB_burolampState_changed_to_ON                  , switched to  ON


But pressing the switch shows in the /var/log/messages file:

Apr  3 10:27:34 ohbserver3 npm[2604]: Zigbee2MQTT:info  2023-04-03 10:27:34: MQTT publish: topic 'zigbee2mqtt/schakelaar_50', payload '{"action":"single_left","battery":100,"device_temperature":29,"linkquality":165,"power_outage_count":164,"voltage":3005}'
Apr  3 10:27:52 ohbserver3 npm[2604]: Zigbee2MQTT:info  2023-04-03 10:27:52: MQTT publish: topic 'zigbee2mqtt/schakelaar_50', payload '{"action":"single_right","battery":100,"device_temperature":29,"linkquality":150,"power_outage_count":164,"voltage":3005}'

But no rules are triggered.

I think I might know the problem.

Your Zigbee2mqtt sent this:

{"action":"single_left","battery":100,"device_temperature":29,"linkquality":165,"power_outage_count":164,"voltage":3005}

But your Openhab Thing config expected this:

      Type string : action                      [ stateTopic="zigbee2mqtt/schakelaar_50/action" ]

I have two suggestions for you:

  1. Change Zigbee2mqtt config output: attribute:
advanced:
  output: attribute

That should solve the problem.

  1. This is something I only learned muuuuuch too late in the game.
    Change your Things config to add postCommand=true just for the action one, like this:
Type string : action [ stateTopic="zigbee2mqtt/schakelaar_50/action", postCommand=true ]

And in your rule, you should trigger on received command

rule "xxx"
when
  Item schakelaar_50action received command
then
  ....
end

For years I didn’t know this trick - well because I hadn’t read the docs properly, and I used “updated”, then “changed” and had to resort to trickery to fight a behaviour where when something reloaded (e.g. the .things file refreshed), my item would get an update even though the actual button wasn’t pressed. The postCommand solved that issue.

Oh one more:

Have a look at JSScripting and JRuby for your scripting.

2 Likes

Thanks Jim, that suggestion solved my problem!
In my OH2 environment I also had this setting, but due to the fact that I wanted to start with a very clean and minimalistic environment I apparently skipped this setting and forget about it, not knowing what its function is.
I will look for the documentation that describes this setting “output: attribute”.
Your second suggestion: I will apply that to the other things in the migration to OH3.

Thanks again, and also thanks to everybody else that contributed to this issue.
I can now continue with the migration from OH2 to OH3.