Store Key/Value pairs

Platform information:

  • Hardware: RasPi 3
  • OS: Raspbian
  • openHAB version:2.5.0 M6

Hi all,
i want to store some Key/Value pairs out from my rules. Actually i do this in HashMaps in variables, but this isn’t persistent.

In some cases (e.g. on starting having a bath) i want to store the position of the Rollershutter, and when i’m finishing in bath, the Rollershutter should return to the old position before bathing.
I use MapDB-Persistence, and can persist the state on starting, but on finishing i don’t know the timestamp, when it was persisted.
And in other cases the Key is variable and not Item-Based…

So, is there a (simple) chance to store flexible Key/Values in a Redis-Cache or other DB?
Or is my actual solution to use HashMaps the best one?

Thanks!

Store the position in a dummy item and restore when done

Such an Item could be a String type, which can be populated with CSV, or key-value, or JSON , or …

When the “undo” event fires, get the item-in-question.previousState

That’ll tell you what it was before this change.

Another option is to use scripted automation and store to Item metadata. There are helper libraries for Python and JS.

I wrote a DP for this option. Design Pattern: Using Item Metadata as an Alternative to Several DPs

If you are using rule variables, you should be using the storeStates and restoreStates Actions instead of writing the code to pull and restore the Item’s states yourself.

Is the item metadata persistent (when you haven’t specifically set up persistence)?

Yes. It is in $OPENHAB_USERDATA/jsondb/org.eclipse.smarthome.core.items.Metadata.json (that name will be different in OH3).

1 Like

Awesome! Learnt something new (as always!). I have recently started using item metadata to store some states rather than using a global variable. One thing I stored was the “direction” of light dimmer - whether it is curently getting brighter, or dimmer, when a button “hold” is received.

1 Like

This sounds like the perfect use for storeStates and restoreStates https://www.openhab.org/v2.4/docs/configuration/actions.html#event-bus-actions

Hi and Thanks for the many ideas!
So…i use actually only DSL, i will have look to Jython to use MetaData, the first look seems to be really big!

The storeStates and restoreStates will solve some of my problems :wink:

For every Key-Value-Pair an extra item is oversized in my opinion…