Persist a few values?

Just making sure I understand.

In order to persist one or two values between reboot I need to set up one of the db addons?

Update: After posting, I continued to research.

I see that mapdb allows the persistence of item states. I have not found anything about saving the state of values defined with a “val” statement in a rule. For example, a simple counter variable that one might want to restore after a power outage, but never is represented as an Item.

Persistence is only for items. If you want to persist a rule-variable, you have to store into and read from a item.

Harry is correct, but you could also save it to a file on shutdown, and then read the value from the file on startup.

I did search fo a way to do that. But, I didn’t find anything with the terms I used.

Is is there an actual file open, write, close?

They’re out there… somewhere. I summarized a few options in this post. Also read Rich’s followup. To read/write to a file, you could use executeCommandLine to do something from a shell, or use something from

Here is another example using JSR223. With your background (I noticed in another thread), you may be much happier using JSR223 with Jython or Javascript rather than Xtend and the Rules DSL. I’m getting pretty close to completing a migration from the Rules DSL to JSR223-Jython, and I’ve been very happy with it!

There is a that is available as OSGi service. You can store your complex objects in jsondb, using that service.

In Java its like:

Storage<Integer> storage = storageService.getStorage("MyStorage");
// save in jsondb file
storage.put("MyKey", 10);
// some time later or after reboot
Integer val = storage.get("MyKey");

storageService is an OSGi @Reference to

Your data goes in USERDATA/jsondb/MyStorage.json.
There will be backups of this file. Flush cycle is by default 500ms to 30 seconds.

Not sure if Xtend environment exposes StorageService to users.

Unfortunately, the Rules DSL does not have access to this, but JSR223 does. I haven’t had time to play with it yet, but this may be very helpful. Thank you!

Do you know were I can find JSR223 and OH integration discussions / docs / github links? I am working on Kotin integration, so it might help.

Check the documentation here. There is also a tag in the forum you can read through, but most of it is older. Ping me if you have any questions. You may find it if you drill down enough, but look at the openhab-Jython repo. I have a few changes to add, including some more documentation. In the OH-Jython-Scripters lucid repo, you will find a link to a Slack channel where there is some more discussion.

I saw your post about Kotlin and it sounds interesting, but Jython is working well for me. I haven’t found any info yet on developing a binding with it, but will look into it more once I’ve migrated all of my rules, but I will probably look at adding LSP first.

1 Like