Restore a HashMap on startup of rule

I’ve different light scenes stored in a Map. Is there a way to restore this Map when a rule starts or when the system starts?
The var that I want to store and restore looks as follows:

var HashMap <String, Map> SceneMaps = new HashMap()

Create a system started rule and populate it there. Even better, consider moving the values in the map to Items, name those items so you can find them by name, put them in a group and use gMyGroup.members.filter(i| == name). Then persistence will handle the restore, your rules will become simpler, and you have the option to update the values on the sitemap. I do this and it madness my does far simpler abd shorter than when i was using hashMaps.

This method seems not to work anymore with openHAB 3. Are there recommended patterns for such a kind of population?

Best regards,

Things have changed a little after six years.

You can still do exactly as described in this thread, by doing it the way it would have been done at the time - using rules in xxx.rules files. Not in GUI rule editor.

Well, using the .rules-files does not work anymore. I replaced the “System started” trigger with a cron trigger and things started working after the cron condition was reached. So the System Started trigger seems to have changed its functionality.

Yes, it no longer fires on editing a xxx.rules file.

In consequence, editing this .rules-file will require either working with the cron workaround for testing or some other way. What I’m asking for is: what is that other, non-workaround way, to initialize member variables in a rule file properly (so that they are initialized in both cases)?

  • When you change the .rules file, you can manually run the rule.
    • There is a play button for that rule in MainUI that will run it.
    • You can run it from the karaf console.
    • Or you can add a Switch trigger to the rule and send a command to the Item.
  • You can put the data into Items like I suggested and let persistence and restoreOnStartup handle that and throw away the rule entirely.
  • You can put the key/values into a .map file and use the transform("MAP", "", "Value") to access the value assuming that it’s unchanging data.
  • You can encode the data in Item tags.
  • If using a non-Rules DSL language you can encode the data in Item Metadata.

Thank you! I will explore the options. Currently, since it’s static data, I find the map/transform approach very appealing!