Need help with a rule

Hi so i have a problem with a rule idk what to do.

This is not working:

rule “2”
when
Item TEST changed
then if (TEST.state == ON) {
createTimer(now.plusSeconds(5)) [
if (TEST.state == OFF) {
TEST2.sendCommand(ON)}]}[{
createTimer(now.plusSeconds(5))
TEST2.sendCommand(OFF)]}

end

In this way it works

rule “2”
when
Item TEST changed
then if (TEST.state == ON) {
createTimer(now.plusSeconds(5)) [
if (TEST.state == OFF) {
TEST2.sendCommand(ON)}]}

end

But after this is executed i need the item TEST2 to be turned off after 5 seconds, but it needs to be in this rule because i have another rule that when i turn TEST3 on it turns TEST2 on and vice versa and when this is activated in this way it cant turn off after 5 seconds, so what i need is that when TEST is on if i turn it off before 5 seconds TEST2 will turn on and after 5 seconds TEST2 turns off basically a toggle switch but only in this rule.

Does that do what you want?
It is not very clear what you want to do in the rule

rule “2”
when
    Item TEST changed
then
    if (TEST.state == ON) {
        createTimer(now.plusSeconds(5), [ |
            if (TEST.state == OFF) {
                TEST2.sendCommand(ON)
             }
        ])
    }
    createTimer(now.plusSeconds(5), [ |
        TEST2.sendCommand(OFF)
    ])
end
1 Like

It was not working but i just changed some stuff on your idea and its working now thanks for the help and yes i know its a weird rule but this is exactly what i needed thanks this is how it is working.

rule “2”
when
Item TEST changed
then if (TEST.state == ON) {
createTimer(now.plusSeconds(5)) [
if (TEST.state == OFF) {
TEST2.sendCommand(ON)}
createTimer(now.plusSeconds(5)) [
TEST2.sendCommand(OFF)]]}

end

Glad you got it,
Please use code fences in the future