How to "remember" a light state within a rule (plus "for a group of lights")?

Hello everyone,

I’m looking for ideas and impulses to improve on a ruleset.
I want to have a group of light items with color and brightness. I then want to change all lights in this group to HSBType::RED when an alert item triggers and back to the state they had before when the alert is turned off.
I have implemented that for two lights “by hand” but would like to have that over a whole group of lights.
Additionally I have an issue with the rule below:
Imagine the alert light was set to a color but is turned off. Now when I send brightness is set to 100 it lights up in the last set color. After the alert triggered once the light will be red if brightness is set to 100. Note that if the light was on it reverts to its previous color and brightness correctly.

So in summary two questions:

  1. How do I change the light via a rule but “remember” the exact previous state (variable HSType seems to be not enough)
  2. How can I achieve this for a whole group of lights?

Thanks in advance!

The rule in question:

var Timer lightTimer = null
var Boolean gateTimerRunning = false
var Boolean warning = false
var HSBType hsb = null
var Number dimmer = 0

rule "GateOpen"
when
        Item stairGateOpen received update or
        Item cellarDoor received update or
        Item vBabySleepMode received command OFF
then
        if (!gateTimerRunning && vBabySleepMode.state == OFF) {
                gateTimerRunning = true
                createTimer(now.plusSeconds(5), [ |
                        if (stairGateOpen.state == CLOSED && cellarDoor.state == CLOSED && warning) {
                                wzIndirectLightColor.sendCommand(hsb)
                                hsb = null
                                stairGateLight.sendCommand(OFF)
                                warning=false
                        }
                        else if (!warning && (stairGateOpen.state == OPEN || cellarDoor.state == OPEN)) {
                                warning = true
                                hsb = wzIndirectLightColor.state
                                stairGateLightBrightness.sendCommand(80)
                                wzIndirectLightColor.sendCommand(HSBType::RED)
                        }
                        gateTimerRunning = false
                ])
        }
end

The two light items for completeness sake:


Dimmer wzIndirectLightDimmer "Wohnzimmer Indirektes Licht" <light> (gLivingroomLights, gWZIndirectLight ) ["Dimmer", "Lighting"] { channel="hue:0210:huebridge:tWZIndirekt:color"}
Color wzIndirectLightColor "Wohnzimmer Indirektes Licht Farbe" <light> (gWZLightColors, gWZIndirectLight) ["Color", "Lighting"] { channel="hue:0210:huebridge:tWZIndirekt:color", alexa="Lighting"}

Switch stairGateLight "Gatter Warnlicht" <light> (gAlarmLights) ["Switch"] { channel="mqtt:topic:localBroker:tGateWarnlight:state" }
Dimmer stairGateLightBrightness "Gatter Warnlicht Brightness" <light> (gAlarmLights) ["Switch"] { channel="mqtt:topic:localBroker:tGateWarnlight:brightness" }

There’s a built-in way of doing exactly this with storeStates and restoreStates. There’s a brief description in the docs:

and if you search the forum for storeStates you’ll find many valid examples.

1 Like

In the “classic” rule you can probably do something with this code snippet I use in my “old rules” to iterate through the group and send a command:

Bewegung_LED_Rot.allMembers.forEach[item | 
	                logInfo("Red LED Switch", "item: " + item.label.toString() + " switch red LED ON" )
					sendCommand(item, ON)
					Thread::sleep(200)
					]

Hope this helps.

That looks like the keyword I was missing, thanks! I’ll drop back with the solution once found although it’ll be a few days :wink:

Thanks a bunch!

Another option would be to use a time stamp. Just remember the moment before switching the lights to alert. When alert ends, do a

MyLights.members.forEach[light|
    val oldState = light.historicState(timestamp).state
    if(light.state != oldState)
        light.sendCommand(oldState.toString)
]

Thanks @Udo_Hartmann I’ll look into this and if it’s only to learn a bit more :heart:

@JustinG sadly the issue is the same - perhaps this is worth an own thread.

To give a specific example:

  • Turn the light yellow. State is now 60/100/100
  • Turn it off. State is now 60/100/0 (switching sometimes to 48/96/0 or something which I don’t understand but seems unrelated)
  • run the rule above. Light changes to 0/100/100 while alert is true, switches to 60/100/0 for a few microseconds and then to 0/100/0.

Any future turning on is then red and not yellow.

I think I’ll rebuild the rule with no other complexity and only a debug trigger and make a separate thread for this - the group question is answers thanks to @JustinG!

I went with @JustinG for the group question and have openend a dedicated, more streamlined topic for the color issue:

Thanks again everyone!