Persistance of variable in rule

I am looking for a solution to restore a list of hue values for a pool RGB light after openhab restart.
Is there a way to use persistence to save and restore a List variable in a rule via openhab persistance?

Simple, use an item and use mapdb persistence for this item (saving everychange).

I would need to persist a List<Number> or List<Integer>. No idea how do create an item for this to use persistance.

Persistence only deals with items, so if you want to persist a list then you would have to break this down into single items (and rebuild the list when system started).

I was afraid that it is like this. The list size is dynamic and can be quit long -> using seperate items would be a big mess. Is normal Java serialization or persistence possible with Xtend ?

You cannot use the persistence engine to save and restore vals and vars.

About the best you can code up some file I/O Java code yourself in rules. I’m not certain Bean serialization works or not in Xtend but see no reason why it wouldn’t.

Not knowing what your code does nor how it works I can’t say for sure, but I do have to say that it sounds like there is probably a better way to implement what you are doing using Items, Groups, and other persistence friendly approaches. At least in my experience there always has been an alternative way to represent these List and HashMap based “data structures” which both works better with persistence and simplifies the code.

And if there isn’t a way to do it using these approaches, it might be a good candidate for recoding using the JSR233 binding and Jython or JavaScript.

Thanks for the explanation. I’m just trying to get a bit more into Openhab and am still fighting with the details.
I’m trying to store a color list for an animation of RGB pool lights with colors that can be defined by the user. I actually just add the current color to a list in a rule when a push button is pressed. Obviously this color list is lost when openhab restarts.
I checked the JavaDoc. GenericItem, the parent class of SwitchItem allows to add Strings “addTag(String tag)” which end up in a Set of Strings. I tried to add RGB color strings as item tags and recover them after openhab restart via mapdb persistance. It did not work. Before I try to figure out why, I have one question:
Is the entire class or just some fields of an item class restored via openhab persistance ?

I think openhab is a great project. It’s just a bit difficult to figure out some details.

Guess only the item state is restored.
-> I’ll use a String item which keeps the color RGB strings as “;” (or other separator) separated String. At openhab start I’ll split this string and regenerate the Color list.

Any reason why you wouldn’t just use a Color Item and ColorPicker on your sitemap for these user configurable values? No need for String parsing and no need for String parsing.

I need to store several colors. The number of different colors is not fixed but variable. Would need lots of Color items and this is kind of a mess in my opinion. I don’t see a way to store several colors in one color item. Do I miss something?

From your description it sounded like you were creating a String Item for each anyway.

If you truly have so many that keeping up the number of Items is a problem it probably would be worth while moving this information out of Items and into a configuration file or something like that. I don’t see how you can put this String on your sitemap and have it be remotely usable anyway so why not drop that pretense entirely.

One thing to take note of is the maximum String length in the DB. I don’t know if there is one for DBs like MapDB but I suspect that the columns that OH defines for String Items in SQL type DBs like MySQL is probably limited to a certain size (e.g. 256 characters). You might run up against that limitation.

Most implementations of SQL the string length is limited to approximately 65520 (I forget the exact number, but it’s a bit less than 65535!).

MapDB is storing java objects, so should (I assume) be able to store “anything” you can put in an object.

Thanks! About 65k ought to be enough for anyone. :slight_smile:

The maximum size of a String is 2,147,483,647 characters so MapDB should be fine for anything shorter than the length of Moby Dick.

Thanks for the info. I guess 2 billion characters should do it.