Loop in rule of light status for switch

I need help for a rule, I have a sonoff t1 as wallswitch, for sending mutt commands on and off,the lamp is not on that switch, because the lamp is a zigbee lamp that is connected all the time with power, I have a rule that when I press the sonoff switch, the lamp goes on and off, I also have a remote control to turn on the lamp from the couch, this is directly paired with the lamp and not in openhab, I can turn on and off the lamp also directly in the sitemap.
now I would like to tell the switch, that I turned on and off the lamp from sitemap or with the remote control, that the light is on of the switch and that the state is all time the same like the status of the lamp:

this is my test rule:

rule "Schaltersteuerung"
when
    Item Sonoff_Touch_GF_Living changed
then
    if (Sonoff_Touch_GF_Living.state == ON) {
        // Wenn der Schalter eingeschaltet wird, schalte die Lampe ein
        Ikea_Pilskott_Livingroom_Lampe.sendCommand(ON)
    } else if (Sonoff_Touch_GF_Living.state == OFF) {
        // Wenn der Schalter ausgeschaltet wird, schalte die Lampe aus
        Ikea_Pilskott_Livingroom_Lampe.sendCommand(OFF)
    }
end

rule "Schalter ausschalten"
when
    Item Ikea_Pilskott_Livingroom_Lampe changed from ON to OFF
then
    if (Sonoff_Touch_GF_Living.state == ON) {
        // Wenn der Schalter eingeschaltet ist, schalte den Schalter aus
        Sonoff_Touch_GF_Living.sendCommand(OFF)
    }
end

the part Schalterstellung is working, but when I try to add Schalter ausschalten, than I can’t turn on the light with the sonoff switch, it goes off immediately after turning on.
why is this?

i had also a rule like this bevor:

rule "Lampensteuerung"
when
    Item Ikea_Pilskott_Livingroom_Switch changed
then
    if (Ikea_Pilskott_Livingroom_Switch.state == ON) {
        // Wenn die Lampe eingeschaltet wird, schalte den Schalter ein
        Sonoff_Touch_GF_Living.sendCommand(ON)
    } else if (Ikea_Pilskott_Livingroom_Switch.state == OFF) {
        // Wenn die Lampe ausgeschaltet wird, schalte den Schalter aus
        Sonoff_Touch_GF_Living.sendCommand(OFF)
    }
end

rule "Schaltersteuerung"
when
    Item Sonoff_Touch_GF_Living changed
then
    if (Sonoff_Touch_GF_Living.state == ON) {
        // Wenn der Schalter eingeschaltet wird, schalte die Lampe ein
        Ikea_Pilskott_Livingroom_Switch.sendCommand(ON)
    } else if (Sonoff_Touch_GF_Living.state == OFF) {
        // Wenn der Schalter ausgeschaltet wird, schalte die Lampe aus
        Ikea_Pilskott_Livingroom_Switch.sendCommand(OFF)
    }
end
Diese Regel besteht aus zwei Teilen: "Lampensteuerung" und "Schaltersteuerung". Die "Lampensteuerung" reagiert auf Änderungen des Status der Lampe und Ă€ndert den Status des Schalters entsprechend. Die "Schaltersteuerung" reagiert auf Änderungen des Status des Schalters und Ă€ndert den Status der Lampe entsprechend.

Stelle sicher, dass du die Items "Sonoff_Touch_GF_Living" und "Ikea_Pilskott_Livingroom_Switch" in deiner OpenHAB-Konfiguration definiert hast, und passe die Item-Namen in den Regeln an, wenn sie von den hier verwendeten Namen abweichen.



User
aus irgendeinen Grund erzeugt das script einen loop

ChatGPT
Ein Loop in deinem Regel-Skript könnte auftreten, wenn sich die ZustĂ€nde der Items gegenseitig beeinflussen und die Regeln immer wieder ausgelöst werden. Um dies zu vermeiden, kannst du eine zusĂ€tzliche Bedingung hinzufĂŒgen, um sicherzustellen, dass die Regel nur dann ausgelöst wird, wenn der Status des Schalters oder der Lampe tatsĂ€chlich geĂ€ndert wurde. Hier ist eine aktualisierte Version der Regeln:

xtend
Copy code
import org.eclipse.smarthome.model.script.ScriptServiceUtil

var boolean lampWasTurnedOn = false
var boolean switchWasTurnedOn = false

rule "Lampensteuerung"
when
    Item Ikea_Pilskott_Livingroom_Switch changed
then
    if (Ikea_Pilskott_Livingroom_Switch.state == ON && !lampWasTurnedOn) {
        // Wenn die Lampe eingeschaltet wird und der Schalter nicht bereits eingeschaltet ist
        Sonoff_Touch_GF_Living.sendCommand(ON)
        lampWasTurnedOn = true
        switchWasTurnedOn = true
    } else if (Ikea_Pilskott_Livingroom_Switch.state == OFF && lampWasTurnedOn) {
        // Wenn die Lampe ausgeschaltet wird und der Schalter nicht bereits ausgeschaltet ist
        Sonoff_Touch_GF_Living.sendCommand(OFF)
        lampWasTurnedOn = false
        switchWasTurnedOn = false
    }
end

rule "Schaltersteuerung"
when
    Item Sonoff_Touch_GF_Living changed
then
    if (Sonoff_Touch_GF_Living.state == ON && !switchWasTurnedOn) {
        // Wenn der Schalter eingeschaltet wird und die Lampe nicht bereits eingeschaltet ist
        Ikea_Pilskott_Livingroom_Switch.sendCommand(ON)
        lampWasTurnedOn = true
        switchWasTurnedOn = true
    } else if (Sonoff_Touch_GF_Living.state == OFF && switchWasTurnedOn) {
        // Wenn der Schalter ausgeschaltet wird und die Lampe nicht bereits ausgeschaltet ist
        Ikea_Pilskott_Livingroom_Switch.sendCommand(OFF)
        lampWasTurnedOn = false
        switchWasTurnedOn = false
    }
end
In dieser aktualisierten Version werden Flags (lampWasTurnedOn und switchWasTurnedOn) verwendet, um zu verhindern, dass die Regeln in einer Endlosschleife ausgelöst werden. Die Flags werden gesetzt, wenn der Status geĂ€ndert wird, und die Regeln prĂŒfen, ob der Schalter oder die Lampe bereits in den gewĂŒnschten Zustand versetzt wurden, bevor sie Aktionen ausfĂŒhren. Dadurch wird vermieden, dass die Regeln sich gegenseitig immer wieder auslösen.

but with that rule, when I turn on or off the light, I have a loop and the switch and light is turning on and off all the time very fast, but I don’t know why, I never have a turn off when on or turn on when off in the rule.

I think ChatGPT already explained what the problem is. I can‘t explain it better.
In rule #1 Sonoff_Touch_GF_Living gets triggered and switches Ikea_Pilskott_Livingroom_Lampe on.
That triggers Ikea_Pilskott_Livingroom_Lampe in your rule #2 and switches on
Sonoff_Touch_GF_Living
That triggers rule #1.

There are several ways to break out of the loop. I would go for a „follow“ profile. You do not need the second rule anymore.

Well, the simplest option would be to use not a switch but a rocker at your Sonoff (like the one in the remote control). This way you could use a simple toggle rule:

rule "Taster an Lampe"
when
    Item Sonoff_Touch_GF_Living changed to ON
then
    Ikea_Pilskott_Livingroom_Lampe.sendCommand(
        if(Ikea_Pilskott_Livingroom_Lampe.state != ON)
            ON
        else
            OFF
    )
end

So each time the rocker is pressed, the light will toggle, no need to send the Light state at all.

If you have to have an active feedback, you will have to use another channel (i.e. split the Sonoff input from the Sonoff output to use the two independently)

thank you very much, I did it like this now:

rule "Taster an Lampe"
when
    Item Sonoff_Touch_GF_Living changed
then
    Ikea_Pilskott_Livingroom_Lampe.sendCommand(
        if(Ikea_Pilskott_Livingroom_Lampe.state != ON)
            ON
        else
            OFF
    )
end

this is working good for me :slight_smile:

1 Like