Tips: Using Previous States after an Automation

Lately I've been going a bit more advanced in some of my automations thanks to my wife/Partner and the W/PAF (Wife/Partner Appreciation Factor) being high with HE.

Where I struggled the most was in trying to understand the most effecient use of Private Booleans, Variables and Global Variables along with how to capture much more complex previous states.

To help the future person like me who was wrestling with this, I've created this post to document two specific scenarios I have as an example for others and ideally conversation around even better ways to achieve some of these or other advanced automations.

While my use-cases may be unique, they can easily be applied to a ton of other scenarios where you wish you could easily know or even get back to your previous state before the automation kicked in.

So on to the automations...

  1. Turn on Closet Light when door opens, turn off when closed, but leave on if the light was already on before the door opened

To achieve this, I was really unsure if I should go down the path of a Global Variable to store the state of the light being on or off when the door is opened, or something else. In a thread I can't find right now, it was suggested that using a Private Boolean was the most resource friendly way to use a variable that is only needed in one Rule.

Here is where I landed.
i. If the Closet door is opened, set the Private Boolean to the current state of the light (on/off), then proceed to turn on the light. Set a cancel-able timer to turn off the light if the door is left open for too long. Also set the light dim value per mode.

ii. If the Closet door is closed, and the light wasn't already on, cancel the timer set on open, and turn off the light. If it was already on, just leave the light on.

  1. Turn on a large set of lights when my Roomba is cleaning, when it's done, revert all of the lights back to the state they were in before the Roomba kicked off.

i. First thing Id did was create a scene for the Roomba Cleaning which turns on the lights I want on.

ii. Secondly I created another scene in the reverse, same devices, just all set by default to off. For this one I turned on the "Create Capture Device", so I can use this scene as basically a variable for capturing the state before the Roomba starts cleaning.

iii. Third, I created a Rule that will Capture the current state of lights when the Roomba starts cleaning, then after 5 seconds activates the Roomba Cleaning scene.
When the Roomba is done cleaning, I activate the previous stored scene so my house is exactly in the state it was before the Roomba kicked off.

I hope this is helpful to others!
And if you see any place I could have optimized, I'm all ears to make improvements here!


This could be done much simpler by using Required Expression: Light is Off. Predicate Expression supports the concept of state transitions. If the light is on, there is (a) no need to turn it on when the door opens, and you don't want to turn it off when the door closes. So just don't allow the rule to run by use of the Predicate Expression. Also, instead of IF-THEN-ELSE on the Mode, just use Dimmer Per Mode. No need to even touch Private Boolean, or test it.

RM has a built-in Capture-Restore function. So, change the rule's trigger to Roomba J switch reports On. Lose the IF-THEN-ELSE, and replace with:

Capture [your list of lights]
Wait for Event: Roomba J switch reports Off


Oh wow, this is gold. thank you. A small question:

Love this. The only thing I think is missing is if I use the Predicate Expression, while it captured the no-op if the light is already on before I open - when I close the door, I can't instantly turn the light off, as this rule won't run given the light is now on before that state. Or am I missing how Predicate Expression works?


1 Like

this is invaluable info, excited to try it. question, can "capture/restore" be used for sonos devices, as such that it stops playing music, speaks a tts as defined in a RM alert, then continues playing the playlist? i understand it could restart the song or go to the next song and that's fine, but if this functionaliry works... well.. merry Christmas to me!

Yes, they are evaluated prior to the rule being triggered, whenever the corresponding state changes. So, when the light was turned on or off, the Required Expression becomes false or true.

I don't think you should used changed for the trigger of this. That's just throwing good information away, namely, did the door open. If the door opens, just turn on the light. Then wait for either the door to close, or if you want to turn off the light after 2 minutes, add a timeout to the Wait for Event contact closes. Followed by the Off. No need for IF-THEN-ELSE.

Dimmers per mode....
Wait for Event Contact closed, timeout 0:02:00
Off light

What was 18 lines of actions becomes 6.


This is a completely different thing. Sonos does support Play Text and Restore, which should do what you want. That is what is normally used when using Sonos as a TTS device.

Fantastic, thank you very much!

thanks for the response. that feature stopped working a long time ago (2 years). it's a sonos api issue, not a hubitat issue.

Could you use a variable to store the URI (as the previous state), then you re-apply it again after your automation? That probably will restart the track, but better than nothing.

Perhaps, I have minimal experience with variables in Hubitat, but this thread has me motivated to learn. Thanks for the idea.

I have the following vacation rule where any outside motion triggers a song to play on my Sonos:

The URI string I hardcoded in this rule, but it could easily come from a hub variable and be updated.

Edit: I'm pretty sure I can simplify this by setting preconditions first, which I will do!

1 Like

Download the Hubitat app