Setting item to a previous state based on different item

This doesn’t directly address your specific problem, but if you separated your rules by function rather than room you will have a lot more opportunities for creating generic rules and reuse of variables in code. It also lets you apply DRY (Don’t Repeat Yourself).

But regardless, you can do this with two events (one event that starts the heating mode) and two very simple rules.

var previousStates = null

rule "Start BR Nursery Heating"
when
    // whatever triggers this mode
then
    // store the current states
    previousStates = storeStates(gAllVents)

    // close all vents but the ones that should remain open
end

rule "End BR Nursery Heating"
when
    // whatever triggers this mode
then
    // restore the states
    if(previousStates != null) restoreStates(previousStates)
end

It gets a whole lot more complicated if you need your previous states that you restore to survive a restart of OH or a reload of the .rule files. In that case, we need to set up persistence and then I’d do something like the following:

rule "Start BR Nursery Heating"
when
    // whatever triggers this mode
then
    // save a timestamp when the special mode starts in a persistence backed up Item with restoreOnStartup
    BR_Nursery_Heating_Started.postUpdate(new DateTime)

    // close all vents but the ones that should remain open
end

rule "End BR Nursery Heating"
when
    // whatever triggers this mode
then
new DateTime((MyDateTimeItem.state as DateTimeType).calendar.timeInMillis)
    
    val startTime = new DateTime((BR_Nursery_Heating_Started.state as DateTimeType).calendar.timeInMillis)
    gAllVents.members.forEach[ vent | 
        vent.sendCommand(vent.historicState(startTime).state)
    ]
end

BR_Nursery_Heating_Started is a DateTime Item that is persisted and restored on startup. The group gAllVents is a group with all the vents as members. When the special mode ends, we loop through all the vents and send a command to restore them to the state they were in when the special heating mode started.

In fewer lines of code than it would take you to restore one vent using your current approach, I can restore ALL the vents. But I can’t do this if all of the controls are separated by room.

1 Like