Error on a rule only after I clear the cache of openhab

Hi,
Strange problem. If I clear the cache of openHab and start openHab I get the error below. The only way I have fond to fix it is to just save the rule and then it works again.
I have no idea why.
If I just restart openHab it works fine.
I am running openHab version 5.0 (just upgraded today to 5.0.1) the issue is still there.

2025-08-20 09:00:00.256 [ERROR] [omation.script.javascript.4c2d784197] - Failed to execute script: TypeError: Cannot read property "publishMQTT" from null
        at <js>.:program(<eval>:32)
        at org.graalvm.polyglot.Context.eval(Context.java:416)
        at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:490)
        at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:458)
        at java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262)
        ... 22 more
2025-08-20 09:00:00.258 [ERROR] [.handler.AbstractScriptModuleHandler] - Script execution of rule with UID '4c2d784197' failed: TypeError: Cannot read property "publishMQTT" from null in <eval> at line number 32 at column number 3

Here is the rule etc:

configuration: {}
triggers:
  - id: "1"
    configuration:
      cronExpression: 0 0/2 * * * ? *
    type: timer.GenericCronTrigger
  - id: "3"
    configuration:
      itemName: Zigbee_Temperature_xxxx
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript
      script: >-
        //Because the zigbee only sends the temperature and humidity at random
        times this

        //makes the graphs and data on the weather web page doesn't work
        properly

        //so I will send the latest data every 2 minutes so hopefully that will
        fix that issue.

        //I had to change the USB console for the weather station and use SDR to
        get the data but it didn't send

        // the inside temperature, humidity and pressure. I am getting the
        pressure from a BME280 device in the shed

        var TOPIC="weewx"

        var PAYLOAD = ""


        //Below is using the xioami sensor in xxxx bedroom

        //Have to change the weewx.conf file as well

        //temperature=(items.getItem("Zigbee_temperature_bedroom_xiaomi_xxxx").state)

        //humidity=(items.getItem("Zigbee_humidity_bedroom_xiaomi_xxxx").state)

        //var PAYLOAD=(JSON.stringify({"humidity": humidity, "temperature":
        temperature}))


        //Below is using the aquara sensor in xxxx room

        var temperature=(items.getItem("Zigbee_Temperature_xxxx").state)

        var humidity=(items.getItem("Zigbee_humidity_xxxx").state)

        var pressure=(items.getItem("Zigbee_xxxx_pressure_sensor").state)

        var PAYLOAD=(JSON.stringify({"humidity": humidity, "temperature":
        temperature, "pressure": pressure}))



        //Below is using the aquara sensor in xxxxs room

        //temperature=(items.getItem("Zigbee_bedroom_xxxx_temperature").state)

        //humidity=(items.getItem("Zigbee_bedroom_xxxx_humidity").state)

        //pressure=(items.getItem("Zigbee_xxxx_pressure_sensor").state)

        //var PAYLOAD=(JSON.stringify({"humidity": humidity, "temperature":
        temperature, "pressure": pressure}))

        //actions.get("mqtt","mqtt:broker:mqttbroker").publishMQTT(TOPIC,
        PAYLOAD, false);


        var mqttAction = actions.get("mqtt", "mqtt:broker:mqttbroker");

        //For some unknow reason this doesn't work if I clean the cache and I
        have to save the rule then it works

        //if (mqttAction !== null) {
          mqttAction.publishMQTT(TOPIC, PAYLOAD, false);
        //} else {

        //   message = "<H2>Rule 4c2d784197 not working <br>" + "</H2>" + "Just
        save the rule and then it works. Only does it after cache clear " ;

        //  actions.get("mail",
        "mail:smtp:a514b96249").sendHtmlMail("user@gmail.com", "Rule
        4c2d784197 not working", message );

        //  console.error("MQTT action could not be retrieved. Check MQTT broker
        configuration.");

        //}


        //actions.Exec.executeCommandLine(time.Duration.ofSeconds(5),
        "/usr/bin/mosquitto_pub","\-h","192.168.1.164","\-t","" + TOPIC,"\-m",""
        + PAYLOAD);


        //console.log(JSON.stringify({"humidity": humidity, "temperature":
        temperature}))
    type: script.ScriptAction

This warning comes up as well but I don’t think it is associated with this script:

[WARN ] [e.script.ScriptTransformationService] - Script engine context config:js:6912317660 is already closed, this should not happen. Recreating 
script engine.

I have commented out a lot of the script to try lots of things but nothing worked. The only way to get it to work is re-save the rule.
I am not 100% sure but I think this worked fine in openHab 4.0 (I could be wrong though)

Anyone else seeing this?

Pay attention to what the MQTT broker thing’s status is when this rule tries to run and fails. If it’s UNINITIALIZED or in some other state than ONLINE, the call to retrieve the MQTT action on that Thing will fail.

After clearing the cache, all the bundles need to be reinstalled and that can often take longer to complete than OH takes to reach start level 40 where rules start running.

To make your rule resistant to this error, you can add a condition to check the status of the MQTT Broker Thing and only run it if it’s ONLINE.

The odd thing is that it is just this one rule and everything else works. I run this rule every 2 minutes and it will not work until I save it.

Is there other rules that use the MQTT action that work when this one fails? That’s the key part. It’s failing to pull the MQTT actions so when you call publishMQTT it can’t because you pulled null instead of the actual actions.

There is nothing about how this rule is written that would require it to be saved to make it work again so I have no answer for that. You pull the actions every time the rule runs so if the Broker is offline when the rule runs it will generate an error but if it’s online later it should succeed and the rule run as expected.

You can verify if the Thing is ONLINE through a log statement just immediately before your try to pull the actions.

console.info(things.getThing('mqtt:broker:mqttbroker').status);

If it’s logging ONLINE but actions.get() is still returning null something odd is going on. But if it’s anything other than ONLINE than it’s working as it should.

Depending on how long it takes your system to come online, it is very likely that the first couple times this rule runs after an OH start after having cleared the cache it will fail. It can take some minutes for all the add-ons to be installed and Things ONLINE even on a fast machine.

The rule gets my barometer for my weather station and I noticed that I was missing half the day of records.
The rule doesn’t work at all until I save it.

I tried to trouble shoot it and I gave up so I am using exec command to run mosquito and that works.
At least I don’t have to remember to save it if I clear the cache. I don’t clear it often, usually at upgrades.

A normal upgrade will clear the cache for you. You only need to do it manually if something is going wrong with installation of the bindings or it seems a binding is corrupted or something like that do you need to clear the cache manually.

The weird part is I use the

actions.get("mqtt","mqtt:broker:mqttbroker").publishMQTT(TOPIC,
        PAYLOAD, false);

in lots of other rules and they work.
I copied the rule and it still did the same thing.
Very strange.
At least I have a work around.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.