[SOLVED] Initializing item value on system startup

Even better:

This is the better syntax for postUpdate
Explanation there: https://www.openhab.org/docs/configuration/rules-dsl.html#myitem-sendcommand-new-state-versus-sendcommand-myitem-new-state

rule "Startup"
when
    System started
then
    Temp_correct.postUpdate(0)
end
2 Likes

It’s a lot at first but soon the concepts will seem second nature. Just ask question when you get stuck

I’ve read and understood it. Will change that first thing after i got some sleep. Thank you!

Any hints on making it remember the last value?

Only having a little background in microcontrollers and C, I am thinking of EEPROM to recover last values.

For just the last value and restoring the value when openHAB starts it is recommended to use the MAPDB persistence service: https://www.openhab.org/addons/persistence/mapdb/

1 Like

I will look into that, thanks! At the moment i am only using inlfuxDB to record the measurement data and visualize it with grafana.

Info: if you link that Item to a modbus data Thing with read capability … the next read poll will overwrite your “initialized” Item state anyway.
A first poll will most likely have already run before your rule, and your rule will overwrite the polled data.
At the next poll after your rule has run, the polled data will overwrite your rule data again.

It is possible to make write-only modbus data Things, if that is what you have in mind.

A tweak to your rule that only “initializes” if no polled data is available yet

rule "Startup"
when
    System started
then
    if ( Temp_correct.state == NULL) {
      Temp_correct.postUpdate(0)
    }
end
1 Like

Thank you very much @rossko57 !

I did not want to maintain some rule files when or if i change/add items so i created this script:
https://github.com/SySfRaMe/gen_init_file.sh
I hope it will help others.

I want to initialize a String item with the following rule:

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

but i get the following error message

Error during the execution of startup rule ‘Startup’: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.postUpdate(java.lang.String,java.lang.String) on instance: null

Any clue on how to fix that?

Does this item exists ?

It is a virtual item defined in my .items file as “String remoteCommand”

check via karaf console.
OH3:

openhab:update remoteCommand init

OH2:

smarthome:update remoteCommand init

If this works, you probaly need a sleep in the rule until all items are initialized/loaded during startup.

That error does not look like it comes from that Item method, but from something more like the action
postUpdate(X,Y)

Sure you only have one rule named “startup”?

@HaKuNa

smarthome:update remoteCommand init

indicates

Update has been sent successfully

via the karaf consol but the rule keep having the same error even with a 20 sec delay

@rossko57 here is my rule

And I only have one “Startup” rule

Try temporarily

rule "Startup"
when
    System started
then
    xxnonsensexx.postUpdate("init")
    Thread::sleep(20000) 
    remoteCommand.postUpdate("init")
end

and expect to see a different error message about unknown Item.

That would leave the value. I doubt there is anything special about the string “init”, but try another. Is remoteCommand really a String type?

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.