MQTT not triggering rule

Hi all,

I am having trouble getting a rule to trigger with mqtt.

My setup:
*Mosquito running on Raspberry Pi
*Openhab 2.5.0 Build #1589 running on Windows
*I have a generic mqtt thing with multiple channels receiving the mqtt commands.
*Switch items linked to the thing channels.
*Rule set to change a separate item switch based on the state of one of the mqtt controlled switches.

If I manually operate the first switch the second will follow as I have set in the rule but if the first switch is operated by mqtt the second will not follow. What am I missing? Is there a difference in how an item state is updated between mqtt and manual operation?

Config files below.

.things

Bridge mqtt:broker:myUnsecureBroker [ host="192.168.1.107", secure=false ]
{
    Thing mqtt:topic:secritySystem {
    Channels:
        Type switch : arm_Away "Alarm System Away" [ stateTopic="home/security/bosch_away", commandTopic="", on="1", off="0" ]
        Type switch : arm_Home "Alarm System Home" [ stateTopic="home/security/bosch_home", commandTopic="", on="1", off="0" ]
        Type switch : arm_Trigger "Alarm System Trigger" [ stateTopic="home/security/bosch_trigger", commandTopic="", on="1", off="0" ]
        Type switch : sirens "Internal Sirens" [ stateTopic="", commandTopic="home/security/sirens", on="1", off="0" ]
        Type switch : sirens_Test "Internal Sirens Test" [ stateTopic="", commandTopic="home/security/sirens_test", on="1", off="0" ]
    }
}

.items

Switch Security_Away "Security Away Status" {channel="mqtt:topic:secritySystem:arm_Away"}
Switch Security_Home "Security Home Status" {channel="mqtt:topic:secritySystem:arm_Home"}
Switch Security_Trigger "Security Trigger Status" {channel="mqtt:topic:secritySystem:arm_Trigger"}
Switch Security_Sirens "Security Sirens" {channel="mqtt:topic:secritySystem:sirens"}
Switch Security_Sirens_Test "Security Sirens Test" {channel="mqtt:topic:secritySystem:sirens_Test"}

.rules


rule "Security alrams trigger"
when 
    Item Security_Away received command
then
    if (receivedCommand == ON) {
        Security_Sirens.sendCommand(ON)
    }    
    else if (receivedCommand == OFF) {
        Security_Sirens.sendCommand(OFF)
    }
end

I have also tried a very basic rule to see if it was triggering at all but this didn’t work either.

rule "Security alrams trigger"
when 
    Item Security_Away received command
then
        Security_Sirens.sendCommand(ON)
end

Can you operate each swith from an UI without a rule?
Did you write the rules in VisualSudioCode? That way syntax problems would be highlighted. Or, did you check the logs when the rule was saved?
Consider using “logInfo” statements in the rule the see if and how far a rule is run.

Hi opus,

Yes to all your questions.

I spent some more time watching live updates of the logs. I noticed when manually clicking the switch the event log logged “received command ON” but when the switch was operated by mqtt the log said “changed from OFF to ON”. I changed the trigger to

Item Security_Away changed from ON to OFF

The rule is now functioning as expected.

I home this helps someone else in the future.

I am yet to use log statements. Are you able to provide example code of a log statement? I have had a read though the documentation but haven’t got one to work yet - I was focused on resolving my other issue.

Put that as the first line after “then”

logInfo ("MyRule","My rule triggered!")