[SOLVED] Initializing item value on system startup

Platform information:

  • Hardware: Raspberry Pi 3 Model B Rev 1.2 // 1GB RAM // 32GB SD
  • OS: Raspbian GNU/Linux 10 (buster) // Linux 4.19.75-v7+
  • Java Runtime Environment: openjdk version (build: 1.8.0_222)
  • openHAB version: openHAB 2.5.0-1 (openHABian)

Dear openHAB-community,

I would like to initialize value of an item to 0 when de system restarts but it does not work. The item I want to initialize is used as a setpoint item in my sitemap. Its purpose is to adjust the temperature reading of a Modbus RTU sensor later on. When the system restarts i would like it to be set to an offset of 0°C or even better to remember the value from before the restart.

This is what i did:

  1. Declared it as an item(Modbus_Sensor.items):
Number Humidity_read    "Relative humidity: [%.1f/100 %%]"          {channel="modbus:data:slave01:SensorRead1:humidity1:number"}
Number Temperature_read "Temperature: [%.1f °C]"                    {channel="modbus:data:slave01:SensorRead1:temperature1:number"}
Number Temp_correct     "Temperature correction: [%.1f °C]"       //  {channel="modbus:data:slave01:SensorWrite1:Temp_correct2:number"}

I want to send this temperature correction value to the sensor later on, therefore the commented part is there.

  1. Added it to the sitemap(HVAC.sitemap):
sitemap HVAC label="HVAC Monitoring"
 {
           Frame label="Modbus Sensor 1" {    
        Default     item= Temperature_read  icon="temperature"      label="Temperature: [%.1f °C]"
        Setpoint    item= Temp_correct      icon="movecontrol"      label="Temperature correction: [%.1f °C]"    minValue=-10 maxValue=10 step=0.5 
        Default     item= Humidity_read     icon="humidity" 
    }
}


3) Tried to initialize it with this rule(HVAC.rules):

rule "Startup"
when
    System started
then
    Temp_correct = 0
end
  1. I get this error:
==> /var/log/openhab2/openhab.log <==

2020-01-25 22:42:32.684 [INFO ] [panel.internal.HABPanelDashboardTile] - Started HABPanel at /habpanel

2020-01-25 22:42:32.997 [INFO ] [ebuilder.internal.HomeBuilderServlet] - Started Home Builder at /homebuilder

2020-01-25 22:42:33.254 [INFO ] [openhab.ui.paper.internal.PaperUIApp] - Started Paper UI at /paperui

==> /var/log/openhab2/events.log <==

2020-01-25 22:42:34.628 [vent.ItemStateChangedEvent] - Temperature_read changed from NULL to 25.6

2020-01-25 22:42:34.655 [vent.ItemStateChangedEvent] - Humidity_read changed from NULL to 42.8

2020-01-25 22:42:34.678 [vent.ItemStateChangedEvent] - Temperature_read changed from 25.6 to 25.5

2020-01-25 22:42:34.710 [vent.ItemStateChangedEvent] - Humidity_read changed from 42.8 to 42.2

2020-01-25 22:42:35.881 [vent.ItemStateChangedEvent] - Local_date_time changed from 2020-01-25T22:42:30.853+0100 to 2020-01-25T22:42:35.853+0100

==> /var/log/openhab2/openhab.log <==

2020-01-25 22:42:38.446 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Error during the execution of startup rule 'Startup': Cannot assign a value in null context.

Any hints on what i am doing wrong? Oh and yes, I am a newbie and this my first project.

Many thanks in advance!
Sancho

rule "Startup"
when
    System started
then
    postUpdate(Temp_correct,0)
end
1 Like

Have a look here

1 Like

Thank you, both of you!!! That solved my problem and pointed to me to a part that i should read once more. I have read so much of the documentation the last days that it is getting a bit blurry in my head.

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?