Rule to return light to previous value

Is it possible to create a RM rule to return a light to it's previous value? Example: I have a rule that dims my living room light to 1% when the Chromecast is playing content. I have a second rule that sets the light to a specific level when Chromecast stops. Rather than programming a specific value to set the light to in this second rule, I would like to return it to the state it was in prior to being dimmed by the first rule, whatever that value was. Is this possible? If so, how?

Seems like capture and restore might be my answer here, but I can't figure out how to use them.

Capture and Restore have to happen within the same rule. Another way to do it is to put the dimmer value into a variable, and then use the variable to set the dimmer later. This latter could be done with two rules and a Hub Variable. You can set a variable to a device attribute, in this case 'level'. And you can set a dimmer from a variable.

2 Likes

I was thinking variables might be the answer, too. Is there a guide on how to create and use them? If not, could you point me in the right direction?

That sounds like exactly what you want. Just try the actions, and you should figure it out. A capture lets you choose what to capture (erasing any previous captures, which are stored per-Rule), and a restore restores everything from the (last) capture.

1 Like

Create them with Settings / Hub Variables. Then, within RM there are actions that will select them.

1 Like

To save the level, put it in a variable, like this:

Later, to set the lamp to the saved level:

Ah! I didn't know I had to create the variable under settings first. Explains why "Set Variable" (as shown in your screen shot) was not available when I chose "Set Variable, Mode or File, ..." Working on building the rules now. I'll update shortly.

One thing to be aware of is that variables have a 'type', like Number, Decimal, String, etc. A dimmer device has two attributes, level and switch. level is a number, and switch is a string (on or off). So when you go to assign an attribute to a variable, those types have to match up. It is possible to convert strings to numbers (%variable-name% in a string setting), and numbers to strings (one set variable choice is 'numeric string'), if need be.

1 Like

Hot damn, that worked! Simple enough once I knew how it worked properly. Thanks!

3 Likes

Thanks for the tips. As @bravenel mentioned, I didn't know Capture and Restore had to be in the same rule. So that explains why it wasn't working for this situation, but I probably could create one big rule to dim the lights when Chromecast starts and restore them when it stops.

Yeah, that's what I was thinking: trigger on Chromecast start, capture, wait for Chomecast stopped, restore. (Those Chromecast event names will absolutely not be correct, so use whatever works for what the device actually reports, but....same idea.)

But doing it yourself in variables and however many rules you need certainly works too!

1 Like

To clean up the programming I may create one rule eventually and delete these two, but for now I decided it was easier to modify the two existing rules.

This is working great so far. Now I would like to expand the functionality and need a little more help.

During the day, when there is plenty of natural light, the lamp might not be on when I'm using the living room, where the TV is located. Or it might be, if it's really cloudy outside, or if I'm reading, etc. But there is always enough daylight in the room during the day that any time the lamp is on when I start watching TV, I want it to not just dim to 1%, but shut off. Then when I'm done watching TV (ie. Chromecast source is None), I want the lamp to stay off if it was already off, or come back on to its previous value if it was on.

Seems like the key here would be to create a hub variable to capture the on/off status of the lamp when the rule fires, just as I did to capture the dimmer value. I've done this, and it works great:

image

The problem I'm running into is using this value in my second rule, the one that returns the light to its previous state when Chromecast stops. I can't seem to find a way to set a switch based on a variable:

Please tell me this is possible. Should I use a different device type besides switch?

Have you tried using Conditional Actions within actions?

Should look something like this:
IF (Variable Activity = true) THEN
Off: Light Living Room
END-IF

You could also specify that it should only dim if the light is on rather than using a variable.

You can't do so directly, so you'll have to write a conditional--as suggested above--to create the same effect. In your case, it looks like something like this:

IF (Living Room Light state is "on") THEN
  Dim: Living Room Light to 1%
END-IF

Or if you wanted to do something different in both cases (sounds like not since it would have already been off?),

IF (Living Room Light state is "off") THEN
  // do whatever
ELSE
  Dim: Living Room Light to 1%
END-IF

I'm also guessing a bit at what your variable means and when it was captured, so I might have this backwards...in which case, I guess flip things around. :smiley:

And if it's already off and that's when you don't want to change things, the idea above to just check the switch state at that time should also work--without using a variable. You'll still need a condition of some sort, like the above, to test that.

As I expand the functionality of this rule, I run into unintended consequences. Naturally. :joy:

Here's what the rule looks like currently:

image

The intent is to dim the light to 1% when I start playing any content EXCEPT YouTube Music since I do not need the lights to dim when just listening to music.

The unintended consequence is that if I am watching, for example, Netflix, and then switch to YouTube, the lights are already at 1% so that is the new value that the variable gets set to, so when I am done watching content and the Chromecast source returns to None, the light stays at 1%. Any thoughts on how to avoid this?

You could make a local variable (or hub variable) to store the last media source--maybe set the value of this variable at the end of your current rule (either inside or outside of your IF THEN, depending on which specific transitions you care about--probably outside, which means you'll also need to put in the missing END-IF so this is possible to do). Then, you can check the value of that variable before doing anything to see if you should, in fact, do that thing.

That being said, I don't see anything in your Rule that ever restores any state from the (manually) "captured" lights, so if that's what's happening, there must be more going on, and it would be good to see the entire set of rule(s)--because this definitely shouldn't do that as-is. :slight_smile:

Alternatively, you can look into the possibility of a predicate rule (formerly called a predicate condition). If the predicate evaluates to false, a trigger event will not trigger the Rule, so that is a way you can avoid doing things (or only do things) in response to specific transitions.

1 Like