[SOLVED] Push button wall switch (hold the change during pressing)

I have pushbutton wall switch NO (at press NC).
For this I use a rule to link the correct input to relay.
Only the light goes on and off immediately.
Have tried different timer options but do not come out.
I was thinking to hold the change for a second.
Does anyone have an idea please?

code 1


rule "Turn light on off"
when
    Item DI213 changed
then
    if (RO210.state == OFF) {
     RO210.sendCommand(ON)
}
else {
    RO210.sendCommand(OFF) }
end

code 2

rule "Turn light on off"
when
    Item DI213 changed
then
	if (RO210.state == OFF) {
	timer = createTimer(now.plusMillis(500)) [|
	RO210.sendCommand(ON)
	timer = null
	]
	}
else {
	timer = createTimer(now.plusMillis(500)) [|
	RO210.sendCommand(OFF)
	timer = null
	]
	}
end

I have to assume that your Item “DI123” is linked to the momentary wall switch.
You are using both rules with the trigger “changed”, if it is a momentary switch you will have a change when pressing and when releasing. Both your rules would trigger twice. I suggest you only trigger on “changed to on”.

As @opus said, this should do:
Also you don’t really need the {} here because it’s a single instruction after the if/else statements

rule "Turn light on off"
when
    Item DI213 changed to ON //Button pressed
then
    if (RO210.state == OFF) RO210.sendCommand(ON)
    else RO210.sendCommand(OFF)
end
1 Like

Even shorter:

rule "Turn light on off"
when
    Item DI213 changed to ON //Button pressed
then
    RO210.sendCommand(if (RO210.state == OFF) ON else OFF)
end
1 Like

Yes, that’s better. Thanks!!

rule "Turn light on off"
when
    Item DI213 changed to CLOSED //Button pressed
then
    RO210.sendCommand(if (RO210.state == OFF) ON else OFF)
end