Setting item to a previous state based on different item

I’ve been chugging along with OH2 for several months now and have had a lot of fun doing it but I’m stumped trying to figure out something.

I’m controlling my HVAC (heat/cool modes) via an arduino with relays on the thermostat lines. I also have a bunch of motorized vents or dampers to most of my rooms. I like to use the HVAC system to maintain nursery and master bedrooms temperatures at night by running the lower stage heat and fan and closing off a lot of the other rooms (in order to get temps in line quickly and efficiently).

When the furnace turns on I close vents in other rooms and afterwards I need to re-open them. That part is easy enough but I’d like to return them to their previous state. If they were previously closed I want them to remain closed. If they were previously open I’d like them to return to being open.

Each room has its own rule file. When the heat/cool trigger changes I want a rule to fire off to close the vent or reset it but I don’t know how to do this part.

Heating/Cooling Items:
AuxHeatUpper (on off)
AuxHeatLower (on off)

example room:

rule "LivingRoom Reset Vent"
when
        Item AuxCoolUpper changed or
        Item AuxHeatUpper changed
then
       if ( AuxCoolUpper.state == ON || AuxHeatUpper.state == OFF )
             VT_Vent_Mode_LR.postUpdate(0) <---- 0 = off 1 = on 2=heat 3=cool
       if (AuxCoolUpper.state ==OFF || AuxHeatUpper.state == OFF)
             VT_Vent_Mode_LR.postUpdate( previous state ????)
end

i 've played with previousState a little but it seems to only work when whatever item is the trigger. I don’t know how to use previous states of items not triggered off of

thanks

Is VT_Vent_Mode_LR a number-item and how did you configure the persictance?

Yes, number item. Rr4jd every change every minute

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

I’ll give that a shot, thanks!
I guess i was sort of close but I think it didn’t work because i was trying to do it in a single rule - no wonder it didn’t work.

Thanks again, I just test it out on a single item with some test triggers and it worked perfectly.

I’ve never heard of this previousstate storestate thing. perfect! thank you again

It’s an old Action that is not really fully covered in the OH 2 docs I think. I’ve honestly never used it and kind of had my fingers crossed that it would work for you. :wink: Glad it did.

However, if you have a lot of vents, you might want to look into the second more complicated option because if OH ever goes down while most of your vents are closed you will have to individually readjust each and every one because the previous state will have been lost.

eh, we’re only talking about 10 rooms.

you might want to double check what topic you were wanting to reply to there ^^^

I hate it when that happens. Now to find the one where this belonged.

As for the the vents, 10 rooms, if your house is like mine, would be roughly 20 vents. That’s a lot of vents to adjust.