Rule - Variable or Dictionary - Save Previous Sate of Multiple Items?


Just a quick question… I’m wanting to save the current state of items into a dictionary, change that state, and then when done, change them back to what they were originally. This will be in a rule that will execute twice, once when it starts, and then again when it stops.

Basically, I have a group of lights. I already know how to cycle through the group, and send a single command to all of them (ON or OFF). But what I want is to save their current state into like a global dictionary variable so that when the rule is ran for the second time (job finishes) it sets the lights back to what they were before it started.

Thoughts? Examples? I know it’s possible… Just been a long day… lol…

Thanks in advance,


There will be a sunrise in the morning, so things are looking up!

What you are describing is one of the things that you can implement through a persistence service. You can configure it to backup the state of specific Items and then restore those states when OH starts back up. Is there are reason why you are not using persistence for this, or is this the first you are aware of it?

It is also possible to read and write data to the file system from a rule. There are some posts in the forum, like this…

If you’re using Jython, it is real easy to use the native stuff…, or you can use Java.

Look in the in the Actions docs for storeStates and restoreStates. the former takes a list of Items and returns a Map with their current by states. The later takes that Map and updates the Item back to that state. But if any global variable will go away when the rules file is reloaded or restarts, you’ll need persistence or to write to a file or something like that.


That’s exactly what I was looking for…

Here’s my final result! Now my Roomba can vaccuum any time of day, and it’s path be well lit!, lol.

var Map 	LightsState		= null
var Boolean boolCleaning	= false

rule "Roomba Mission"
		Item RoombaMission changed
		val currentMission = triggeringItem.state.toString.toUpperCase
		if (currentMission == "NONE" && boolCleaning == true) {	
			sendBroadcastNotification("Roomba has finished cleaning.")
			restoreStates (LightsState)
		else if (boolCleaning == false) {			
			sendBroadcastNotification("Roomba has started cleaning.")
			LightsState = storeStates(RoombaLights)
			RoombaLights.allMembers.forEach[ n | n.sendCommand(ON) ]

Thanks again everyone for the assist!