[SOLVED] IF statement doesn't work properly

Hello,
I have this problem with my open hab, first of all, this is the code

home.items

//Virtual button
Switch cam1_sa1 "Setul1" (Camera1) { gpio="pin:18 activelow:yes initialValue:high" }

// Push button
Contact cam1_intrerupator "Setul1a" { gpio="pin:19 activelow:yes" }

home.rules

rule "test1"
when
    Item cam1_intrerupator changed
then
    if (cam1_sa1.state == ON) {
        cam1_sa1.sendCommand(OFF)
    }
    else if (cam1_sa1.state == OFF) {
        cam1_sa1.sendCommand(ON)
    }
end

Now…the problem:))

If the light is off and I press on the button, it lights up and remains… if i press press the button to turn off the light, it will not turn off. it will remain off as long as the button is pressed or if I repeatedly pressed on the button (by mistake it goes out)

I really don’t know what to do, i’ve reinstalled the Openhab2 and nothing. I also changed the GPIOs

At the moment i do not see anything strahe in the if statement, bit to compare with a State you should use === instead of ==

Second add some lof statements. Show the cam1_intrerupator, the show the stanes in the if clause like New the cam1 is on and it should go off now

1 Like

I think I know why,

cam1_intrerupator will change to CLOSED when you push it and OPEN when you release
Change the trigger of your rule to:

rule "test1"
when
    Item cam1_intrerupator changed to CLOSED
then
    if (cam1_sa1.state == ON) {
        cam1_sa1.sendCommand(OFF)
    }
    else if (cam1_sa1.state == OFF) {
        cam1_sa1.sendCommand(ON)
    }
end

Now the rule will only trigger when you push the button down. Before, it was also triggering when releasing.
Also I highly recommend that you debounce the switch by editing the file services/gpio.cfg
as such:

debounce=10
1 Like

same thing…
i’ve seen in the log that the OH2 is running twice the rule.

//After i saved the “home.rules”

2018-11-04 21:08:47.720 [vent.ItemStateChangedEvent] - buc1_intrerupator changed from UNDEF to OPEN
2018-11-04 21:08:47.724 [ome.event.ItemCommandEvent] - Item ‘cam1_sa1’ received command ON

//After i pushed the button once.

2018-11-04 21:09:56.902 [vent.ItemStateChangedEvent] - buc1_intrerupator changed from OPEN to CLOSED

2018-11-04 21:09:56.911 [ome.event.ItemCommandEvent] - Item ‘cam1_sa1’ received command OFF

2018-11-04 21:09:56.922 [vent.ItemStateChangedEvent] - cam1_sa1 changed from ON to OFF

2018-11-04 21:09:57.105 [vent.ItemStateChangedEvent] - buc1_intrerupator changed from CLOSED to OPEN

2018-11-04 21:09:57.120 [ome.event.ItemCommandEvent] - Item ‘cam1_sa1’ received command ON

2018-11-04 21:09:57.132 [vent.ItemStateChangedEvent] - cam1_sa1 changed from OFF to ON

That should not happen if you change the rule trigger

rule "test1"
when
    Item cam1_intrerupator changed to CLOSED
then
    ....

Cooll…now it works!

I didn’t had the services/gpio.cfg, i need to have something else on that file beside the debounce=10? I don’t know, some things that needs to be there?

Nope. that’s it
Please mark the thread as solved, thanks

Thank youuu :blush:

Only use === when null is on one side of the comparison. In all other cases, including NULL you must use ==. The two behave differently and if you use === in some cases you will get the wrong answer.

For example.

val Number testNum = 20

// Lets assume Item Temperature is set to 20

if(Temperature.state === null) // correct answer
if(Temperature.state == null) // potentially can cause an exception
if(Temperature.state === testNum) // incorrect answer, will return false
if(Temperature.state == testNum) // correct answer
1 Like

I was sure i read it somewhere differently. But i didnt find it anymore. Thats the problem of the documentation …

Thanks @rlkoshak