How to set the light state back after blinking

Hello,

I have a problem with my rules.
I’m using OpenHAB 2.5.3 on my Raspberry.

I want let my leds blinking if a call is coming in.
That works fine, but when the call is over the leds shoud go to their state before.

So i need to save the info about of the different Switches from the lights.
But at the moment I only get the reference of the Items but I need a copy of the lights.

That’s my code of the rule:

rule "Heimtelefon Anruf"`

when
    Item fboxRinging changed to ON or
    Item HandyVincent_CallState changed to "RINGING"
then

val logName = "Anruf"

//If i'm not at home or sleeping, do nothing
if (HandyVincent_Zuhause.state != ON || SchlafenszeitIsActive.state == ON) {
    return
}

if (isRinging.apply()) {

    //Wait 1 second because my smartphone needs some time to set the callername
    Thread::sleep(1000)

    val callerName = getCallername.apply()

    logInfo(logName, "Callername: " + callerName)

    GoogleHome_Lautstaerke.sendCommand(50)

    timerBlink = createTimer(now, [|
        say("Du wirst gerade von " + callerName + " auf dem Telefon angerufen!")

        if (isRinging.apply() == false) {
            return 
        }
    ])

val lampenInitial = WohnzimmerDeckenLampenSchalter.getAllMembers

    timerBlink = createTimer(now.plusSeconds(1), [|
        if (isRinging.apply()) { //If someone is calling
            if (turnOn) {
                WohnzimmerDeckenLampenSchalter.sendCommand(ON)
                turnOn = false

                //The lights are 2 seconds on
                timerBlink.reschedule(now.plusMillis(2000))
            } else {
                WohnzimmerDeckenLampenSchalter.sendCommand(OFF)
                turnOn = true

                //The lights are 1 second off
                timerBlink.reschedule(now.plusMillis(1000))
            }

        } else { //If the call is over
            lampenInitial.forEach[lampeInitial |

                //here I want to set the state back to the "initial" state
                WohnzimmerDeckenLampenSchalter.getAllMembers.forEach[lampe |
                    if(lampeInitial.name == lampe.name) {
                        logInfo(logName, "Match found: " + lampeInitial.name + "; State: " + lampeInitial.state)

                        lampe.sendCommand(lampeInitial.state.toString)
                    }
                ]
            ]
        }
    ])
} else {
    logInfo(logName, "Phone isn't ringing")
}
end

I hope youre understanding my problem and can help me, if you need to know something, please ask. :slight_smile:

Bit like this -

Thank for the reply :slight_smile:
Is there no way to safe them dynamically per rule?

So I would need to create for every lamp one new item and when I add a new lamp another one.

The post talks about a global variable, not Items.
You could keep many such in a Map (array) and key by Item name.

Though you can use a Item if you wish.
Then you might put them in a Group and use “Associated Items” naming conventions to make it easier.

Or you could use one of the persistence services to store and retrieve Item states as and when required, in a rule.

Or you could use store/restore states

Ohh wow, thank you very much.
That was easier than I thought :))

For all who are interested, I made it with a HashMap<String, String>.
At first save the values of all lamps before:

    //Create Hashmap with item name and switch state
    var HashMap<String, String> lamps = new HashMap<String, String>()

    //Save all ceiling lights in the HashMap
    for(var i = 0; i < WohnzimmerDeckenLampenSchalter.getAllMembers.size; i++) {
        var lamp = WohnzimmerDeckenLampenSchalter.getAllMembers.get(i)
        lamps.put(lamp.name.toString, lamp.state.toString)
    }

    val lampenInitial = lamps

And if the call is over, set it back:

            //Take the name of the HashMap and compare it with the names from the group and then send the command to the light
            lampenInitial.forEach[lampenNameInitial, lampenSchalterInitial |                    
                WohnzimmerDeckenLampenSchalter.getAllMembers.forEach[lampe |
                    if(lampenNameInitial == lampe.name.toString) {
                        lampe.sendCommand(lampenSchalterInitial)
                    }
                ]
            ]
1 Like