Lifecycle of an item

Hi There,

currently i am struggeling about the lifcycle of an item. Unfortunatly i have not found an overview about thins like NULL, Uninitializes, Undefined, … .

Could someone please give an overview about the diffent states of the lifecycle and how to check them. That would be really great.

Oh i am using OH2

Thanks
Thomas

I’ve not checked the code on this and could be wrong.

When an Item first gets loaded into memory from the Items file, I believe it gets set to Uninitialized.

If you have restoreOnStartup for the Item it now gets set to the most recent value saved in the persistence.

If you do not have restoreOnStartup for the Item it now gets set to NULL. NULL is the same as Undefined in openHAB 1.x.

I don’t know how or if it ever makes sense to test for Uninitialized as I don’t think the rules load soon enough for an Item to ever have that state. I’ve never seen it.

To test for NULL simply do MyItem.state == NULL.

Once it gets set to NULL or some other state via restoreOnStartup or from a binding or the sitemap or what have you, it keeps that state until it is updated or OH restarts or a .items file changes. I don’t think you are allowed to set an Item to NULL within Rules.

Any change to the .items file causes all .items files to be reloaded and all Items starting off again as Uninitialized and the whole process starting over again. The same occurs when OH is restarted.

There is a new “expire” binding which is used to set items to a default (even NULL) value if there is no update for a certain time (just to be complete) :slight_smile: I think it’s not released yet, though.

1 Like

hi @rlkoshak, how are you?
i understand partially what you say here; when the OH is restarted i beleive that Persistence applies (because i already have restoreOnStartup for some Items), so i think restarting OH would be returning to a “normal” situation but …

what happens if i am editing my items file? will they stay Uninitalized until they receive an update?
so it’s kind of different from the restart, isn’t it?

The problem is i’m migrating from OH 1.x to OH2 and now i’m seeing these messages in logs:

Could not transform 'NULL' with the file 'motion.map' : Target value not found in map for 'NULL'

any help with this would be kindly appreciated

…so, i’m trying to understand my item states :wink:

thank you very much!
maxplay

The same thing happens when you restart openHAB and when you change a .items file.

First OH loads/reloads the Items into memory with an initial value of Undefined/NULL

Then it loads the previous value of the restoreOnStartup configured Items.

They behave exactly the same as if you restarted OH. They get reloaded with an initial value of NULL and then they get set to their last saved value by persistence with restoreOnStartup. There might be a slight difference in that when you reload the .items file other parts of OH are up and running (e.g. Transformation, sitemaps, etc.) which would not be up and running during a restart so as your Items get reloaded, initialized to NULL, and then restored by persistence they generate errors that do not exist when OH starts.

Item definitions?

One thing that may trip some users up when migrating to OH 2 is the fact that they are starting over with a clean slate. What often happens as we grow our systems is that the Items we are dealing with will have always had a value so we forget to check for and handle the NULL cases.

Ideally, you should have the following in your .map files:

-=Unknown
NULL=Unknown

(replace Unknown with your desired text)

It is always a good idea to think about and write your config to deal with the NULL cases, especially with label and state Transforms.

Thanks @rlkoshak
:wink:

(yes, 2 years later in violation of #3 :money_mouth_face:)

@rlkoshak @vzorglub

Is “-” acting as a catch-all? Do we really need it in a map xformation file?

I will try to dig into the code to see if there is a reference to this.
For sure we need NULL (not null) for a possible state value but I am not sure about -

I usually use at the end of all of my *.map files the following:

-=OK
.=Uknown
NULL=Welcome to the Void!

but this is due to my ignorance :stuck_out_tongue:

We used to in OH1. Not sure it’s needed for OH2 anymore.
You might want to ad UNDEF to your backstop list, for bound Items the binding can set this state to flag problems.

1 Like

Looks like not.
I found this: https://github.com/mgbowman/openhab2-addons/pull/1#issuecomment-444096608