[SOLVED] Automatic lights . Beginner

Hi guys . i’m new on the openhab and i have some problems.i want to automate my lights but at a button press i wanna change light state ( ON-Off and reverse) :)) . i wrote the code but is not working .

this is my items tab :

//Relays
Switch channela “Bec Interior” { gpio=“pin:26 activelow:yes initialValue:high” }
Switch channel2 “Bec Exterior” { gpio=“pin:19 activelow:yes initialValue:high” }
Switch channel3 “Channel 3” { gpio=“pin:13 activelow:yes initialValue:high”}
Switch channel4 “Channel 4” { gpio=“pin:6 activelow:yes initialValue:high”}

Contact DoorSensor “Door Sensor [%s]” { gpio=“pin:16 debounce:10 activelow:yes” }
Contact contact_interior { gpio=“pin:24 debounce:30 activelow:yes” }

and this is my rules tab:

rule “Update light when button pressed”
when
Item contact_interior changed from OPEN to CLOSED
then
if(channela.state == ON){sendCommand(channela,OFF)
//LogInfo(“interior”,“Switching to OFF”)
}
else{ sendCommand(channela,OFF)
//LogInfo(“interior”,“Switching to OFF”)
}
end

and this is my sitemap tab:

sitemap home label=“Andy’s House”
{

   Switch item=channela
   Switch item=channel2
   Switch item=channel3
   Switch item=channel4

   Text item=contact_interior
   Text item=DoorSensor

idk what i’m doing wrong . when the button is pressed . nothing happens

You are setting it to OFF in both cases!

rule “Update light when button pressed”
when
    Item contact_interior changed from OPEN to CLOSED
then
    if(channela.state == ON) {
        channela.sendCommand(OFF)
        //logInfo(“interior”,“Switching to OFF”)
    } else {
        channela.sendCommand(ON)
        //logInfo(“interior”,“Switching to ON”)
    }
end

What happens in the log when you press the button?

oh yea. my fault . thx a lot :))

Please note the syntax item.sendCommand(COMMAND) instead of sendCommand(Item, COMMAND)

It is recommended to use the first one and only use the second one for group commands and the like.

See:

https://docs.openhab.org/configuration/rules-dsl.html#sendcommand-method-vs-action