[SOLVED] Initializing item value on system startup

That is weird. If I add

xxnonsensexx.postUpdate("init")

to the rule I have no error

I was expecting “weird” from somewhere, but not that :crazy_face:

Okay, the problem is about the rule not that Item.
Really sure there is only one rule “startup”?
Confident your editor is saving the file to the folder?
You should see “xxx.rules loaded” message in your openhab.log at each save. No parser complaints following that?
I’d look at the preceding or following rules in the file, make sure they are structured properly and all brackets are paired.

Nothing found, try your test rule in a new file of its own. Type it in, don’t copy-paste to avoid copying invisible control characters. Remember, only one rule of name “startup” at a time,give it a new name.

If I create a separate .rules file with the rule

rule "Startup"
when
    System started
then
    remoteCommand.postUpdate("init")
end

It works but if I copy (or write) the exact same rule in my main .rules file I keep having the “Could not invoke method on instance null”.

I have checked my main .rules file and I could not see any syntax error and all its other rules are fine.

I guess I will live with a separate startup.rules file

Well, it’s not pixies, there is a syntax fault in your big file somewhere. Non-obvious faults can be down to invisible characters outside of regular rules, maybe your editor has an option to show them.

I was happy to quickly.

I am using remoteCommand as a virtual item to keep track of remote commands received through mqtt

I have a rule that starts when I receive a new remote command through mqtt and the first action is to check if it is a new command or a repeat of the previous one as you could see below (simplified version):

rule "Home Remote"
when
    Item mqtt_topic_d2cfb0fd_remote changed
then
    if(mqtt_topic_d2cfb0fd_remote.state.toString == remoteCommand) {
        return
    }
    remoteCommand.postUpdate(mqtt_topic_d2cfb0fd_remote.state.toString)
    logInfo("rule remote", remoteCommand)
end

I had with that rule the “Could not invoke method 
 on instance null” error and I thought that was because, at first, the remoteCommand item was not initialized, hence my startup rule.

Now I coud clearly see that remoteCommand is initialized but I still have the “Could not invoke method 
 on instance null” error on my “Home Remote” rule.

To make sure that there was no typo I rewrote the rule alone on a new remote.rules file and I still have the same error:

> 14:10:31.165 [INFO ] [smarthome.event.ItemStateChangedEvent] - mqtt_topic_d2cfb0fd_remote changed from Film to Netflix
> 14:10:31.165 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule 'Home Remote': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.LogAction.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null
> 14:10:31.165 [INFO ] [smarthome.event.ItemStateChangedEvent] - remoteCommand changed from Film to Netflix

This will never be true; a string cannot equal an Item object. remoteCommand.state.toString perhaps?
(You don’t have to initialize that Item, if it is uninitialized this will return the string “NULL”)

Slow down and read the error.
The logInfo() is complaining, the postUpdate is already done.

remoteCommand is an Item object, not the string that logInfo() expects.

logInfo("rule remote", "Item is " + remoteCommand.name)
logInfo("rule remote", "state is " + remoteCommand.state.toString)

But I will also point out the next surprise before you get it.
postUpdate() is asynchronous; it sends a request to update off to the openHAB event bus, so that rules, bindings, UIs, etc. that are interested can see the event. Eventually it updates the Item state, takes a couple of milliseconds.
Your rule does not stop and wait for that to happen.
If you examine the Item state in the next rule line, it will probably be the “old” state.
But you don’t need to do that at all, because you already know what you just sent in the postUpdate().

Items are not simple variables.

Many thanks Rossko for your help and sorry for so many basic mistakes.

I agree that items are not as simple as variables. In fact remoteCommand was initially a rule variable but when I decided to test against its last state I could not find a way to use variables across rules and I had to move it to an item hence all the .state.toString mistakes


Thanks again for your help and time

There are ways to declare “global variables” at the top of a rules file, simply
var xx
and all rules in that one file may refer to and change the shared xx

To share across rules from different files, using an Item is indeed the best way.

umm, your rule is triggered from change.