Store/Recall Vars

Hi there,

I use openhab with OLA to have a simple setup for a few lights. Everything is good so far.
But now I try to make some simple Store/Recall Buttons to temporarely store some states.

But there is no success. I guess that the vars are only recocnized for the runtime of the rule. Ist that correct? If so: How can I create vars who stay for the uptime of openhab?

Cheers for your help, Michael

Here is my code:

rule "save1"
Item Store_1 received update
var colorValue_1_1 = Spot_1_RGB_Color.state as HSBType
var colorValue_1_2 = Spot_2_RGB_Color.state as HSBType
var colorValue_1_3 = Spot_3_RGB_Color.state as HSBType
var colorValue_1_4 = Spot_4_RGB_Color.state as HSBType
var colorValue_1_5 = Spot_5_RGB_Color.state as HSBType
var colorValue_1_6 = Spot_6_RGB_Color.state as HSBType 
rule "recall1"
Item Recall_1 received update
sendCommand (Spot_1_RGB_Color,colorValue_1_1)
sendCommand (Spot_2_RGB_Color,colorValue_1_2)
sendCommand (Spot_3_RGB_Color,colorValue_1_3)
sendCommand (Spot_4_RGB_Color,colorValue_1_4)
sendCommand (Spot_5_RGB_Color,colorValue_1_5)
sendCommand (Spot_6_RGB_Color,colorValue_1_6)

Yes, that’s right.

To create a global variable, declare it with var (or val) outside of any rule at the top of your rules file.

Note, that it is only global to that rules file. Rules in other files cannot see it. If you want system-wide variable, use an Item.

1 Like

I understand, thanks!

So I created some ColorItems for that.

If I want to update them which syntax should I use:

set ‘myVar’…

or with


If they are Items, you would probably use postUpdate

1 Like

You can then “store” the virtual items with persistence, for example rrd4j, and they will be restored after an openHAB restart.

Strategies {
	everyMinute : "0 * * * * ?"
	default = everyChange

Items {
	MyItem : strategy = everyChange, restoreOnStartup
1 Like

Thanks for the hint. I will get on it later, because I don’t need persistence in my case.
Cheers - Michael

OK Markus,

I’ve still implemented it, but the value I’ve stored is different from the value I get after a restart.


I store the HSB Value 0,100,100 for one spot. So I get 255/0/0 as DMX that means the Spot is red.
If I recall it everything is fine. After a restart I do a recall and the DMX goes 255/255/255.

So the stored value in persistence seems to be different. Ist there a way to read out the database to see whats stored, or do I need to store it in another way, because the Item as a HSBType has three values ?

Cheers, Michael

Edit: I’ve read out one value in habPanel. If I store I get 0,100,100 after a restart it changes to 0,0,100???

Edit2: I found the rdd files in /var/lib/openhab2/persistence/rrd4j. One file for every stored Item. But there are all empty.

rrd4j can store numbers only. You can try store each segment of the HSB in its own item, without the commas. Or use another persistence provider and store the whole HSB value as a string.

I dont know how to read a rrd4j db. Mysql should be the better choice for your needs.

EDIT: openHAB2 & MySQL persistence setup

1 Like

Changed to MySQL and it works well! Thanks!

Nice to hear that. One more notice. The rrd4j is fixed in file size, because it “forgets” old data automatically. The MySQL db is growing over time and you have to do some housekeeping then and now. But if the values don’t change that often it shouldn’t be an issue.

Yeah, i realized that, when I watched the Database, but think the same: If the values do not change that often it shouldn’t be an issue.