mqttActions Error

OpenHAB 4.2.0 Release Build

Not sure if something updated or not, I am unsure when this stopped working in my rules.

I am having trouble publishing mqtt messages in my Rules DSL. Here is just a simple rule to verify it was not publishing anything. I have the MQTT Binding and MQTT Actions installed and still does not work.

	val mqttActions = getActions("mqtt","mqtt:broker:MosquittoMqttBroker")
	mqttActions.publishMQTT("Test/Test","Test1", true)
	logInfo("sonoffOH4.rules", "MQTT Test.")

I am getting

2024-07-28 20:38:35.755 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'SonoffOH4-2' failed: Cannot invoke "org.openhab.binding.mqtt.internal.action.MQTTActions.publishMQTT(String, String, java.lang.Boolean)" because "actions" is null in SonoffOH4

Nothing is being published to my MQTTbroker…

Thanks for any help.

I uninstalled and reinstalled the MQTT and MQTT Actions Binding and rebooted my Openhab Server and this started working again. Hopefully this might help someone.

There isn’t a separate MQTT Actions binding. What exactly did you install and how did you install it?

The original error is weird too. What does MQTT have to do with Sonoff?

This is what I reinstalled from the add on store through the web UI.

Sorry for the confusion when trying to diagnose the error. The oringinal error was from a rule in a SonoffOH4.rules file. It was telling me that it was the second rule in that file

I was just putting a simple rule in place of it to verify the MQTT Actions was not publishing to my MQTT server.

It errored out on the “actions.publishMQTT(device_id +”/cmnd/status", “0”)" part of the code.

Here was the oringinal rule, its just send a command to each sonoff device and if the ip address changes it will update it.


rule "Status0"
when
    Time cron "0 0 0/4 ? * * *" or // Every 4 Hours
	Item Sonoff_Status0 changed from OFF to ON
then 
	if (Sonoff_Status0.state==ON)	logInfo("sonoffOH4.rules", "Refreshing Sonoff Info.")
	val sonoff_device_ids = newArrayList(
    //... add all your modules here!
    "BabyCamera",
	"BackupServer",
	"BambuLabsP1S",
	"BambuLabsP1SLights",
	"BasementTV",
	"Computer",
	"DeepFreezer1",
	"DeepFreezer1Sensors",
	"DeepFreezer2",
	"DeepFreezer2Sensors",
	"DoubleGarageDoorSensors",
	"Dryer",
	"GarageHeater",
	"GarageFridge",
	"HolidayLights",
	"HouseFridge",
	"IceMachine",
	"ComputerMonitor",
	"Kettle",
	"KitchenChristmasLights",
	"LivingRoomTV",
	"LoganBottle",
	"MasterBedroomTV",
	"pbs2",
	"pve",
	"pveOffsite",
	"pvetest",
	"ServerRackFan",
	"ServerRackSensors",
	"SingleGarageDoorSensors",
	"Smoker",
	"SolderBench",
	"SolderBenchLights",
	"SonoffS31",
	"Windows10PC",
	"Washer"
	)
	val actions = getActions("mqtt","mqtt:broker:MosquittoMqttBroker")
	for (String device_id : sonoff_device_ids) {
		actions.publishMQTT(device_id +"/cmnd/status", "0")
		java.lang.Thread.sleep(500);
	}
end
/////////////////////////////////////////////////////////

Interesting.

OK, the MQTT Actions is a Block library so one can use publishMQTT in Blockly rules. That’s not necessary if you are not using Blockly.

If the MQTT Broker is in any state other than ONLINE, I’m pretty sure getActions returns null.

Was the Broker Thing ONLINE when this error occurred?

I cannot verify 100% but i’m guessing 99% it was ONLINE since OpenHAB runs all the MQTT rules for my Z-Wave JS UI and Zigbee2MQTT. I notice right away when motion light dont come on or anything zigbee, zwave or mqtt related is not properly working. This one I found when I had frontail up and I was randomly debugging a new Unifi Protect Camera.

I guess when in doubt just reboot lol feel like it fixes 90% of the issues sometimes ha ha

I have seen some services have difficulty reconnecting when tailscale goes up and down. If it happens again, check the Thing’s status. That’s usually the only reason for getting the action to return null. That or a typo in the Thing UID.

Sorry meant Frontail (web log viewer), I don’t know why it changed to Tailscale.

In case it helps anyone: Had the same error “actions is null” after a recent update.
Fiddling around, I noticed that I had the connection to the MQTT broker initiated as val mqttActions only in the very beginning of my rules file with several rules in there.
Once I changed that to var and reassigned the broker within the “rule when then” block, it worked just fine again.

mqttActions = getActions("mqtt", "mqtt:broker:MQTTBroker")