MQTT Action service ThingHandler connection is null

I have a rules file like this:

val mqttActions = getActions("mqtt","mqtt:broker:myUnsecureBroker")
rule "alarmdisabled"
when
    Item StatusArmed changed to 0
    or
    Item CamEnable changed to OFF
then


    mqttActions.publishMQTT( "eufy/cmd", "{\"command\": \"station.set_guard_mode\", \"messageId\": \"set_guard_mode\", \"serialNumber\": \"Txxx\",\"mode\":2 }",false) 
end

Occasionally it stops working and I see the following in the logs:

2022-08-18 09:31:42.451 [WARN ] [ing.mqtt.internal.action.MQTTActions] - MQTT Action service ThingHandler connection is null!

Re-saving the rules file so that it is re-loaded fixes the problem for a while. Is there an issue with MQTT in 3.3? I use MQTT for receiving so much other logged stuff so I think the connection to my broker is fine (there are no other errors or dropouts)

Thanks!

No, there is a misunderstanding on what happens when you call getActions. That call gives you a reference to a Thing that provides actions. But what if that Thing goes OFFLINE? What happens if the Thing doesn’t even exist yet or isn’t yet initialized when your .rules file is loaded? Obviously, mqttActions isn’t going to work because there was no Thing available at the time getActions was called, or the reference changed later because the Thing went offline and then came back online.

You must call getActions inside your rule, not outside. You need to get the current reference to the Thing, not what happened to exist when the file happened to be loaded.

I see. Thanks Rich!

This is an old thread but I thought I would mention it had started failing again for some reason, this time with the getAction inside the rule as @rlkoshak had suggested.
For example:

   val mqttActions = getActions("mqtt","mqtt:broker:myUnsecureBroker")
   if(mqttActions === null) {
     logError("MQTT_EB", "There is no Action!")
     return;
   }

The broker defined as:

Bridge mqtt:broker:myUnsecureBroker [ host="mqtt",secure=false ]

mqttActions was always null again no matter what I tried. To finally fix it (for now at least). I edited the mqtt.things file, changed the broker host to specify it by a different alias, let it reconnect, and then it started working again :slight_smile: It survived a restart also… Not sure if its a bug but this fixed it for me.

When it stops working is the MQTT Thing ONLINE? If the Thing is in any other state than ONLINE (e.g. OFFLINE, ERROR, etc.) getActions will return null.

Yes it was online. I rely on that for a bunch of sensor information and timestamp the update. The thing was green/online in mainui and my timestamp was up to date. Nothing would fix it other than resaving the thing with a different config.

I had updated my broker that the binding connects to a few weeks ago and hadn’t restarted OH. Maybe that upset it.