OH3, retore previous state and rules graphical editor

I’m trying to use OH3 GUI as much as posible to define rules but I have one question regarding the way to manage rules where you want to save current status of a light (for instance) to go back to it in the future.

Let’s me try with an example. I have this defined into a rules file

var vDS_LightCeilingHall_Brightness_PreviousState = null

rule “Turn on light when open closet”
when
Item DS_Closet_Magnet changed from CLOSED to OPEN
then
if((DS_MotionSensor_Illuminance.state as Number).floatValue < 100) {
vDS_LightCeilingHall_Brightness_PreviousState = storeStates(DS_LightCeilingHall_Brightness)
DS_LightCeilingHall_Brightness.sendCommand(50)
}
end

rule “Restore light when close closet”
when
Item DS_Closet_Magnet changed from OPEN to CLOSED
then
if(vDS_LightCeilingHall_Brightness_PreviousState !== null) {
restoreStates(vDS_LightCeilingHall_Brightness_PreviousState)
vDS_LightCeilingHall_Brightness_PreviousState = null
}
else {
DS_LightCeilingHall_Brightness.sendCommand(0)
}
end

I think it is pretty straightforward but the idea is just turn on a light when I open a closet door and go back to its previous status (off or on with another brightness) when I close the same door.

This code is working at OH3 RC2 with no issue but, as I said, if I set it into a rules file the graphical rules editor says it is not editable and I would like to have as much as posible rules editable into the GUI.

Is it posible to have this rule/behavior not using .rules files in OH3?

At my understanding, putting the variables out of the rules themselves make them available for other rules hence the “vDS_LightCeilingHall_Brightness_PreviousState” is defined out of the “rules”.

At my understanding too, in OH3 rules editor, you can’t define those kind of global variables.

Are my understandings right? I’m new at OH but I’m trying to read here and there and I can be misunderstanding something.

Thank you in advance

I don’t think it’s possible with Rules DSL. You can save a variable from one run of a single rule to the next in JavaScript by saving the value to this. For example this.previousStates = events.storeStates(DS_LightCeiling_Hall_Brightness).

Of course this will require that you combine the two rules.

Thank you @rlkoshak!
I’ll give it a try but definitely it sounds what I was looking for. I missed your OH 3 Examples: Writing and using JavaScript Libraries in MainUI created Rules post talking about it but I’ve found it now :slight_smile:
It’s a “pity” I started with DSL rules (I’m new with OH and I pick that choice because it seems to be more examples with them) but definitely seems learning JS will be very helpful.